Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
aggregating_monitor Class Reference

Aggregating monitor that collects metrics from multiple sources. More...

Inheritance diagram for aggregating_monitor:
Inheritance graph
Collaboration diagram for aggregating_monitor:
Collaboration graph

Public Member Functions

void register_component (std::shared_ptr< ci::IMonitorable > component)
 
kcenon::common::VoidResult record_metric (const std::string &name, double value) override
 
kcenon::common::VoidResult record_metric (const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags) override
 
kcenon::common::Result< ci::metrics_snapshot > get_metrics () override
 
kcenon::common::Result< ci::health_check_result > check_health () override
 
kcenon::common::VoidResult reset () override
 
std::shared_ptr< ci::IMonitor > get_monitor () override
 
std::shared_ptr< ci::IMonitor > create_monitor (const std::string &name) override
 
size_t get_component_count () const
 

Private Attributes

std::vector< std::shared_ptr< ci::IMonitorable > > monitored_components_
 
std::unordered_map< std::string, double > aggregated_metrics_
 
std::mutex mutex_
 

Detailed Description

Aggregating monitor that collects metrics from multiple sources.

Examples
monitoring_integration_example.cpp.

Definition at line 32 of file monitoring_integration_example.cpp.

Member Function Documentation

◆ check_health()

kcenon::common::Result< ci::health_check_result > aggregating_monitor::check_health ( )
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 97 of file monitoring_integration_example.cpp.

97 {
98 std::lock_guard<std::mutex> lock(mutex_);
99
100 ci::health_check_result result;
101 result.timestamp = std::chrono::system_clock::now();
102 result.status = ci::health_status::healthy;
103 result.message = "Aggregating monitor operational";
104
105 // Check health of all components
106 for (const auto& component : monitored_components_) {
107 auto comp_health = component->health_check();
108 const auto component_name = component->get_component_name();
109
110 if (kcenon::common::is_ok(comp_health)) {
111 const auto& component_result = kcenon::common::get_value(comp_health);
112 result.metadata["component_status:" + component_name] = ci::to_string(component_result.status);
113
114 if (component_result.status == ci::health_status::unhealthy) {
115 result.status = ci::health_status::unhealthy;
116 result.message = "One or more components unhealthy";
117 } else if (component_result.status == ci::health_status::degraded &&
118 result.status == ci::health_status::healthy) {
119 result.status = ci::health_status::degraded;
120 result.message = "One or more components degraded";
121 }
122 } else {
123 const auto& error = kcenon::common::get_error(comp_health);
124 result.metadata["component_status:" + component_name] = "error:" + error.message;
125 if (result.status == ci::health_status::healthy) {
126 result.status = ci::health_status::degraded;
127 result.message = "Component health check failed";
128 }
129 }
130 }
131
133 }
std::vector< std::shared_ptr< ci::IMonitorable > > monitored_components_
bool is_ok(const Result< T > &result)
Result< T > error(error_info info)
T & get_value(Result< T > &result)
error_info & get_error(Result< T > &result)

References kcenon::common::get_error(), kcenon::common::get_value(), kcenon::common::is_ok(), monitored_components_, and mutex_.

Here is the call graph for this function:

◆ create_monitor()

std::shared_ptr< ci::IMonitor > aggregating_monitor::create_monitor ( const std::string & name)
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 146 of file monitoring_integration_example.cpp.

146 {
147 // For this example, return self
148 return shared_from_this();
149 }

◆ get_component_count()

size_t aggregating_monitor::get_component_count ( ) const
inline
Examples
monitoring_integration_example.cpp.

Definition at line 151 of file monitoring_integration_example.cpp.

151 {
152 std::lock_guard<std::mutex> lock(mutex_);
153 return monitored_components_.size();
154 }

References monitored_components_, and mutex_.

◆ get_metrics()

kcenon::common::Result< ci::metrics_snapshot > aggregating_monitor::get_metrics ( )
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 69 of file monitoring_integration_example.cpp.

69 {
70 std::lock_guard<std::mutex> lock(mutex_);
71
72 ci::metrics_snapshot snapshot;
73 snapshot.source_id = "aggregating_monitor";
74 snapshot.capture_time = std::chrono::system_clock::now();
75
76 // Add own metrics
77 for (const auto& [name, value] : aggregated_metrics_) {
78 snapshot.add_metric(name, value);
79 }
80
81 // Collect metrics from all registered components
82 for (const auto& component : monitored_components_) {
83 auto comp_data = component->get_monitoring_data();
84 if (kcenon::common::is_ok(comp_data)) {
85 const auto& component_metrics = kcenon::common::get_value(comp_data);
86 for (const auto& metric : component_metrics.metrics) {
87 snapshot.metrics.push_back(metric);
88 }
89 } else {
90 snapshot.add_metric("component_error_" + component->get_component_name(), 1.0);
91 }
92 }
93
94 return kcenon::common::Result<ci::metrics_snapshot>::ok(std::move(snapshot));
95 }
std::unordered_map< std::string, double > aggregated_metrics_

References aggregated_metrics_, kcenon::common::get_value(), kcenon::common::is_ok(), monitored_components_, and mutex_.

Here is the call graph for this function:

◆ get_monitor()

std::shared_ptr< ci::IMonitor > aggregating_monitor::get_monitor ( )
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 142 of file monitoring_integration_example.cpp.

142 {
143 return shared_from_this();
144 }

◆ record_metric() [1/2]

kcenon::common::VoidResult aggregating_monitor::record_metric ( const std::string & name,
double value )
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 48 of file monitoring_integration_example.cpp.

51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 aggregated_metrics_[name] = value;
54 return kcenon::common::ok();
55 }
VoidResult ok()

References aggregated_metrics_, mutex_, and kcenon::common::ok().

Referenced by record_metric().

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

◆ record_metric() [2/2]

kcenon::common::VoidResult aggregating_monitor::record_metric ( const std::string & name,
double value,
const std::unordered_map< std::string, std::string > & tags )
inlineoverride

Definition at line 57 of file monitoring_integration_example.cpp.

61 {
62 std::string tagged_name = name;
63 for (const auto& [k, v] : tags) {
64 tagged_name += "." + k + ":" + v;
65 }
66 return record_metric(tagged_name, value);
67 }
kcenon::common::VoidResult record_metric(const std::string &name, double value) override

References record_metric().

Here is the call graph for this function:

◆ register_component()

void aggregating_monitor::register_component ( std::shared_ptr< ci::IMonitorable > component)
inline
Examples
monitoring_integration_example.cpp.

Definition at line 41 of file monitoring_integration_example.cpp.

41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 monitored_components_.push_back(component);
44 std::cout << "[AggregatingMonitor] Registered component: "
45 << component->get_component_name() << std::endl;
46 }

References monitored_components_, and mutex_.

◆ reset()

kcenon::common::VoidResult aggregating_monitor::reset ( )
inlineoverride
Examples
monitoring_integration_example.cpp.

Definition at line 135 of file monitoring_integration_example.cpp.

135 {
136 std::lock_guard<std::mutex> lock(mutex_);
137 aggregated_metrics_.clear();
138 return kcenon::common::ok();
139 }

References aggregated_metrics_, mutex_, and kcenon::common::ok().

Here is the call graph for this function:

Member Data Documentation

◆ aggregated_metrics_

std::unordered_map<std::string, double> aggregating_monitor::aggregated_metrics_
private

◆ monitored_components_

std::vector<std::shared_ptr<ci::IMonitorable> > aggregating_monitor::monitored_components_
private

◆ mutex_

std::mutex aggregating_monitor::mutex_
mutableprivate

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