Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::thread_to_monitoring_adapter Class Reference

#include <thread_to_monitoring_adapter.h>

Collaboration diagram for kcenon::monitoring::thread_to_monitoring_adapter:
Collaboration graph

Classes

struct  collection_config
 

Public Member Functions

 thread_to_monitoring_adapter (std::shared_ptr< event_bus > bus)
 
bool is_thread_system_available () const
 
common::Result< std::vector< metric > > collect_metrics ()
 
std::vector< std::string > get_metric_types () const
 
common::VoidResult start_collection (const collection_config &cfg)
 
common::VoidResult stop_collection ()
 
 ~thread_to_monitoring_adapter ()
 

Private Attributes

std::shared_ptr< event_busbus_
 
std::atomic< bool > running_ {false}
 
std::thread worker_
 

Detailed Description

Examples
event_bus_example.cpp.

Definition at line 62 of file thread_to_monitoring_adapter.h.

Constructor & Destructor Documentation

◆ thread_to_monitoring_adapter()

kcenon::monitoring::thread_to_monitoring_adapter::thread_to_monitoring_adapter ( std::shared_ptr< event_bus > bus)
inlineexplicit

Definition at line 69 of file thread_to_monitoring_adapter.h.

70 : bus_(std::move(bus)) {}

◆ ~thread_to_monitoring_adapter()

kcenon::monitoring::thread_to_monitoring_adapter::~thread_to_monitoring_adapter ( )
inline

Definition at line 171 of file thread_to_monitoring_adapter.h.

References stop_collection().

Here is the call graph for this function:

Member Function Documentation

◆ collect_metrics()

common::Result< std::vector< metric > > kcenon::monitoring::thread_to_monitoring_adapter::collect_metrics ( )
inline

Definition at line 95 of file thread_to_monitoring_adapter.h.

95 {
96 std::vector<metric> out;
97
98#if MONITORING_THREAD_SYSTEM_AVAILABLE && defined(MONITORING_HAS_COMMON_INTERFACES)
99 try {
100 auto& container = kcenon::thread::service_container::global();
101 auto monitorable = container.resolve<kcenon::common::interfaces::IMonitorable>();
102 if (monitorable) {
103 // Convert minimal subset of thread_system metrics into adapter metrics
104 // thread_system v3.0 uses common::interfaces::IMonitorable
105 auto monitoring_result = monitorable->get_monitoring_data();
106 if (!monitoring_result.is_err()) {
107 const auto& snap = monitoring_result.value();
108 // Map metrics from common_system snapshot
109 for (const auto& m : snap.metrics) {
110 metric adapted{m.name, m.value, {}};
111 out.emplace_back(std::move(adapted));
112 }
113 }
114 }
115 } catch (...) {
116 // Fall back to empty on any failure
117 }
118#endif
119
120 return common::ok(std::move(out));
121 }

References kcenon::monitoring::container.

Referenced by start_collection(), and TEST_F().

Here is the caller graph for this function:

◆ get_metric_types()

std::vector< std::string > kcenon::monitoring::thread_to_monitoring_adapter::get_metric_types ( ) const
inline

Definition at line 124 of file thread_to_monitoring_adapter.h.

124 {
125#if MONITORING_THREAD_SYSTEM_AVAILABLE
126 return {
127 "thread.pool.jobs_pending",
128 "thread.pool.jobs_completed",
129 "thread.pool.worker_threads"
130 };
131#else
132 return {};
133#endif
134 }

Referenced by TEST_F().

Here is the caller graph for this function:

◆ is_thread_system_available()

bool kcenon::monitoring::thread_to_monitoring_adapter::is_thread_system_available ( ) const
inline
Examples
event_bus_example.cpp.

Definition at line 74 of file thread_to_monitoring_adapter.h.

74 {
75#if MONITORING_THREAD_SYSTEM_AVAILABLE
76 // thread_system v3.0+ uses common_system interfaces
77# if defined(MONITORING_HAS_COMMON_INTERFACES)
78 // Dynamic discovery via service_container if present
79 try {
80 auto& container = kcenon::thread::service_container::global();
81 auto monitorable = container.resolve<kcenon::common::interfaces::IMonitorable>();
82 return static_cast<bool>(monitorable);
83 } catch (...) {
84 return true; // Headers present; treat as available even if not registered
85 }
86# else
87 return true; // thread_system available but no common interfaces
88# endif
89#else
90 return false;
91#endif
92 }

References kcenon::monitoring::container.

Referenced by main(), and TEST_F().

Here is the caller graph for this function:

◆ start_collection()

common::VoidResult kcenon::monitoring::thread_to_monitoring_adapter::start_collection ( const collection_config & cfg)
inline
Examples
event_bus_example.cpp.

Definition at line 138 of file thread_to_monitoring_adapter.h.

138 {
139 if (running_.exchange(true)) {
140 return common::ok(); // already running
141 }
142
143 if (!bus_) {
144 running_ = false;
145 return common::VoidResult::err(static_cast<int>(monitoring_error_code::operation_failed), "event_bus not set");
146 }
147
148 worker_ = std::thread([this, cfg]() {
149 while (running_.load()) {
150 auto res = collect_metrics();
151 if (res.is_ok() && cfg.publish_events && !res.value().empty()) {
152 bus_->publish_event(metric_collection_event("thread_system_adapter", res.value()));
153 }
154 std::this_thread::sleep_for(cfg.interval);
155 }
156 });
157
158 return common::ok();
159 }
common::Result< std::vector< metric > > collect_metrics()

References bus_, collect_metrics(), kcenon::monitoring::thread_to_monitoring_adapter::collection_config::interval, kcenon::monitoring::operation_failed, kcenon::monitoring::thread_to_monitoring_adapter::collection_config::publish_events, running_, and worker_.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop_collection()

common::VoidResult kcenon::monitoring::thread_to_monitoring_adapter::stop_collection ( )
inline
Examples
event_bus_example.cpp.

Definition at line 161 of file thread_to_monitoring_adapter.h.

161 {
162 if (!running_.exchange(false)) {
163 return common::ok();
164 }
165 if (worker_.joinable()) {
166 worker_.join();
167 }
168 return common::ok();
169 }

References running_, and worker_.

Referenced by main(), and ~thread_to_monitoring_adapter().

Here is the caller graph for this function:

Member Data Documentation

◆ bus_

std::shared_ptr<event_bus> kcenon::monitoring::thread_to_monitoring_adapter::bus_
private

Definition at line 174 of file thread_to_monitoring_adapter.h.

Referenced by start_collection().

◆ running_

std::atomic<bool> kcenon::monitoring::thread_to_monitoring_adapter::running_ {false}
private

Definition at line 175 of file thread_to_monitoring_adapter.h.

175{false};

Referenced by start_collection(), and stop_collection().

◆ worker_

std::thread kcenon::monitoring::thread_to_monitoring_adapter::worker_
private

Definition at line 176 of file thread_to_monitoring_adapter.h.

Referenced by start_collection(), and stop_collection().


The documentation for this class was generated from the following file: