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 60 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 67 of file thread_to_monitoring_adapter.h.

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

◆ ~thread_to_monitoring_adapter()

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

Definition at line 169 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 93 of file thread_to_monitoring_adapter.h.

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

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 122 of file thread_to_monitoring_adapter.h.

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

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 72 of file thread_to_monitoring_adapter.h.

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

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 136 of file thread_to_monitoring_adapter.h.

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

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

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 172 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 173 of file thread_to_monitoring_adapter.h.

173{false};

Referenced by start_collection(), and stop_collection().

◆ worker_

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

Definition at line 174 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: