16#include <kcenon/common/interfaces/logger_interface.h>
17#include <kcenon/common/interfaces/monitoring_interface.h>
25using namespace kcenon::common::interfaces;
39 kcenon::common::VoidResult
log(log_level level,
const std::string& message)
override {
41 return kcenon::common::ok();
44 auto now = std::chrono::system_clock::now();
45 auto time = std::chrono::system_clock::to_time_t(now);
50 localtime_s(&tm_buf, &time);
52 localtime_r(&time, &tm_buf);
55 std::cout <<
"[" << std::put_time(&tm_buf,
"%H:%M:%S")
57 << message << std::endl;
60 return kcenon::common::ok();
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 +
"]";
68 return log(entry.level, msg);
72 return static_cast<int>(level) >=
static_cast<int>(
min_level_);
75 kcenon::common::VoidResult
set_level(log_level level)
override {
77 return kcenon::common::ok();
84 kcenon::common::VoidResult
flush()
override {
85 std::cout << std::flush;
86 return kcenon::common::ok();
96 std::cout <<
"\n=== Example 1: Basic Monitoring ===" << std::endl;
99 auto monitor = std::make_shared<performance_monitor>();
101 std::cout <<
"\nRecording metrics..." << std::endl;
104 auto result1 = monitor->record_counter(
"requests_total", 100.0);
105 if (result1.is_ok()) {
106 std::cout <<
" Metric 'requests_total' recorded" << std::endl;
109 auto result2 = monitor->record_counter(
"errors_total", 5.0);
110 if (result2.is_ok()) {
111 std::cout <<
" Metric 'errors_total' recorded" << std::endl;
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;
126 std::cout <<
"\n=== Example 2: Error Handling ===" << std::endl;
128 auto monitor = std::make_shared<performance_monitor>();
131 auto result = monitor->record_gauge(
"cpu_usage", 45.5);
132 if (result.is_ok()) {
133 std::cout <<
" Metric recorded successfully" << std::endl;
135 const auto& err = result.error();
136 std::cout <<
" Error: " << err.message << std::endl;
144 std::cout <<
"\n=== Example 3: Threshold Monitoring ===" << std::endl;
146 auto monitor = std::make_shared<performance_monitor>();
148 std::cout <<
"\nConfiguring thresholds..." << std::endl;
151 monitor->set_cpu_threshold(80.0);
152 monitor->set_memory_threshold(90.0);
153 monitor->set_latency_threshold(std::chrono::milliseconds(1000));
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;
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;
172 std::cout <<
"\n=== Example 4: Multiple Monitors ===" << std::endl;
175 auto monitor1 = std::make_shared<performance_monitor>(
"monitor1");
176 auto monitor2 = std::make_shared<performance_monitor>(
"monitor2");
178 std::cout <<
"\nMonitor 1 recording metrics..." << std::endl;
179 monitor1->record_counter(
"monitor1_metric", 100.0);
181 std::cout <<
"Monitor 2 recording metrics..." << std::endl;
182 monitor2->record_counter(
"monitor2_metric", 200.0);
185 auto metrics1 = monitor1->collect();
186 auto metrics2 = monitor2->collect();
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;
200 std::cout <<
"\n=== Example 5: Metrics with Tags ===" << std::endl;
202 auto monitor = std::make_shared<performance_monitor>();
207 {
"region",
"us-east-1"},
208 {
"instance",
"i-12345"}
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;
217 auto all_metrics = monitor->get_all_tagged_metrics();
218 std::cout <<
" Total tagged metrics: " << all_metrics.size() << std::endl;
225 std::cout <<
"\n=== Example 6: Monitoring Workflow ===" << std::endl;
227 auto monitor = std::make_shared<performance_monitor>();
228 auto logger = std::make_shared<simple_console_logger>();
230 std::cout <<
"\nSimulating application workload..." << std::endl;
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));
238 std::this_thread::sleep_for(std::chrono::milliseconds(100));
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"));
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");
255 std::cout <<
"\n Workflow completed successfully" << std::endl;
256 std::cout <<
" Logger events: " << logger->get_log_count() << std::endl;
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;
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;
282 }
catch (
const std::exception& e) {
283 std::cerr <<
"Error: " << e.what() << std::endl;
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
std::atomic< size_t > log_count_
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
size_t get_log_count() const
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.
Result pattern type definitions for monitoring system.