Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_metrics.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11
12#include <mutex>
13
14#if KCENON_WITH_COMMON_SYSTEM
16#include <kcenon/common/patterns/event_bus.h>
17#endif
18
20
21namespace {
22
23// Global histogram instances for metrics tracking
24struct histogram_storage
25{
29 std::mutex mutex;
30
31 static auto instance() -> histogram_storage&
32 {
33 static histogram_storage storage;
34 return storage;
35 }
36};
37
38#if KCENON_WITH_COMMON_SYSTEM
50void publish_metric(const std::string& name, double value,
52 const std::map<std::string, std::string>& labels = {})
53{
54 // Guard against static destruction - EventBus may already be destroyed
56 return;
57 }
58 auto& bus = kcenon::common::get_event_bus();
59 bus.publish(events::network_metric_event{name, value, type, labels});
60}
61#endif // KCENON_WITH_COMMON_SYSTEM
62
63} // anonymous namespace
64
66{
67#if KCENON_WITH_COMMON_SYSTEM
68 // Publish via EventBus for external consumers (e.g., monitoring_system)
69 publish_metric(metric_names::CONNECTIONS_TOTAL, 1.0,
71 {{"event", "accepted"}});
72#endif
73
74 // Report to local monitoring integration
77}
78
79void metric_reporter::report_connection_failed(const std::string& reason)
80{
81#if KCENON_WITH_COMMON_SYSTEM
82 publish_metric(metric_names::CONNECTIONS_FAILED, 1.0,
84 {{"reason", reason}});
85#endif
86
88 metric_names::CONNECTIONS_FAILED, 1.0, {{"reason", reason}});
89}
90
92{
93 auto byte_value = static_cast<double>(bytes);
94
95#if KCENON_WITH_COMMON_SYSTEM
96 publish_metric(metric_names::BYTES_SENT, byte_value,
98 publish_metric(metric_names::PACKETS_SENT, 1.0,
100#endif
101
103 metric_names::BYTES_SENT, byte_value);
106}
107
109{
110 auto byte_value = static_cast<double>(bytes);
111
112#if KCENON_WITH_COMMON_SYSTEM
113 publish_metric(metric_names::BYTES_RECEIVED, byte_value,
115 publish_metric(metric_names::PACKETS_RECEIVED, 1.0,
117#endif
118
120 metric_names::BYTES_RECEIVED, byte_value);
123}
124
135
136void metric_reporter::report_error(const std::string& error_type)
137{
138#if KCENON_WITH_COMMON_SYSTEM
139 publish_metric(metric_names::ERRORS_TOTAL, 1.0,
141 {{"error_type", error_type}});
142#endif
143
145 metric_names::ERRORS_TOTAL, 1.0, {{"error_type", error_type}});
146}
147
158
160{
161 auto count_value = static_cast<double>(count);
162
163#if KCENON_WITH_COMMON_SYSTEM
164 publish_metric(metric_names::CONNECTIONS_ACTIVE, count_value,
166#endif
167
170}
171
182
183// ============================================================================
184// Histogram-based metrics implementation
185// ============================================================================
186
188{
189 // Record to sliding histogram for percentile calculations
190 auto& storage = histogram_storage::instance();
191 storage.latency.record(ms);
192
193 // Also report to traditional monitoring integration
194 report_latency(ms);
195}
196
198{
199 auto& storage = histogram_storage::instance();
200 storage.connection_time.record(ms);
201
202#if KCENON_WITH_COMMON_SYSTEM
205#endif
206
209}
210
212{
213 auto& storage = histogram_storage::instance();
214 storage.request_duration.record(ms);
215
216#if KCENON_WITH_COMMON_SYSTEM
219#endif
220
223}
224
226{
227 auto& storage = histogram_storage::instance();
228 return storage.latency.p50();
229}
230
232{
233 auto& storage = histogram_storage::instance();
234 return storage.latency.p95();
235}
236
238{
239 auto& storage = histogram_storage::instance();
240 return storage.latency.p99();
241}
242
244{
245 auto& storage = histogram_storage::instance();
246 return storage.connection_time.p99();
247}
248
250{
251 auto& storage = histogram_storage::instance();
252 return storage.request_duration.p99();
253}
254
255auto metric_reporter::get_all_histograms() -> std::map<std::string, histogram_snapshot>
256{
257 auto& storage = histogram_storage::instance();
258 std::map<std::string, histogram_snapshot> result;
259
261 storage.latency.snapshot({{"metric", "latency"}});
263 storage.connection_time.snapshot({{"metric", "connection_time"}});
265 storage.request_duration.snapshot({{"metric", "request_duration"}});
266
267 return result;
268}
269
271{
272 auto& storage = histogram_storage::instance();
273 storage.latency.reset();
274 storage.connection_time.reset();
275 storage.request_duration.reset();
276}
277
278} // namespace kcenon::network::metrics
static bool is_logging_safe()
Check if logging is safe (not in static destruction)
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 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.
static void report_error(const std::string &error_type)
Report a network error.
static auto get_connection_time_p99() -> double
Get connection time 99th percentile.
static void record_connection_time(double ms)
Record connection time to internal histogram.
static void report_session_duration(double ms)
Report session duration.
static void report_latency(double ms)
Report network latency.
static auto get_latency_p99() -> double
Get latency 99th percentile.
static void record_latency(double ms)
Record latency to internal histogram.
static void report_connection_failed(const std::string &reason)
Report a failed connection attempt.
static auto get_latency_p95() -> double
Get latency 95th percentile.
static auto get_all_histograms() -> std::map< std::string, histogram_snapshot >
Get all histogram snapshots for export.
static void reset_histograms()
Reset all histogram data.
static void report_connection_accepted()
Report a new connection accepted.
static void record_request_duration(double ms)
Record request duration to internal histogram.
static void report_timeout()
Report a timeout.
static void report_active_connections(size_t count)
Report active connections count.
static auto get_request_duration_p99() -> double
Get request duration 99th percentile.
static void report_bytes_received(size_t bytes)
Report bytes received.
static void report_bytes_sent(size_t bytes)
Report bytes sent.
static auto get_latency_p50() -> double
Get latency 50th percentile (median)
Feature flags for network_system.
Histogram metric implementation for latency distribution tracking.
Logger system integration interface for network_system.
Monitoring system integration interface for network_system.
network_metric_type
Types of network metrics.
constexpr const char * SESSION_DURATION_MS
constexpr const char * CONNECTIONS_ACTIVE
constexpr const char * CONNECTION_TIME_HISTOGRAM
constexpr const char * CONNECTIONS_FAILED
constexpr const char * REQUEST_DURATION_HISTOGRAM
Network metric events for EventBus-based metric publishing.
std::mutex mutex
sliding_histogram request_duration
sliding_histogram connection_time
sliding_histogram latency
Network system metrics definitions and reporting utilities.
Sliding window histogram for time-based latency tracking.
static auto default_config() -> sliding_histogram_config
Create default configuration (60 second window, 6 buckets = 10s each)