Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
logger_di_integration_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
16#include <kcenon/common/interfaces/logger_interface.h>
17#include <kcenon/common/interfaces/monitoring_interface.h>
18#include <iostream>
19#include <memory>
20#include <thread>
21#include <chrono>
22#include <iomanip>
23
24using namespace kcenon::monitoring;
25using namespace kcenon::common::interfaces;
26
30class simple_console_logger : public ILogger {
31private:
32 log_level min_level_ = log_level::debug;
33 std::atomic<size_t> log_count_{0};
34
35public:
36 explicit simple_console_logger(log_level min = log_level::debug)
37 : min_level_(min) {}
38
39 kcenon::common::VoidResult log(log_level level, const std::string& message) override {
40 if (!is_enabled(level)) {
41 return kcenon::common::ok();
42 }
43
44 auto now = std::chrono::system_clock::now();
45 auto time = std::chrono::system_clock::to_time_t(now);
46
47 // Thread-safe time conversion
48 std::tm tm_buf;
49#ifdef _WIN32
50 localtime_s(&tm_buf, &time);
51#else
52 localtime_r(&time, &tm_buf);
53#endif
54
55 std::cout << "[" << std::put_time(&tm_buf, "%H:%M:%S")
56 << "] [" << to_string(level) << "] "
57 << message << std::endl;
58
59 log_count_++;
60 return kcenon::common::ok();
61 }
62
63 kcenon::common::VoidResult log(const log_entry& entry) override {
64 std::string msg = entry.message;
65 if (!entry.file.empty()) {
66 msg += " [" + entry.file + ":" + std::to_string(entry.line) + " " + entry.function + "]";
67 }
68 return log(entry.level, msg);
69 }
70
71 bool is_enabled(log_level level) const override {
72 return static_cast<int>(level) >= static_cast<int>(min_level_);
73 }
74
75 kcenon::common::VoidResult set_level(log_level level) override {
76 min_level_ = level;
77 return kcenon::common::ok();
78 }
79
80 log_level get_level() const override {
81 return min_level_;
82 }
83
84 kcenon::common::VoidResult flush() override {
85 std::cout << std::flush;
86 return kcenon::common::ok();
87 }
88
89 size_t get_log_count() const { return log_count_.load(); }
90};
91
96 std::cout << "\n=== Example 1: Basic Monitoring ===" << std::endl;
97
98 // Create monitor
99 auto monitor = std::make_shared<performance_monitor>();
100
101 std::cout << "\nRecording metrics..." << std::endl;
102
103 // Record metrics using Result pattern (updated API: record_counter, record_gauge)
104 auto result1 = monitor->record_counter("requests_total", 100.0);
105 if (result1.is_ok()) {
106 std::cout << " Metric 'requests_total' recorded" << std::endl;
107 }
108
109 auto result2 = monitor->record_counter("errors_total", 5.0);
110 if (result2.is_ok()) {
111 std::cout << " Metric 'errors_total' recorded" << std::endl;
112 }
113
114 // Get metrics snapshot using collect()
115 auto metrics = monitor->collect();
116 if (metrics.is_ok()) {
117 const auto& snapshot = metrics.value();
118 std::cout << " Retrieved " << snapshot.metrics.size() << " metrics" << std::endl;
119 }
120}
121
126 std::cout << "\n=== Example 2: Error Handling ===" << std::endl;
127
128 auto monitor = std::make_shared<performance_monitor>();
129
130 // Record metric and check result (updated API: record_gauge)
131 auto result = monitor->record_gauge("cpu_usage", 45.5);
132 if (result.is_ok()) {
133 std::cout << " Metric recorded successfully" << std::endl;
134 } else {
135 const auto& err = result.error();
136 std::cout << " Error: " << err.message << std::endl;
137 }
138}
139
144 std::cout << "\n=== Example 3: Threshold Monitoring ===" << std::endl;
145
146 auto monitor = std::make_shared<performance_monitor>();
147
148 std::cout << "\nConfiguring thresholds..." << std::endl;
149
150 // Set thresholds
151 monitor->set_cpu_threshold(80.0);
152 monitor->set_memory_threshold(90.0);
153 monitor->set_latency_threshold(std::chrono::milliseconds(1000));
154
155 // Check thresholds
156 auto threshold_result = monitor->check_thresholds();
157 if (threshold_result.is_ok()) {
158 bool exceeded = threshold_result.value();
159 std::cout << " Thresholds exceeded: " << (exceeded ? "Yes" : "No") << std::endl;
160
161 auto thresholds = monitor->get_thresholds();
162 std::cout << " CPU threshold: " << thresholds.cpu_threshold << "%" << std::endl;
163 std::cout << " Memory threshold: " << thresholds.memory_threshold << "%" << std::endl;
164 std::cout << " Latency threshold: " << thresholds.latency_threshold.count() << "ms" << std::endl;
165 }
166}
167
172 std::cout << "\n=== Example 4: Multiple Monitors ===" << std::endl;
173
174 // Create multiple monitors
175 auto monitor1 = std::make_shared<performance_monitor>("monitor1");
176 auto monitor2 = std::make_shared<performance_monitor>("monitor2");
177
178 std::cout << "\nMonitor 1 recording metrics..." << std::endl;
179 monitor1->record_counter("monitor1_metric", 100.0);
180
181 std::cout << "Monitor 2 recording metrics..." << std::endl;
182 monitor2->record_counter("monitor2_metric", 200.0);
183
184 // Get metrics from both using collect()
185 auto metrics1 = monitor1->collect();
186 auto metrics2 = monitor2->collect();
187
188 if (metrics1.is_ok() && metrics2.is_ok()) {
189 const auto& snapshot1 = metrics1.value();
190 const auto& snapshot2 = metrics2.value();
191 std::cout << " Monitor 1: " << snapshot1.metrics.size() << " metrics" << std::endl;
192 std::cout << " Monitor 2: " << snapshot2.metrics.size() << " metrics" << std::endl;
193 }
194}
195
200 std::cout << "\n=== Example 5: Metrics with Tags ===" << std::endl;
201
202 auto monitor = std::make_shared<performance_monitor>();
203
204 // Record metrics with tags (updated API: record_histogram with tags)
205 tag_map tags{
206 {"service", "api"},
207 {"region", "us-east-1"},
208 {"instance", "i-12345"}
209 };
210
211 auto result = monitor->record_histogram("request_latency", 150.0, tags);
212 if (result.is_ok()) {
213 std::cout << " Metric with tags recorded successfully" << std::endl;
214 }
215
216 // Get all tagged metrics
217 auto all_metrics = monitor->get_all_tagged_metrics();
218 std::cout << " Total tagged metrics: " << all_metrics.size() << std::endl;
219}
220
225 std::cout << "\n=== Example 6: Monitoring Workflow ===" << std::endl;
226
227 auto monitor = std::make_shared<performance_monitor>();
228 auto logger = std::make_shared<simple_console_logger>();
229
230 std::cout << "\nSimulating application workload..." << std::endl;
231
232 // Simulate application metrics (updated API: record_gauge)
233 for (int i = 0; i < 5; ++i) {
234 auto result = monitor->record_gauge("requests", static_cast<double>(i * 10));
235 if (result.is_ok()) {
236 logger->log(log_level::info, "Recorded metric: requests = " + std::to_string(i * 10));
237 }
238 std::this_thread::sleep_for(std::chrono::milliseconds(100));
239 }
240
241 // Check thresholds and log results
242 auto threshold_result = monitor->check_thresholds();
243 if (threshold_result.is_ok()) {
244 bool exceeded = threshold_result.value();
245 logger->log(log_level::info, "Thresholds exceeded: " + std::string(exceeded ? "Yes" : "No"));
246 }
247
248 // Get metrics and log summary
249 auto metrics = monitor->collect();
250 if (metrics.is_ok()) {
251 const auto& snapshot = metrics.value();
252 logger->log(log_level::info, "Collected " + std::to_string(snapshot.metrics.size()) + " metrics");
253 }
254
255 std::cout << "\n Workflow completed successfully" << std::endl;
256 std::cout << " Logger events: " << logger->get_log_count() << std::endl;
257}
258
259int main() {
260 std::cout << "========================================================" << std::endl;
261 std::cout << "Monitoring System - Integration Examples" << std::endl;
262 std::cout << "Using common_system interfaces and Result<T> pattern" << std::endl;
263 std::cout << "========================================================" << std::endl;
264
265 try {
272
273 std::cout << "\n========================================================" << std::endl;
274 std::cout << "All integration examples completed!" << std::endl;
275 std::cout << "Key Points:" << std::endl;
276 std::cout << " common_system interfaces used" << std::endl;
277 std::cout << " Result<T> pattern for error handling" << std::endl;
278 std::cout << " Interface-based loose coupling" << std::endl;
279 std::cout << " Comprehensive monitoring" << std::endl;
280 std::cout << "========================================================" << std::endl;
281
282 } catch (const std::exception& e) {
283 std::cerr << "Error: " << e.what() << std::endl;
284 return 1;
285 }
286
287 return 0;
288}
Simple logger implementation for demonstration.
log_level get_level() const override
kcenon::common::VoidResult set_level(log_level level) override
simple_console_logger(log_level min=log_level::debug)
bool is_enabled(log_level level) const override
kcenon::common::VoidResult log(log_level level, const std::string &message) override
kcenon::common::VoidResult flush() override
kcenon::common::VoidResult log(const log_entry &entry) override
void example_4_multiple_monitors()
Example 4: Multiple monitors.
void example_2_error_handling()
Example 2: Error handling with Result pattern.
void example_6_monitoring_workflow()
Example 6: Simulating monitoring workflow.
void example_3_threshold_monitoring()
Example 3: Threshold monitoring.
void example_5_metrics_with_tags()
Example 5: Metrics with tags.
void example_1_basic_monitoring()
Example 1: Basic monitoring with Result pattern.
auto to_string(plugin_load_error error) -> std::string
Convert plugin_load_error to string.
std::unordered_map< std::string, std::string > tag_map
Type alias for metric tags/labels.
Performance monitoring and profiling implementation.
Result pattern type definitions for monitoring system.