Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
monitoring_interface.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13#pragma once
14
15#include <algorithm>
16#include <array>
17#include <chrono>
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <string>
22#include <string_view>
23#include <unordered_map>
24#include <utility>
25#include <vector>
26#include "../patterns/result.h"
28
29namespace kcenon::common {
30namespace interfaces {
31
36enum class metric_type {
37 gauge, // Instant value that can go up or down
38 counter, // Monotonic increasing value
39 histogram, // Distribution of values across buckets
40 summary // Statistical summary (min, max, mean, percentiles)
41};
42
43} // namespace interfaces
44
48template<>
49struct enum_traits<interfaces::metric_type> {
50 static constexpr auto values = std::array{
51 std::pair{interfaces::metric_type::gauge, std::string_view{"GAUGE"}},
52 std::pair{interfaces::metric_type::counter, std::string_view{"COUNTER"}},
53 std::pair{interfaces::metric_type::histogram, std::string_view{"HISTOGRAM"}},
54 std::pair{interfaces::metric_type::summary, std::string_view{"SUMMARY"}},
55 };
56 static constexpr std::string_view module_name = "monitoring_interface";
57};
58
59namespace interfaces {
60
64inline std::string to_string(metric_type type) {
65 return enum_to_string(type);
66}
67
71inline Result<metric_type> metric_type_from_string(const std::string& str) {
73}
74
80 std::string name;
81 double value;
83 std::chrono::system_clock::time_point timestamp;
84 std::unordered_map<std::string, std::string> tags;
85
86 metric_value(const std::string& n = "", double v = 0.0, metric_type t = metric_type::gauge)
87 : name(n)
88 , value(v)
89 , type(t)
90 , timestamp(std::chrono::system_clock::now()) {}
91};
92
98 std::vector<metric_value> metrics;
99 std::chrono::system_clock::time_point capture_time;
100 std::string source_id;
101
103 : capture_time(std::chrono::system_clock::now()) {}
104
105 void add_metric(const std::string& name, double value) {
106 metrics.emplace_back(name, value);
107 }
108};
109
121
122 std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
123
124 // Pool identification for multi-pool scenarios
125 std::string pool_name;
126 std::uint32_t pool_instance_id{0};
127
129
130 explicit thread_pool_metrics(const std::string& name, std::uint32_t instance_id = 0)
131 : pool_name(name), pool_instance_id(instance_id) {}
132
136 std::vector<metric_value> to_metrics() const {
137 return {
144 };
145 }
146};
147
157
158 std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
159 std::size_t worker_id{0};
160
161 worker_metrics() = default;
162
163 explicit worker_metrics(std::size_t id) : worker_id(id) {}
164
168 std::vector<metric_value> to_metrics() const {
169 return {
174 };
175 }
176};
177
187
188 std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
189
193 std::vector<metric_value> to_metrics() const {
194 return {
199 };
200 }
201};
202
207enum class health_status {
208 healthy = 0,
209 degraded = 1,
210 unhealthy = 2,
211 unknown = 3
212};
213
214} // namespace interfaces
215
219template<>
220struct enum_traits<interfaces::health_status> {
221 static constexpr auto values = std::array{
222 std::pair{interfaces::health_status::healthy, std::string_view{"HEALTHY"}},
223 std::pair{interfaces::health_status::degraded, std::string_view{"DEGRADED"}},
224 std::pair{interfaces::health_status::unhealthy, std::string_view{"UNHEALTHY"}},
225 std::pair{interfaces::health_status::unknown, std::string_view{"UNKNOWN"}},
226 };
227 static constexpr std::string_view module_name = "monitoring_interface";
228};
229
230namespace interfaces {
231
238 std::string message;
239 std::chrono::system_clock::time_point timestamp;
240 std::chrono::milliseconds check_duration{0};
241 std::unordered_map<std::string, std::string> metadata;
242
244 : timestamp(std::chrono::system_clock::now()) {}
245
246 bool is_healthy() const {
248 }
249
250 bool is_operational() const {
251 return status == health_status::healthy ||
253 }
254};
255
263class IMonitor {
264public:
265 virtual ~IMonitor() = default;
266
273 virtual VoidResult record_metric(const std::string& name, double value) = 0;
274
283 const std::string& name,
284 double value,
285 const std::unordered_map<std::string, std::string>& tags) = 0;
286
292
298
303 virtual VoidResult reset() = 0;
304};
305
314public:
315 virtual ~IMonitorable() = default;
316
322
328
333 virtual std::string get_component_name() const = 0;
334};
335
339using MonitorFactory = std::function<std::shared_ptr<IMonitor>()>;
340
346public:
347 virtual ~IMonitorProvider() = default;
348
353 virtual std::shared_ptr<IMonitor> get_monitor() = 0;
354
360 virtual std::shared_ptr<IMonitor> create_monitor(const std::string& name) = 0;
361};
362
366inline std::string to_string(health_status status) {
367 return enum_to_string(status);
368}
369
370} // namespace interfaces
371} // namespace kcenon::common
Result type for error handling with member function support.
Definition core.cppm:165
Interface for modules that provide monitor implementations.
virtual std::shared_ptr< IMonitor > create_monitor(const std::string &name)=0
Create a new monitor with specific name.
virtual std::shared_ptr< IMonitor > get_monitor()=0
Get the default monitor instance.
Standard interface for monitoring implementations.
virtual Result< health_check_result > check_health()=0
Perform health check.
virtual VoidResult record_metric(const std::string &name, double value)=0
Record a metric value.
virtual Result< metrics_snapshot > get_metrics()=0
Get current metrics snapshot.
virtual VoidResult reset()=0
Reset all metrics.
virtual VoidResult record_metric(const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags)=0
Record a metric with tags.
Interface for objects that can be monitored.
virtual Result< health_check_result > health_check()=0
Check health status.
virtual std::string get_component_name() const =0
Get component name for monitoring.
virtual Result< metrics_snapshot > get_monitoring_data()=0
Get monitoring data.
Generic enum serialization utilities using C++20 concepts.
health_status
Standard health status levels.
metric_type
Types of metrics supported by the monitoring system.
std::function< std::shared_ptr< IMonitor >()> MonitorFactory
Factory function type for creating monitor instances.
Result< metric_type > metric_type_from_string(const std::string &str)
Convert string to metric type.
std::string to_string(log_level level)
Convert log level to string.
Core interfaces.
Definition adapter.h:21
Result< Enum > enum_from_string(std::string_view str)
Convert a string to its enum value (case-insensitive)
std::string enum_to_string(Enum value)
Convert an enum value to its string representation.
Umbrella header for Result<T> type and related utilities.
Primary template for enum traits (must be specialized for each enum)
std::unordered_map< std::string, std::string > metadata
std::chrono::system_clock::time_point timestamp
Standard metric value structure with type information.
metric_value(const std::string &n="", double v=0.0, metric_type t=metric_type::gauge)
std::chrono::system_clock::time_point timestamp
std::unordered_map< std::string, std::string > tags
Complete snapshot of metrics at a point in time.
void add_metric(const std::string &name, double value)
std::chrono::system_clock::time_point capture_time
Specialized metrics for system-level monitoring.
std::vector< metric_value > to_metrics() const
Convert to vector of metric_value.
std::chrono::system_clock::time_point timestamp
Specialized metrics for thread pool monitoring.
std::vector< metric_value > to_metrics() const
Convert to vector of metric_value.
thread_pool_metrics(const std::string &name, std::uint32_t instance_id=0)
std::chrono::system_clock::time_point timestamp
Specialized metrics for worker thread monitoring.
std::chrono::system_clock::time_point timestamp
std::vector< metric_value > to_metrics() const
Convert to vector of metric_value.