Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
monitoring_integration.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
7
8#include <atomic>
9#include <mutex>
10#include <sstream>
11#include <unordered_map>
12
14{
15
16 //===========================================================================
17 // basic_monitoring implementation
18 //===========================================================================
19
21 {
22 public:
23 explicit impl(bool enable_logging) : logging_enabled_(enable_logging) {}
24
25 void report_counter(const std::string& name, double value,
26 const std::map<std::string, std::string>& labels)
27 {
29 return;
30
31 std::ostringstream oss;
32 oss << "[MONITORING] Counter: " << name << " = " << value;
33 if (!labels.empty())
34 {
35 oss << " {";
36 bool first = true;
37 for (const auto& [key, val] : labels)
38 {
39 if (!first)
40 oss << ", ";
41 oss << key << "=" << val;
42 first = false;
43 }
44 oss << "}";
45 }
46 NETWORK_LOG_DEBUG(oss.str());
47 }
48
49 void report_gauge(const std::string& name, double value,
50 const std::map<std::string, std::string>& labels)
51 {
53 return;
54
55 std::ostringstream oss;
56 oss << "[MONITORING] Gauge: " << name << " = " << value;
57 if (!labels.empty())
58 {
59 oss << " {";
60 bool first = true;
61 for (const auto& [key, val] : labels)
62 {
63 if (!first)
64 oss << ", ";
65 oss << key << "=" << val;
66 first = false;
67 }
68 oss << "}";
69 }
70 NETWORK_LOG_DEBUG(oss.str());
71 }
72
73 void report_histogram(const std::string& name, double value,
74 const std::map<std::string, std::string>& labels)
75 {
77 return;
78
79 std::ostringstream oss;
80 oss << "[MONITORING] Histogram: " << name << " = " << value;
81 if (!labels.empty())
82 {
83 oss << " {";
84 bool first = true;
85 for (const auto& [key, val] : labels)
86 {
87 if (!first)
88 oss << ", ";
89 oss << key << "=" << val;
90 first = false;
91 }
92 oss << "}";
93 }
94 NETWORK_LOG_DEBUG(oss.str());
95 }
96
97 void report_health(const std::string& connection_id, bool is_alive,
98 double response_time_ms, size_t missed_heartbeats,
99 double packet_loss_rate)
100 {
101 if (!logging_enabled_)
102 return;
103
104 std::ostringstream oss;
105 oss << "[MONITORING] Health: " << connection_id << " - alive=" << is_alive
106 << ", response_time=" << response_time_ms << "ms"
107 << ", missed_heartbeats=" << missed_heartbeats
108 << ", packet_loss=" << (packet_loss_rate * 100.0) << "%";
109 NETWORK_LOG_DEBUG(oss.str());
110 }
111
113
114 bool is_logging_enabled() const { return logging_enabled_; }
115
116 private:
118 };
119
121 : pimpl_(std::make_unique<impl>(enable_logging))
122 {
123 }
124
126
127 void basic_monitoring::report_counter(const std::string& name, double value,
128 const std::map<std::string, std::string>& labels)
129 {
130 pimpl_->report_counter(name, value, labels);
131 }
132
133 void basic_monitoring::report_gauge(const std::string& name, double value,
134 const std::map<std::string, std::string>& labels)
135 {
136 pimpl_->report_gauge(name, value, labels);
137 }
138
139 void basic_monitoring::report_histogram(const std::string& name, double value,
140 const std::map<std::string, std::string>& labels)
141 {
142 pimpl_->report_histogram(name, value, labels);
143 }
144
145 void basic_monitoring::report_health(const std::string& connection_id, bool is_alive,
146 double response_time_ms, size_t missed_heartbeats,
147 double packet_loss_rate)
148 {
149 pimpl_->report_health(connection_id, is_alive, response_time_ms, missed_heartbeats,
150 packet_loss_rate);
151 }
152
157
159
160 // Note: monitoring_system_adapter has been removed in favor of EventBus-based
161 // metric publishing. See issue #342 for details.
162
163 //===========================================================================
164 // monitoring_integration_manager implementation
165 //===========================================================================
166
168 {
169 public:
170 impl() = default;
171
172 void set_monitoring(std::shared_ptr<monitoring_interface> monitoring)
173 {
174 std::lock_guard<std::mutex> lock(mutex_);
175 monitoring_ = monitoring;
176 }
177
178 std::shared_ptr<monitoring_interface> get_monitoring()
179 {
180 std::lock_guard<std::mutex> lock(mutex_);
181 if (!monitoring_)
182 {
183 // Create default basic monitoring if none set
184 monitoring_ = std::make_shared<basic_monitoring>(true);
185 }
186 return monitoring_;
187 }
188
189 private:
190 std::mutex mutex_;
191 std::shared_ptr<monitoring_interface> monitoring_;
192 };
193
195 : pimpl_(std::make_unique<impl>())
196 {
197 }
198
200
206
208 std::shared_ptr<monitoring_interface> monitoring)
209 {
210 pimpl_->set_monitoring(monitoring);
211 }
212
213 std::shared_ptr<monitoring_interface> monitoring_integration_manager::get_monitoring()
214 {
215 return pimpl_->get_monitoring();
216 }
217
219 const std::string& name, double value, const std::map<std::string, std::string>& labels)
220 {
221 get_monitoring()->report_counter(name, value, labels);
222 }
223
225 const std::string& name, double value, const std::map<std::string, std::string>& labels)
226 {
227 get_monitoring()->report_gauge(name, value, labels);
228 }
229
231 const std::string& name, double value, const std::map<std::string, std::string>& labels)
232 {
233 get_monitoring()->report_histogram(name, value, labels);
234 }
235
236 void monitoring_integration_manager::report_health(const std::string& connection_id,
237 bool is_alive, double response_time_ms,
238 size_t missed_heartbeats,
239 double packet_loss_rate)
240 {
241 get_monitoring()->report_health(connection_id, is_alive, response_time_ms,
242 missed_heartbeats, packet_loss_rate);
243 }
244
245} // namespace kcenon::network::integration
void report_gauge(const std::string &name, double value, const std::map< std::string, std::string > &labels)
void report_health(const std::string &connection_id, bool is_alive, double response_time_ms, size_t missed_heartbeats, double packet_loss_rate)
void report_histogram(const std::string &name, double value, const std::map< std::string, std::string > &labels)
void report_counter(const std::string &name, double value, const std::map< std::string, std::string > &labels)
void report_health(const std::string &connection_id, bool is_alive, double response_time_ms, size_t missed_heartbeats, double packet_loss_rate) override
Report connection health metrics.
basic_monitoring(bool enable_logging=true)
Constructor.
void report_histogram(const std::string &name, double value, const std::map< std::string, std::string > &labels={}) override
Report a histogram metric.
void report_counter(const std::string &name, double value, const std::map< std::string, std::string > &labels={}) override
Report a counter metric.
void set_logging_enabled(bool enabled)
Enable or disable logging.
void report_gauge(const std::string &name, double value, const std::map< std::string, std::string > &labels={}) override
Report a gauge metric.
bool is_logging_enabled() const
Check if logging is enabled.
void set_monitoring(std::shared_ptr< monitoring_interface > monitoring)
std::shared_ptr< monitoring_interface > get_monitoring()
Get the current monitoring implementation.
void report_health(const std::string &connection_id, bool is_alive, double response_time_ms, size_t missed_heartbeats, double packet_loss_rate)
Report connection health metrics.
static monitoring_integration_manager & instance()
Get the singleton instance.
void report_histogram(const std::string &name, double value, const std::map< std::string, std::string > &labels={})
Report a histogram metric.
void set_monitoring(std::shared_ptr< monitoring_interface > monitoring)
Set the monitoring implementation.
void report_counter(const std::string &name, double value, const std::map< std::string, std::string > &labels={})
Report a counter metric.
void report_gauge(const std::string &name, double value, const std::map< std::string, std::string > &labels={})
Report a gauge metric.
std::atomic< bool > enabled
Definition exporters.cpp:28
Logger system integration interface for network_system.
#define NETWORK_LOG_DEBUG(msg)
Monitoring system integration interface for network_system.