Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
basic_monitoring_example.cpp

This example shows how to:

// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <iostream>
#include <thread>
#include <chrono>
using namespace kcenon::monitoring;
using namespace std::chrono_literals;
int main() {
std::cout << "=== Basic Monitoring Example ===" << std::endl;
try {
// Step 1: Configure the monitoring system
config.history_size = 1000;
config.collection_interval = std::chrono::milliseconds(1000);
config.enable_compression = false; // Keep it simple for this example
std::cout << "1. Creating monitoring system with configuration:" << std::endl;
std::cout << " - History size: " << config.history_size << std::endl;
std::cout << " - Collection interval: 1000ms" << std::endl;
// Step 2: Create performance monitor directly (simplified for example)
performance_monitor perf_monitor("example_monitor");
// Step 3: Initialize performance monitor
if (auto result = perf_monitor.initialize(); result.is_err()) {
std::cerr << "Failed to initialize performance monitor: "
<< result.error().message << std::endl;
return 1;
}
std::cout << "2. Initialized performance monitor" << std::endl;
// Step 4: Configure and add storage backend
storage_config storage_cfg;
storage_cfg.type = storage_backend_type::file_json;
storage_cfg.path = "monitoring_data.json";
auto storage = std::make_unique<file_storage_backend>(storage_cfg);
// Storage backend is ready to use
// Storage is configured and ready to use
std::cout << "3. Configured JSON file storage backend" << std::endl;
std::cout << "4. Monitoring system ready" << std::endl;
std::cout << std::endl;
// Step 6: Simulate some application work and collect metrics
std::cout << "5. Simulating application workload..." << std::endl;
for (int i = 0; i < 10; ++i) {
std::cout << " Iteration " << (i + 1) << "/10" << std::endl;
// Simulate timing an operation
{
auto timer = perf_monitor.time_operation("iteration_" + std::to_string(i));
std::this_thread::sleep_for(100ms);
}
// Simulate some work
std::this_thread::sleep_for(500ms);
// Get current system metrics
if (system_metrics.is_ok()) {
std::cout << " CPU: " << system_metrics.value().cpu_usage_percent
<< "%, Memory: " << (system_metrics.value().memory_usage_bytes / (1024.0 * 1024.0)) << " MB" << std::endl;
}
}
std::cout << std::endl;
// Step 7: Collect and display metrics
std::cout << "6. Collecting metrics:" << std::endl;
auto metrics_result = perf_monitor.collect();
if (metrics_result.is_ok()) {
auto& snapshot = metrics_result.value();
std::cout << " Total metrics collected: " << snapshot.metrics.size() << std::endl;
for (const auto& metric : snapshot.metrics) {
std::cout << " - Metric: " << metric.name << std::endl;
}
}
std::cout << std::endl;
// Step 8: Cleanup
if (auto result = perf_monitor.cleanup(); result.is_err()) {
std::cerr << "Failed to cleanup: "
<< result.error().message << std::endl;
return 1;
}
// Flush storage
if (auto result = storage->flush(); result.is_err()) {
std::cerr << "Failed to flush storage: "
<< result.error().message << std::endl;
}
std::cout << std::endl;
std::cout << "7. Monitoring completed successfully" << std::endl;
std::cout << " Data saved to: monitoring_data.json" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
std::cout << std::endl;
std::cout << "=== Example completed successfully ===" << std::endl;
return 0;
}
Performance monitor combining profiling and system monitoring.
common::VoidResult initialize() override
Initialize the collector.
system_monitor & get_system_monitor()
Get system monitor.
common::VoidResult cleanup() override
Cleanup collector resources.
common::Result< metrics_snapshot > collect() override
Collect metrics.
scoped_timer time_operation(const std::string &operation_name)
Create a scoped timer for an operation.
common::Result< system_metrics > get_current_metrics() const
Get current system metrics.
Core monitoring system interface definitions.
Performance monitoring and profiling implementation.
Result pattern type definitions for monitoring system.
Storage backend type definitions for metric persistence.
Basic metric structure for interface compatibility.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval