15#include <kcenon/common/interfaces/logger_interface.h>
16#include <kcenon/common/interfaces/monitoring_interface.h>
24#include <unordered_map>
27namespace ci = kcenon::common::interfaces;
33 public ci::IMonitorProvider,
34 public std::enable_shared_from_this<aggregating_monitor> {
42 std::lock_guard<std::mutex> lock(
mutex_);
44 std::cout <<
"[AggregatingMonitor] Registered component: "
45 << component->get_component_name() << std::endl;
49 const std::string& name,
50 double value)
override
52 std::lock_guard<std::mutex> lock(
mutex_);
58 const std::string& name,
60 const std::unordered_map<std::string, std::string>& tags)
override
62 std::string tagged_name = name;
63 for (
const auto& [k, v] : tags) {
64 tagged_name +=
"." + k +
":" + v;
70 std::lock_guard<std::mutex> lock(
mutex_);
72 ci::metrics_snapshot snapshot;
73 snapshot.source_id =
"aggregating_monitor";
74 snapshot.capture_time = std::chrono::system_clock::now();
78 snapshot.add_metric(name, value);
83 auto comp_data = component->get_monitoring_data();
86 for (
const auto& metric : component_metrics.metrics) {
87 snapshot.metrics.push_back(metric);
90 snapshot.add_metric(
"component_error_" + component->get_component_name(), 1.0);
98 std::lock_guard<std::mutex> lock(
mutex_);
100 ci::health_check_result
result;
101 result.timestamp = std::chrono::system_clock::now();
102 result.status = ci::health_status::healthy;
103 result.message =
"Aggregating monitor operational";
107 auto comp_health = component->health_check();
108 const auto component_name = component->get_component_name();
112 result.metadata[
"component_status:" + component_name] = ci::to_string(component_result.status);
114 if (component_result.status == ci::health_status::unhealthy) {
115 result.status = ci::health_status::unhealthy;
116 result.message =
"One or more components unhealthy";
117 }
else if (component_result.status == ci::health_status::degraded &&
118 result.status == ci::health_status::healthy) {
119 result.status = ci::health_status::degraded;
120 result.message =
"One or more components degraded";
124 result.metadata[
"component_status:" + component_name] =
"error:" + error.message;
125 if (
result.status == ci::health_status::healthy) {
126 result.status = ci::health_status::degraded;
127 result.message =
"Component health check failed";
136 std::lock_guard<std::mutex> lock(
mutex_);
143 return shared_from_this();
148 return shared_from_this();
152 std::lock_guard<std::mutex> lock(
mutex_);
161 std::cout <<
"\n--- Metrics Snapshot ---" << std::endl;
162 std::cout <<
"Source: " << snapshot.source_id << std::endl;
163 std::cout <<
"Captured at: "
164 << std::chrono::system_clock::to_time_t(snapshot.capture_time)
166 std::cout <<
"Metrics:" << std::endl;
168 for (
const auto& metric : snapshot.metrics) {
169 std::cout <<
" " << std::setw(30) << std::left << metric.name
170 <<
": " << metric.value << std::endl;
178 std::cout <<
"\n--- Health Check ---" << std::endl;
179 std::cout <<
"Status: " << ci::to_string(health.status) << std::endl;
180 std::cout <<
"Message: " << health.message << std::endl;
182 if (!health.metadata.empty()) {
183 std::cout <<
"Component Status:" << std::endl;
184 for (
const auto& [key, value] : health.metadata) {
185 if (key.rfind(
"component_status:", 0) == 0) {
186 std::cout <<
" - " << key.substr(std::string(
"component_status:").
size())
187 <<
": " << value << std::endl;
192 std::cout <<
"Check duration: "
193 << health.check_duration.count() <<
"ms" << std::endl;
200 std::cout <<
"\n=== Example 1: Basic Monitor Integration ===" << std::endl;
202 auto monitor = std::make_shared<aggregating_monitor>();
209 if (!logger_result) {
210 std::cerr <<
"Failed to create logger" << std::endl;
214 auto logger_instance = std::shared_ptr<logger>(std::move(logger_result.value()));
220 std::cout <<
"[Note: logger IMonitorable integration pending Phase 2.2]" << std::endl;
223 for (
int i = 0; i < 5; ++i) {
224 logger_instance->log(ci::log_level::info,
225 "Log message " + std::to_string(i + 1));
229 auto metrics = monitor->get_metrics();
235 auto health = monitor->check_health();
245 std::cout <<
"\n=== Example 2: Multiple Loggers, Single Monitor ===" << std::endl;
247 auto monitor = std::make_shared<aggregating_monitor>();
260 if (!logger1_result || !logger2_result) {
261 std::cerr <<
"Failed to create loggers" << std::endl;
265 auto logger1 = std::shared_ptr<logger>(std::move(logger1_result.value()));
266 auto logger2 = std::shared_ptr<logger>(std::move(logger2_result.value()));
272 std::cout <<
"[Note: logger IMonitorable integration pending Phase 2.2]" << std::endl;
275 logger1->log(ci::log_level::info, std::string(
"Message from logger 1"));
276 logger2->log(ci::log_level::warning, std::string(
"Message from logger 2"));
277 logger1->log(ci::log_level::error, std::string(
"Error from logger 1"));
279 std::cout <<
"\nMonitor tracks " << monitor->get_component_count()
280 <<
" components" << std::endl;
283 auto metrics = monitor->get_metrics();
285 std::cout <<
"Combined metrics from all loggers:" << std::endl;
294 std::cout <<
"\n=== Example 3: IMonitorable Interface ===" << std::endl;
296 auto monitor = std::make_shared<aggregating_monitor>();
303 if (!logger_result)
return;
305 auto logger_instance = std::shared_ptr<logger>(std::move(logger_result.value()));
308 if (
auto monitorable = std::dynamic_pointer_cast<ci::IMonitorable>(logger_instance)) {
309 std::cout <<
"Logger component name: "
310 << monitorable->get_component_name() << std::endl;
313 logger_instance->log(ci::log_level::info, std::string(
"Test message 1"));
314 logger_instance->log(ci::log_level::info, std::string(
"Test message 2"));
317 auto data = monitorable->get_monitoring_data();
319 std::cout <<
"\nDirect monitoring data from logger:" << std::endl;
324 auto health = monitorable->health_check();
326 std::cout <<
"\nDirect health check from logger:" << std::endl;
336 std::cout <<
"\n=== Example 4: Monitoring System Integration Simulation ===" << std::endl;
337 std::cout <<
"Note: This demonstrates how logger_system and monitoring_system" << std::endl;
338 std::cout <<
" interact via interfaces without circular dependencies" << std::endl;
341 std::shared_ptr<ci::IMonitor> monitor = std::make_shared<aggregating_monitor>();
349 if (!logger_result)
return;
351 auto logger_instance = std::shared_ptr<logger>(std::move(logger_result.value()));
353 std::cout <<
"\nPhase 1: Logger operates and reports to monitor" << std::endl;
356 for (
int i = 0; i < 10; ++i) {
357 logger_instance->log(ci::log_level::info,
358 "Application event " + std::to_string(i + 1));
359 std::this_thread::sleep_for(std::chrono::milliseconds(100));
363 std::this_thread::sleep_for(std::chrono::milliseconds(500));
365 std::cout <<
"\nPhase 2: Monitoring system queries metrics" << std::endl;
368 auto metrics = monitor->get_metrics();
370 std::cout <<
"Monitoring system received metrics:" << std::endl;
375 if (
auto monitorable = std::dynamic_pointer_cast<ci::IMonitorable>(logger_instance)) {
376 auto health = monitorable->health_check();
378 std::cout <<
"\nLogger health status:" << std::endl;
383 std::cout <<
"\nā Integration successful without circular dependencies" << std::endl;
387 std::cout <<
"============================================================" << std::endl;
388 std::cout <<
"Logger-Monitor Integration Examples (Phase 4)" << std::endl;
389 std::cout <<
"Demonstrating loose coupling via common_system interfaces" << std::endl;
390 std::cout <<
"============================================================" << std::endl;
398 std::cout <<
"\n============================================================" << std::endl;
399 std::cout <<
"All integration examples completed successfully!" << std::endl;
400 std::cout <<
"============================================================" << std::endl;
402 }
catch (
const std::exception& e) {
403 std::cerr <<
"Error: " << e.what() << std::endl;
Aggregating monitor that collects metrics from multiple sources.
kcenon::common::VoidResult reset() override
std::shared_ptr< ci::IMonitor > create_monitor(const std::string &name) override
size_t get_component_count() const
kcenon::common::Result< ci::health_check_result > check_health() override
kcenon::common::VoidResult record_metric(const std::string &name, double value) override
std::vector< std::shared_ptr< ci::IMonitorable > > monitored_components_
std::shared_ptr< ci::IMonitor > get_monitor() override
kcenon::common::VoidResult record_metric(const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags) override
void register_component(std::shared_ptr< ci::IMonitorable > component)
std::unordered_map< std::string, double > aggregated_metrics_
kcenon::common::Result< ci::metrics_snapshot > get_metrics() override
Builder pattern for logger construction with validation.
logger_builder & with_monitoring(std::shared_ptr< common::interfaces::IMonitor > monitor)
Set monitoring interface (Phase 2.2.4)
logger_builder & with_async(bool async=true)
result< std::unique_ptr< logger > > build()
Builder pattern implementation for flexible logger configuration kcenon.
void example_3_imonitorable_interface()
Example 3: Demonstrating IMonitorable interface.
void example_2_multiple_loggers()
Example 2: Multiple loggers with single monitor.
void example_1_basic_integration()
Example 1: Basic monitor integration with logger.
void print_health_result(const ci::health_check_result &health)
Print health check result.
void print_metrics_snapshot(const ci::metrics_snapshot &snapshot)
Print metrics snapshot in formatted way.
void example_4_monitoring_system_simulation()
Example 4: Simulating monitoring_system integration.
bool is_ok(const Result< T > &result)
T & get_value(Result< T > &result)
error_info & get_error(Result< T > &result)
@ size
Rotate based on file size only.