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

Adapter that wraps performance_monitor to implement IMonitor interface. More...

#include <performance_monitor_adapter.h>

Inheritance diagram for kcenon::monitoring::performance_monitor_adapter:
Inheritance graph
Collaboration diagram for kcenon::monitoring::performance_monitor_adapter:
Collaboration graph

Classes

struct  validated_tag
 

Public Member Functions

 performance_monitor_adapter (std::shared_ptr< performance_monitor > monitor)
 Construct adapter with existing performance_monitor.
 
common::VoidResult record_metric (const std::string &name, double value) override
 Record a simple metric value.
 
common::VoidResult record_metric (const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags) override
 Record a metric with additional tags.
 
common::Result< common::interfaces::metrics_snapshot > get_metrics () override
 Get current metrics snapshot.
 
common::Result< common::interfaces::health_check_result > check_health () override
 Perform health check on the performance_monitor.
 
common::VoidResult reset () override
 Reset all metrics in the wrapped performance_monitor.
 
std::shared_ptr< performance_monitorget_wrapped_monitor () const
 Get the wrapped performance_monitor.
 

Static Public Member Functions

static common::Result< std::shared_ptr< performance_monitor_adapter > > create (std::shared_ptr< performance_monitor > monitor)
 Create an adapter with validated monitor pointer.
 

Private Member Functions

 performance_monitor_adapter (std::shared_ptr< performance_monitor > monitor, validated_tag)
 

Private Attributes

std::shared_ptr< performance_monitormonitor_
 

Detailed Description

Adapter that wraps performance_monitor to implement IMonitor interface.

This adapter follows the Adapter design pattern to eliminate multiple inheritance. It provides a clean separation between the monitoring system's internal interface (metrics_collector) and the common system's standard interface (IMonitor).

Thread Safety:
Thread-safe if the wrapped performance_monitor is thread-safe

Example usage:

auto monitor = std::make_shared<performance_monitor>("my_monitor");
auto adapter = std::make_shared<performance_monitor_adapter>(monitor);
// Use through IMonitor interface
adapter->record_metric("requests_count", 42.0);
auto snapshot = adapter->get_metrics();

Definition at line 45 of file performance_monitor_adapter.h.

Constructor & Destructor Documentation

◆ performance_monitor_adapter() [1/2]

kcenon::monitoring::performance_monitor_adapter::performance_monitor_adapter ( std::shared_ptr< performance_monitor > monitor)
inlineexplicit

Construct adapter with existing performance_monitor.

Parameters
monitorShared pointer to the performance_monitor to wrap
Exceptions
std::invalid_argumentif monitor is nullptr
Deprecated
Use create() for Result-based error handling

Definition at line 71 of file performance_monitor_adapter.h.

72 : monitor_(std::move(monitor)) {
73 if (!monitor_) {
74 throw std::invalid_argument("performance_monitor cannot be null");
75 }
76 }

References monitor_.

Referenced by create().

Here is the caller graph for this function:

◆ performance_monitor_adapter() [2/2]

kcenon::monitoring::performance_monitor_adapter::performance_monitor_adapter ( std::shared_ptr< performance_monitor > monitor,
validated_tag  )
inlineprivate

Definition at line 239 of file performance_monitor_adapter.h.

240 : monitor_(std::move(monitor)) {}

Member Function Documentation

◆ check_health()

common::Result< common::interfaces::health_check_result > kcenon::monitoring::performance_monitor_adapter::check_health ( )
inlineoverride

Perform health check on the performance_monitor.

Returns
Result containing health check result @override

Definition at line 188 of file performance_monitor_adapter.h.

188 {
189 try {
190 common::interfaces::health_check_result result;
191 result.timestamp = std::chrono::system_clock::now();
192
193 if (monitor_->is_enabled()) {
194 result.status = common::interfaces::health_status::healthy;
195 result.message = "Performance monitor is operational";
196 } else {
197 result.status = common::interfaces::health_status::degraded;
198 result.message = "Performance monitor is disabled";
199 }
200
201 return common::ok(std::move(result));
202 } catch (const std::exception& e) {
203 common::interfaces::health_check_result result;
204 result.status = common::interfaces::health_status::unhealthy;
205 result.message = std::string("Health check failed: ") + e.what();
206 result.timestamp = std::chrono::system_clock::now();
207 return common::ok(std::move(result));
208 }
209 }

References monitor_.

◆ create()

static common::Result< std::shared_ptr< performance_monitor_adapter > > kcenon::monitoring::performance_monitor_adapter::create ( std::shared_ptr< performance_monitor > monitor)
inlinestatic

Create an adapter with validated monitor pointer.

Parameters
monitorShared pointer to the performance_monitor to wrap
Returns
Result containing the adapter or error if monitor is nullptr

Definition at line 52 of file performance_monitor_adapter.h.

53 {
54 if (!monitor) {
55 return common::Result<std::shared_ptr<performance_monitor_adapter>>::err(
56 common::error_info{
58 "performance_monitor cannot be null",
59 "performance_monitor_adapter"});
60 }
61 return common::ok(std::shared_ptr<performance_monitor_adapter>(
62 new performance_monitor_adapter(std::move(monitor), validated_tag{})));
63 }
performance_monitor_adapter(std::shared_ptr< performance_monitor > monitor)
Construct adapter with existing performance_monitor.

References kcenon::monitoring::invalid_configuration, and performance_monitor_adapter().

Referenced by kcenon::monitoring::make_monitor_adapter(), TEST(), and TEST().

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

◆ get_metrics()

common::Result< common::interfaces::metrics_snapshot > kcenon::monitoring::performance_monitor_adapter::get_metrics ( )
inlineoverride

