96 {
97 std::cout << "=== Production Monitoring Stack Example ===" << std::endl;
98 std::cout << std::endl;
99
100 try {
101
102
103
104 std::cout << "1. Configuring Production Monitoring Stack" << std::endl;
105 std::cout << " =========================================" << std::endl;
106 std::cout << std::endl;
107
108
113
114 std::cout << " Performance Monitor:" << std::endl;
115 std::cout <<
" - History size: " << perf_config.
history_size << std::endl;
116 std::cout << " - Collection interval: 5s" << std::endl;
117 std::cout << std::endl;
118
119
124
125 std::cout << " Alert Manager:" << std::endl;
126 std::cout << " - Evaluation interval: 10s" << std::endl;
127 std::cout << " - Grouping: enabled" << std::endl;
128 std::cout << std::endl;
129
130
134
135 std::cout << " Health Monitor:" << std::endl;
136 std::cout << " - Check interval: 5s" << std::endl;
137 std::cout << " - Auto-recovery: enabled" << std::endl;
138 std::cout << std::endl;
139
140
141
142
143 std::cout << "2. Initializing Components" << std::endl;
144 std::cout << " ======================= " << std::endl;
145 std::cout << std::endl;
146
147
149 if (auto result = perf_monitor.initialize(); result.is_err()) {
150 std::cerr << "Failed to initialize performance monitor" << std::endl;
151 return 1;
152 }
153 std::cout << " [OK] Performance monitor" << std::endl;
154
155
157 std::cout << " [OK] Health monitor" << std::endl;
158
159
161 std::cout << " [OK] Alert manager" << std::endl;
162 std::cout << std::endl;
163
164
165
166
167 std::cout << "3. Configuring Storage" << std::endl;
168 std::cout << " ====================" << std::endl;
169 std::cout << std::endl;
170
172 storage_cfg.
type = storage_backend_type::file_json;
173 storage_cfg.
path =
"production_metrics.json";
174
175 auto storage = std::make_unique<file_storage_backend>(storage_cfg);
176 std::cout << " [OK] JSON file storage configured" << std::endl;
177 std::cout << std::endl;
178
179
180
181
182 std::cout << "4. Registering Health Checks" << std::endl;
183 std::cout << " ===========================" << std::endl;
184 std::cout << std::endl;
185
186 auto db_check = std::make_shared<database_health_check>("database");
187 health_mon.register_check("database", db_check);
188 std::cout << " [OK] Database health check" << std::endl;
189
190 auto api_check = std::make_shared<external_api_health_check>("external_api");
191 health_mon.register_check("external_api", api_check);
192 std::cout << " [OK] External API health check" << std::endl;
193 std::cout << std::endl;
194
195
196
197
198 std::cout << "5. Configuring Alert Rules" << std::endl;
199 std::cout << " ========================" << std::endl;
200 std::cout << std::endl;
201
202 auto cpu_rule = std::make_shared<alert_rule>("high_cpu_usage");
203 cpu_rule->set_metric_name("cpu_usage")
204 .set_severity(alert_severity::warning)
205 .set_summary("CPU usage exceeds 80%")
207
208 alert_mgr.add_rule(cpu_rule);
209 std::cout << " [OK] CPU usage alert rule" << std::endl;
210
211 auto log_notifier_ptr = std::make_shared<log_notifier>("console_logger");
212 alert_mgr.add_notifier(log_notifier_ptr);
213 std::cout << " [OK] Console notifier" << std::endl;
214 std::cout << std::endl;
215
216
217
218
219 std::cout << "6. Starting Monitoring" << std::endl;
220 std::cout << " ====================" << std::endl;
221 std::cout << std::endl;
222
223 health_mon.start();
224 std::cout << " [OK] Health monitor started" << std::endl;
225
226 alert_mgr.start();
227 std::cout << " [OK] Alert manager started" << std::endl;
228 std::cout << std::endl;
229
230
233
234 std::cout << "7. Monitoring Active (Ctrl+C to shutdown)" << std::endl;
235 std::cout << " ========================================" << std::endl;
236 std::cout << std::endl;
237
238
239
240
241 int iteration = 0;
243 std::cout << " Iteration " << (iteration + 1) << "/10" << std::endl;
244
245
246 {
247 auto timer = perf_monitor.time_operation(
"iteration_" + std::to_string(iteration));
248 std::this_thread::sleep_for(200ms);
249 }
250
251
252 auto health_result = health_mon.check_health();
253 if (health_result.status == health_status::healthy) {
254 std::cout << " Health: Healthy" << std::endl;
255 }
256
257
258 auto system_metrics = perf_monitor.get_system_monitor().get_current_metrics();
262 << "%, Memory: " << (metrics.memory_usage_bytes / (1024.0 * 1024.0))
263 << " MB" << std::endl;
264
265
266 alert_mgr.process_metric("cpu_usage", metrics.cpu_usage_percent);
267 }
268
269 std::cout << std::endl;
270 std::this_thread::sleep_for(2s);
271 iteration++;
272 }
273
274
275
276
277 std::cout << "8. Graceful Shutdown" << std::endl;
278 std::cout << " ==================" << std::endl;
279 std::cout << std::endl;
280
281 alert_mgr.stop();
282 std::cout << " [OK] Alert manager stopped" << std::endl;
283
284 health_mon.stop();
285 std::cout << " [OK] Health monitor stopped" << std::endl;
286
287 perf_monitor.cleanup();
288 std::cout << " [OK] Performance monitor cleaned up" << std::endl;
289
291 std::cout << " [OK] Storage flushed" << std::endl;
292 std::cout << std::endl;
293
294 std::cout << "=== Production Monitoring Completed Successfully ===" << std::endl;
295
296 } catch (const std::exception& e) {
297 std::cerr << "Exception: " << e.what() << std::endl;
298 return 1;
299 }
300
301 return 0;
302}
Central coordinator for the alert pipeline.
Health monitor with dependency management, auto-recovery, and statistics.
static std::shared_ptr< threshold_trigger > above(double threshold)
Create trigger for value > threshold.
@ timer
StatsD-specific timer metric.
@ storage
Storage device sensor.
std::atomic< bool > shutdown_requested
void signal_handler(int signal)
Configuration for the alert manager.
std::chrono::milliseconds default_evaluation_interval
Default eval interval.
bool enable_grouping
Enable alert grouping.
std::chrono::milliseconds default_repeat_interval
Default repeat interval.
Configuration for the health_monitor.
std::chrono::milliseconds check_interval
Interval between automatic health check cycles.
bool enable_auto_recovery
Whether to invoke recovery handlers on failure.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval
storage_backend_type type