Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
result_pattern_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
17#include <iostream>
21
22using namespace kcenon::monitoring;
23
24// Example function that may fail
25kcenon::common::Result<double> divide(double a, double b) {
26 if (b == 0) {
27 return kcenon::common::Result<double>::err(error_info(monitoring_error_code::invalid_configuration, "Division by zero").to_common_error());
28 }
29 return kcenon::common::ok(a / b);
30}
31
32// Example function using kcenon::common::VoidResult
33kcenon::common::VoidResult validate_range(double value, double min, double max) {
34 if (value < min || value > max) {
35 error_info err(monitoring_error_code::invalid_configuration,
36 "Value out of range [" + std::to_string(min) + ", " + std::to_string(max) + "]");
37 return kcenon::common::VoidResult::err(err.to_common_error());
38 }
39 return kcenon::common::ok();
40}
41
42// Example using monadic operations
43kcenon::common::Result<std::string> process_metric(double value) {
44 // Chain operations using map and and_then
45 return divide(100.0, value)
46 .map([](double x) { return x * 2; })
47 .and_then([](double x) {
48 if (x > 50) {
49 return kcenon::common::ok("High value: " + std::to_string(x));
50 }
51 return kcenon::common::ok("Normal value: " + std::to_string(x));
52 });
53}
54
55int main() {
56 std::cout << "=== Result Pattern Example ===" << std::endl << std::endl;
57
58 // Example 1: Successful operation
59 std::cout << "Example 1: Successful division" << std::endl;
60 auto result1 = divide(10.0, 2.0);
61 if (result1.is_ok()) {
62 std::cout << " Result: " << result1.value() << std::endl;
63 } else {
64 std::cout << " Error: " << result1.error().message << std::endl;
65 }
66 std::cout << std::endl;
67
68 // Example 2: Failed operation
69 std::cout << "Example 2: Division by zero" << std::endl;
70 auto result2 = divide(10.0, 0.0);
71 if (result2.is_ok()) {
72 std::cout << " Result: " << result2.value() << std::endl;
73 } else {
74 std::cout << " Error: " << result2.error().message << std::endl;
75 }
76 std::cout << std::endl;
77
78 // Example 3: Using value_or
79 std::cout << "Example 3: Using value_or with default" << std::endl;
80 auto result3 = divide(5.0, 0.0);
81 double value = result3.value_or(-1.0);
82 std::cout << " Value (with default): " << value << std::endl;
83 std::cout << std::endl;
84
85 // Example 4: kcenon::common::VoidResult usage
86 std::cout << "Example 4: Validation with kcenon::common::VoidResult" << std::endl;
87 auto validation1 = validate_range(50.0, 0.0, 100.0);
88 if (validation1.is_ok()) {
89 std::cout << " Validation passed" << std::endl;
90 } else {
91 std::cout << " Validation failed: " << validation1.error().message << std::endl;
92 }
93
94 auto validation2 = validate_range(150.0, 0.0, 100.0);
95 if (validation2.is_ok()) {
96 std::cout << " Validation passed" << std::endl;
97 } else {
98 std::cout << " Validation failed: " << validation2.error().message << std::endl;
99 }
100 std::cout << std::endl;
101
102 // Example 5: Monadic operations
103 std::cout << "Example 5: Chaining operations" << std::endl;
104 auto result4 = process_metric(4.0);
105 if (result4.is_ok()) {
106 std::cout << " " << result4.value() << std::endl;
107 } else {
108 std::cout << " Error: " << result4.error().message << std::endl;
109 }
110
111 auto result5 = process_metric(1.0);
112 if (result5.is_ok()) {
113 std::cout << " " << result5.value() << std::endl;
114 } else {
115 std::cout << " Error: " << result5.error().message << std::endl;
116 }
117 std::cout << std::endl;
118
119 // Example 6: Metrics snapshot
120 std::cout << "Example 6: Metrics snapshot" << std::endl;
121 metrics_snapshot snapshot;
122 snapshot.add_metric("cpu_usage", 65.5);
123 snapshot.add_metric("memory_usage", 4096.0);
124 snapshot.add_metric("disk_io", 150.25);
125
126 std::cout << " Metrics collected: " << snapshot.metrics.size() << std::endl;
127
128 if (auto cpu = snapshot.get_metric("cpu_usage")) {
129 std::cout << " CPU Usage: " << cpu.value() << "%" << std::endl;
130 }
131
132 if (auto mem = snapshot.get_metric("memory_usage")) {
133 std::cout << " Memory Usage: " << mem.value() << " MB" << std::endl;
134 }
135 std::cout << std::endl;
136
137 // Example 7: Configuration validation
138 std::cout << "Example 7: Configuration validation" << std::endl;
139 monitoring_config config;
140 config.history_size = 1000;
141 config.collection_interval = std::chrono::milliseconds(100);
142 config.buffer_size = 5000;
143
144 auto config_result = config.validate();
145 if (config_result.is_ok()) {
146 std::cout << " Configuration is valid" << std::endl;
147 std::cout << " - History size: " << config.history_size << std::endl;
148 std::cout << " - Collection interval: " << config.collection_interval.count() << "ms" << std::endl;
149 std::cout << " - Buffer size: " << config.buffer_size << std::endl;
150 } else {
151 std::cout << " Configuration error: " << config_result.error().message << std::endl;
152 }
153
154 return 0;
155}
Monitoring system specific error codes.
Core monitoring system interface definitions.
@ cpu
CPU power domain (RAPL)
kcenon::common::Result< double > divide(double a, double b)
kcenon::common::VoidResult validate_range(double value, double min, double max)
kcenon::common::Result< std::string > process_metric(double value)
Result pattern type definitions for monitoring system.
Extended error information with context.
common::error_info to_common_error() const
Convert to common_system error_info.
Complete snapshot of metrics at a point in time.
std::optional< double > get_metric(const std::string &name) const
Get a specific metric value.
std::vector< metric_value > metrics
void add_metric(const std::string &name, double value)
Add a metric to the snapshot.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval
common::VoidResult validate() const
Validate configuration parameters.