-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_monitoring_example.cpp
More file actions
137 lines (105 loc) · 4.97 KB
/
Copy pathbasic_monitoring_example.cpp
File metadata and controls
137 lines (105 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
/**
* @file basic_monitoring_example.cpp
* @brief Basic example demonstrating simple monitoring setup
* @example basic_monitoring_example.cpp
*
* This example shows how to:
* - Initialize the monitoring system
* - Collect basic metrics
* - Store metrics to a file
* - Query and display metrics
*/
#include <iostream>
#include <thread>
#include <chrono>
#include "kcenon/monitoring/interfaces/monitoring_core.h"
#include "kcenon/monitoring/core/performance_monitor.h"
#include "kcenon/monitoring/storage/storage_backends.h"
#include "kcenon/monitoring/core/result_types.h"
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
monitoring_config config;
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
auto system_metrics = perf_monitor.get_system_monitor().get_current_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;
}