Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
event_bus_example.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
20#include <iostream>
21#include <thread>
22#include <chrono>
23
24using namespace kcenon::monitoring;
25using namespace std::chrono_literals;
26
27int main() {
28 std::cout << "=== Event Bus Example ===" << std::endl;
29
30 // Create event bus with configuration
31 event_bus::config bus_config;
32 bus_config.max_queue_size = 1000;
33 bus_config.worker_thread_count = 2;
34 bus_config.auto_start = true;
35
36 auto bus = std::make_shared<event_bus>(bus_config);
37
38 // Example 1: Subscribe to performance alerts
39 std::cout << "\nExample 1: Performance Alert Monitoring" << std::endl;
40
41 auto alert_token = bus->subscribe_event<performance_alert_event>(
42 [](const performance_alert_event& event) {
43 std::cout << " Alert: " << event.get_message()
44 << " (Component: " << event.get_component() << ")" << std::endl;
45 },
46 event_priority::high
47 );
48
49 // Publish a performance alert
51 performance_alert_event::alert_type::high_cpu_usage,
52 performance_alert_event::alert_severity::warning,
53 "main_processor",
54 "CPU usage exceeds 80%",
55 80.0, // threshold
56 85.5 // actual value
57 );
58
59 bus->publish_event(alert);
60 std::this_thread::sleep_for(100ms);
61
62 // Example 2: System resource monitoring
63 std::cout << "\nExample 2: System Resource Monitoring" << std::endl;
64
65 bus->subscribe_event<system_resource_event>(
66 [](const system_resource_event& event) {
67 auto stats = event.get_stats();
68 std::cout << " Resource Update:" << std::endl;
69 std::cout << " CPU: " << stats.cpu_usage_percent << "%" << std::endl;
70 std::cout << " Memory: " << stats.memory_used_bytes / (1024*1024) << " MB" << std::endl;
71 }
72 );
73
74 // Simulate resource event
76 resources.cpu_usage_percent = 45.2;
77 resources.memory_used_bytes = 1024 * 1024 * 512; // 512 MB
78 resources.memory_total_bytes = 1024 * 1024 * 1024 * 8; // 8 GB
79
80 bus->publish_event(system_resource_event(resources));
81 std::this_thread::sleep_for(100ms);
82
83 // Example 3: Component lifecycle tracking
84 std::cout << "\nExample 3: Component Lifecycle Tracking" << std::endl;
85
86 bus->subscribe_event<component_lifecycle_event>(
87 [](const component_lifecycle_event& event) {
88 std::cout << " Component '" << event.get_component()
89 << "' changed from "
90 << static_cast<int>(event.get_old_state())
91 << " to "
92 << static_cast<int>(event.get_new_state()) << std::endl;
93 }
94 );
95
96 // Simulate component lifecycle
97 bus->publish_event(component_lifecycle_event(
98 "database_connector",
99 component_lifecycle_event::lifecycle_state::stopped,
100 component_lifecycle_event::lifecycle_state::initializing
101 ));
102
103 bus->publish_event(component_lifecycle_event(
104 "database_connector",
105 component_lifecycle_event::lifecycle_state::initializing,
106 component_lifecycle_event::lifecycle_state::running
107 ));
108
109 std::this_thread::sleep_for(100ms);
110
111 // Example 4: Thread system adapter (when available)
112 std::cout << "\nExample 4: Thread System Adapter" << std::endl;
113
114 thread_system_adapter thread_adapter(bus);
115
116 if (thread_adapter.is_thread_system_available()) {
117 std::cout << " Thread system is available" << std::endl;
118
119 // Start collection
120 collection_config config;
121 config.interval = 1s;
122 thread_adapter.start_collection(config);
123
124 std::this_thread::sleep_for(3s);
125
126 thread_adapter.stop_collection();
127 } else {
128 std::cout << " Thread system is not available (expected)" << std::endl;
129 }
130
131 // Example 5: Logger system adapter
132 std::cout << "\nExample 5: Logger System Adapter" << std::endl;
133
134 logger_system_adapter logger_adapter(bus);
135
136 if (logger_adapter.is_logger_system_available()) {
137 std::cout << " Logger system is available" << std::endl;
138 } else {
139 std::cout << " Logger system is not available (expected)" << std::endl;
140 }
141
142 // Example 6: Event bus statistics
143 std::cout << "\nExample 6: Event Bus Statistics" << std::endl;
144
145 auto stats = bus->get_stats();
146 std::cout << " Total published: " << stats.total_published << std::endl;
147 std::cout << " Total processed: " << stats.total_processed << std::endl;
148 std::cout << " Total dropped: " << stats.total_dropped << std::endl;
149 std::cout << " Queue size: " << stats.current_queue_size << std::endl;
150 std::cout << " Subscribers: " << stats.subscriber_count << std::endl;
151
152 // Example 7: Health check events
153 std::cout << "\nExample 7: Health Check Events" << std::endl;
154
155 bus->subscribe_event<health_check_event>(
156 [](const health_check_event& event) {
157 auto overall = event.get_overall_status();
158 std::cout << " Health check for '" << event.get_component() << "': ";
159 switch (overall) {
160 case health_check_event::health_status::healthy:
161 std::cout << "HEALTHY" << std::endl;
162 break;
163 case health_check_event::health_status::degraded:
164 std::cout << "DEGRADED" << std::endl;
165 break;
166 case health_check_event::health_status::unhealthy:
167 std::cout << "UNHEALTHY" << std::endl;
168 break;
169 default:
170 std::cout << "UNKNOWN" << std::endl;
171 }
172 }
173 );
174
175 // Create health check results
176 std::vector<health_check_event::health_check_result> health_results;
177
179 db_check.check_name = "database_connection";
180 db_check.status = health_check_event::health_status::healthy;
181 db_check.message = "Connection OK";
182 db_check.response_time = 15ms;
183 health_results.push_back(db_check);
184
186 api_check.check_name = "api_endpoint";
187 api_check.status = health_check_event::health_status::degraded;
188 api_check.message = "Slow response";
189 api_check.response_time = 500ms;
190 health_results.push_back(api_check);
191
192 bus->publish_event(health_check_event("backend_service", health_results));
193 std::this_thread::sleep_for(100ms);
194
195 // Cleanup
196 std::cout << "\nStopping event bus..." << std::endl;
197 bus->stop();
198
199 std::cout << "✅ Event bus example completed successfully!" << std::endl;
200
201 return 0;
202}
Event for component lifecycle changes.
Event for health check results.
Logger system adapter using dependency injection (Phase 2.3.3)
bool is_logger_system_available() const
Check if logger is available.
Event for performance-related alerts.
Event containing system resource metrics.
common::VoidResult start_collection(const collection_config &cfg)
Lightweight event bus implementation for monitoring system.
int main()
Common event type definitions for monitoring system.
Consolidated logger system adapters for monitoring_system.
Core alert data structure.
Configuration for metric collection.
Configuration for event bus.
Definition event_bus.h:87
Consolidated thread system adapters for monitoring_system.