Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
observability_bridge.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6#include <stdexcept>
7
9
11 std::shared_ptr<logger_interface> logger,
12 std::shared_ptr<monitoring_interface> monitor,
13 BackendType backend_type)
14 : logger_(std::move(logger))
15 , monitor_(std::move(monitor))
16 , backend_type_(backend_type) {
17 if (!logger_) {
18 throw std::invalid_argument("ObservabilityBridge requires non-null logger");
19 }
20 if (!monitor_) {
21 throw std::invalid_argument("ObservabilityBridge requires non-null monitor");
22 }
23}
24
30
32 if (initialized_.load()) {
33 return error_void(
35 "ObservabilityBridge already initialized",
36 "ObservabilityBridge::initialize");
37 }
38
39 if (!logger_) {
40 return error_void(
42 "Logger is null",
43 "ObservabilityBridge::initialize");
44 }
45
46 if (!monitor_) {
47 return error_void(
49 "Monitor is null",
50 "ObservabilityBridge::initialize");
51 }
52
53 // Check if bridge is enabled (default: true)
54 auto enabled_it = config.properties.find("enabled");
55 if (enabled_it != config.properties.end() && enabled_it->second == "false") {
56 return error_void(
58 "Bridge is disabled in configuration",
59 "ObservabilityBridge::initialize");
60 }
61
62 // Check if monitoring is enabled (default: true)
63 auto monitoring_enabled_it = config.properties.find("enable_monitoring");
64 monitoring_enabled_ = (monitoring_enabled_it == config.properties.end() ||
65 monitoring_enabled_it->second != "false");
66
67 // Initialize metrics
68 std::lock_guard<std::mutex> lock(metrics_mutex_);
70 cached_metrics_.last_activity = std::chrono::steady_clock::now();
71 cached_metrics_.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
72 cached_metrics_.custom_metrics["monitoring_enabled"] = monitoring_enabled_ ? 1.0 : 0.0;
73 cached_metrics_.custom_metrics["logger_available"] = logger_ ? 1.0 : 0.0;
74 cached_metrics_.custom_metrics["monitor_available"] = monitor_ ? 1.0 : 0.0;
75
76 initialized_.store(true);
77 return ok();
78}
79
81 if (!initialized_.load()) {
82 return ok(); // Idempotent: already shut down
83 }
84
85 // Flush logger before shutdown
86 if (logger_) {
87 try {
88 logger_->flush();
89 } catch (...) {
90 // Ignore flush errors during shutdown
91 }
92 }
93
94 // Update metrics to reflect shutdown state
95 {
96 std::lock_guard<std::mutex> lock(metrics_mutex_);
98 cached_metrics_.last_activity = std::chrono::steady_clock::now();
99 }
100
101 initialized_.store(false);
102 return ok();
103}
104
106 return initialized_.load() && logger_ && monitor_;
107}
108
110 std::lock_guard<std::mutex> lock(metrics_mutex_);
111
112 if (!initialized_.load() || !logger_ || !monitor_) {
113 BridgeMetrics metrics;
114 metrics.is_healthy = false;
116 return metrics;
117 }
118
119 // Update metrics with current state
121 metrics.is_healthy = (logger_ && monitor_);
122 metrics.last_activity = std::chrono::steady_clock::now();
123 metrics.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
124 metrics.custom_metrics["monitoring_enabled"] = monitoring_enabled_ ? 1.0 : 0.0;
125 metrics.custom_metrics["logger_available"] = logger_ ? 1.0 : 0.0;
126 metrics.custom_metrics["monitor_available"] = monitor_ ? 1.0 : 0.0;
127
128 // Update cached metrics for next call
129 cached_metrics_ = metrics;
130
131 return metrics;
132}
133
134std::shared_ptr<logger_interface> ObservabilityBridge::get_logger() const {
135 return logger_;
136}
137
138std::shared_ptr<monitoring_interface> ObservabilityBridge::get_monitor() const {
139 return monitor_;
140}
141
145
146#if KCENON_WITH_COMMON_SYSTEM
147std::shared_ptr<ObservabilityBridge> ObservabilityBridge::from_common_system(
148 std::shared_ptr<::kcenon::common::interfaces::ILogger> logger,
149 std::shared_ptr<::kcenon::common::interfaces::IMonitor> monitor) {
150 if (!logger) {
151 throw std::invalid_argument("ObservabilityBridge::from_common_system requires non-null logger");
152 }
153 if (!monitor) {
154 throw std::invalid_argument("ObservabilityBridge::from_common_system requires non-null monitor");
155 }
156
157 // Adapt common_system logger and monitor to network_system interfaces
158 auto adapted_logger = std::make_shared<common_system_logger_adapter>();
159
160 // For monitor, we use basic_monitoring since there's no direct adapter yet
161 // This is a known limitation - future work could create a proper adapter
162 auto adapted_monitor = std::make_shared<basic_monitoring>();
163
164 return std::make_shared<ObservabilityBridge>(
165 adapted_logger,
166 adapted_monitor,
168}
169#endif
170
171} // namespace kcenon::network::integration
BridgeMetrics get_metrics() const override
Get current metrics.
bool is_initialized() const override
Check if the bridge is initialized.
std::shared_ptr< logger_interface > get_logger() const
Get the logger interface.
std::shared_ptr< monitoring_interface > monitor_
VoidResult initialize(const BridgeConfig &config) override
Initialize the bridge with configuration.
VoidResult shutdown() override
Shutdown the bridge.
BackendType get_backend_type() const
Get the backend type.
ObservabilityBridge(std::shared_ptr< logger_interface > logger, std::shared_ptr< monitoring_interface > monitor, BackendType backend_type=BackendType::Standalone)
Construct bridge with logger and monitoring interfaces.
std::shared_ptr< monitoring_interface > get_monitor() const
Get the monitoring interface.
@ CommonSystem
Uses common_system's ILogger and IMonitor.
tracing_config config
Definition exporters.cpp:29
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
Observability (logger and monitoring) integration bridge for network_system.
Configuration for bridge initialization.
Metrics and health information for a bridge.
std::chrono::steady_clock::time_point last_activity
Timestamp of last activity or health check.
std::map< std::string, double > custom_metrics
Bridge-specific custom metrics.
bool is_healthy
Overall health status of the bridge.