Get current metrics snapshot.

Returns
Result containing metrics snapshot compatible with common::interfaces @override

Definition at line 131 of file performance_monitor_adapter.h.

131 {
132 try {
133 // Get all recorded metrics from the performance_monitor
134 common::interfaces::metrics_snapshot snapshot;
135 snapshot.source_id = monitor_->get_name();
136 snapshot.capture_time = std::chrono::system_clock::now();
137
138 // Get performance metrics from the monitor's profiler
139 const auto& perf_metrics = monitor_->get_profiler().get_all_metrics();
140
141 // Convert each performance_metrics (operation timing) into multiple metric_value entries
142 for (const auto& perf : perf_metrics) {
143 // Add various duration metrics as gauges
144 snapshot.add_metric(perf.operation_name + "_min_ns",
145 static_cast<double>(perf.min_duration.count()));
146 snapshot.add_metric(perf.operation_name + "_max_ns",
147 static_cast<double>(perf.max_duration.count()));
148 snapshot.add_metric(perf.operation_name + "_mean_ns",
149 static_cast<double>(perf.mean_duration.count()));
150 snapshot.add_metric(perf.operation_name + "_median_ns",
151 static_cast<double>(perf.median_duration.count()));
152 snapshot.add_metric(perf.operation_name + "_p95_ns",
153 static_cast<double>(perf.p95_duration.count()));
154 snapshot.add_metric(perf.operation_name + "_p99_ns",
155 static_cast<double>(perf.p99_duration.count()));
156
157 // Add count metrics as counters (conceptually)
158 snapshot.add_metric(perf.operation_name + "_call_count",
159 static_cast<double>(perf.call_count));
160 snapshot.add_metric(perf.operation_name + "_error_count",
161 static_cast<double>(perf.error_count));
162 }
163
164 // Add tagged metrics (counters, gauges, histograms with tags)
165 const auto& tagged_metrics = monitor_->get_all_tagged_metrics();
166 for (const auto& metric : tagged_metrics) {
167 // Create metric_value with tags directly
168 common::interfaces::metric_value mv(metric.name, metric.value);
169 mv.tags = std::unordered_map<std::string, std::string>(
170 metric.tags.begin(), metric.tags.end());
171 mv.timestamp = metric.timestamp;
172 snapshot.metrics.push_back(std::move(mv));
173 }
174
175 return common::ok(std::move(snapshot));
176 } catch (const std::exception& e) {
177 return common::Result<common::interfaces::metrics_snapshot>(
178 common::error_info{3, std::string("Exception in get_metrics: ") + e.what(), "performance_monitor_adapter"}
179 );
180 }
181 }

References monitor_, kcenon::monitoring::metric::name, kcenon::monitoring::metric::tags, kcenon::monitoring::metric::timestamp, and kcenon::monitoring::metric::value.

◆ get_wrapped_monitor()

std::shared_ptr< performance_monitor > kcenon::monitoring::performance_monitor_adapter::get_wrapped_monitor ( ) const
inline

Get the wrapped performance_monitor.

Returns
Shared pointer to the wrapped monitor

Definition at line 231 of file performance_monitor_adapter.h.

231 {
232 return monitor_;
233 }

References monitor_.

◆ record_metric() [1/2]

common::VoidResult kcenon::monitoring::performance_monitor_adapter::record_metric ( const std::string & name,
double value )
inlineoverride

Record a simple metric value.

Parameters
nameMetric name
valueMetric value
Returns
VoidResult indicating success or error @override
Note
performance_monitor is designed for operation timing, not arbitrary metrics. This method is a no-op for compatibility with IMonitor interface. Use start_operation() / scoped_timer for actual performance monitoring.

Definition at line 89 of file performance_monitor_adapter.h.

89 {
90 // performance_monitor is specifically designed for timing operations,
91 // not recording arbitrary metric values. For IMonitor compatibility,
92 // we accept the call but don't store it.
93 (void)name; // Suppress unused parameter warning
94 (void)value;
95 return common::ok();
96 }

◆ record_metric() [2/2]

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

Record a metric with additional tags.

Parameters
nameMetric name
valueMetric value
tagsMetadata tags for the metric
Returns
VoidResult indicating success or error @override

Records the metric using the underlying performance_monitor's tag-aware counter recording. Tags are preserved and passed through to enable multi-dimensional metric analysis.

Definition at line 110 of file performance_monitor_adapter.h.

113 {
114 // Convert to tag_map and record as counter with tags
115 tag_map converted_tags(tags.begin(), tags.end());
116 auto result = monitor_->record_counter(name, value, converted_tags);
117 if (result.is_err()) {
118 const auto& err = result.error();
119 return common::VoidResult(
120 common::error_info{static_cast<int>(err.code), err.message, "performance_monitor_adapter"}
121 );
122 }
123 return common::ok();
124 }
std::unordered_map< std::string, std::string > tag_map
Type alias for metric tags/labels.

References monitor_.

◆ reset()

common::VoidResult kcenon::monitoring::performance_monitor_adapter::reset ( )
inlineoverride

Reset all metrics in the wrapped performance_monitor.

Returns
VoidResult indicating success or error @override

Definition at line 216 of file performance_monitor_adapter.h.

216 {
217 try {
218 monitor_->reset();
219 return common::ok();
220 } catch (const std::exception& e) {
221 return common::VoidResult(
222 common::error_info{4, std::string("Failed to reset monitor: ") + e.what(), "performance_monitor_adapter"}
223 );
224 }
225 }

References monitor_.

Member Data Documentation

◆ monitor_

std::shared_ptr<performance_monitor> kcenon::monitoring::performance_monitor_adapter::monitor_
private

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