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

Monitoring system integration example with Result pattern. More...

#include <kcenon/monitoring/core/performance_monitor.h>
#include <kcenon/monitoring/core/result_types.h>
#include <kcenon/common/interfaces/logger_interface.h>
#include <kcenon/common/interfaces/monitoring_interface.h>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <iomanip>
Include dependency graph for logger_di_integration_example.cpp:

Go to the source code of this file.

Classes

class  simple_console_logger
 Simple logger implementation for demonstration. More...
 

Functions

void example_1_basic_monitoring ()
 Example 1: Basic monitoring with Result pattern.
 
void example_2_error_handling ()
 Example 2: Error handling with Result pattern.
 
void example_3_threshold_monitoring ()
 Example 3: Threshold monitoring.
 
void example_4_multiple_monitors ()
 Example 4: Multiple monitors.
 
void example_5_metrics_with_tags ()
 Example 5: Metrics with tags.
 
void example_6_monitoring_workflow ()
 Example 6: Simulating monitoring workflow.
 
int main ()
 

Detailed Description

Monitoring system integration example with Result pattern.

Definition in file logger_di_integration_example.cpp.

Function Documentation

◆ example_1_basic_monitoring()

void example_1_basic_monitoring ( )

Example 1: Basic monitoring with Result pattern.

Examples
logger_di_integration_example.cpp.

Definition at line 95 of file logger_di_integration_example.cpp.

95 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ example_2_error_handling()

void example_2_error_handling ( )

Example 2: Error handling with Result pattern.

Examples
logger_di_integration_example.cpp.

Definition at line 125 of file logger_di_integration_example.cpp.

125 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ example_3_threshold_monitoring()

void example_3_threshold_monitoring ( )

Example 3: Threshold monitoring.

Examples
logger_di_integration_example.cpp.

Definition at line 143 of file logger_di_integration_example.cpp.

143 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ example_4_multiple_monitors()

void example_4_multiple_monitors ( )

Example 4: Multiple monitors.

Examples
logger_di_integration_example.cpp.

Definition at line 171 of file logger_di_integration_example.cpp.

171 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ example_5_metrics_with_tags()

void example_5_metrics_with_tags ( )

Example 5: Metrics with tags.

Examples
logger_di_integration_example.cpp.

Definition at line 199 of file logger_di_integration_example.cpp.

199 {
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}
std::unordered_map< std::string, std::string > tag_map
Type alias for metric tags/labels.

Referenced by main().

Here is the caller graph for this function:

◆ example_6_monitoring_workflow()

void example_6_monitoring_workflow ( )

Example 6: Simulating monitoring workflow.

Examples
logger_di_integration_example.cpp.

Definition at line 224 of file logger_di_integration_example.cpp.

224 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 259 of file logger_di_integration_example.cpp.

259 {
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}
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.

References example_1_basic_monitoring(), example_2_error_handling(), example_3_threshold_monitoring(), example_4_multiple_monitors(), example_5_metrics_with_tags(), and example_6_monitoring_workflow().

Here is the call graph for this function: