Demonstrates how monitoring_system uses common_system interfaces and Result<T> pattern for error handling.
#include <kcenon/common/interfaces/logger_interface.h>
#include <kcenon/common/interfaces/monitoring_interface.h>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <iomanip>
using namespace kcenon::common::interfaces;
private:
public:
kcenon::common::VoidResult
log(log_level level,
const std::string& message)
override {
return kcenon::common::ok();
}
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf;
#ifdef _WIN32
localtime_s(&tm_buf, &time);
#else
localtime_r(&time, &tm_buf);
#endif
std::cout << "[" << std::put_time(&tm_buf, "%H:%M:%S")
<< message << std::endl;
return kcenon::common::ok();
}
kcenon::common::VoidResult
log(
const log_entry& entry)
override {
std::string msg = entry.message;
if (!entry.file.empty()) {
msg += " [" + entry.file + ":" + std::to_string(entry.line) + " " + entry.function + "]";
}
return log(entry.level, msg);
}
return static_cast<int>(level) >=
static_cast<int>(
min_level_);
}
kcenon::common::VoidResult
set_level(log_level level)
override {
return kcenon::common::ok();
}
}
kcenon::common::VoidResult
flush()
override {
std::cout << std::flush;
return kcenon::common::ok();
}
};
std::cout << "\n=== Example 1: Basic Monitoring ===" << std::endl;
auto monitor = std::make_shared<performance_monitor>();
std::cout << "\nRecording metrics..." << std::endl;
auto result1 = monitor->record_counter("requests_total", 100.0);
if (result1.is_ok()) {
std::cout << " Metric 'requests_total' recorded" << std::endl;
}
auto result2 = monitor->record_counter("errors_total", 5.0);
if (result2.is_ok()) {
std::cout << " Metric 'errors_total' recorded" << std::endl;
}
auto metrics = monitor->collect();
if (metrics.is_ok()) {
const auto& snapshot = metrics.value();
std::cout << " Retrieved " << snapshot.metrics.size() << " metrics" << std::endl;
}
}
std::cout << "\n=== Example 2: Error Handling ===" << std::endl;
auto monitor = std::make_shared<performance_monitor>();
auto result = monitor->record_gauge("cpu_usage", 45.5);
if (result.is_ok()) {
std::cout << " Metric recorded successfully" << std::endl;
} else {
const auto& err = result.error();
std::cout << " Error: " << err.message << std::endl;
}
}
std::cout << "\n=== Example 3: Threshold Monitoring ===" << std::endl;
auto monitor = std::make_shared<performance_monitor>();
std::cout << "\nConfiguring thresholds..." << std::endl;
monitor->set_cpu_threshold(80.0);
monitor->set_memory_threshold(90.0);
monitor->set_latency_threshold(std::chrono::milliseconds(1000));
auto threshold_result = monitor->check_thresholds();
if (threshold_result.is_ok()) {
bool exceeded = threshold_result.value();
std::cout << " Thresholds exceeded: " << (exceeded ? "Yes" : "No") << std::endl;
auto thresholds = monitor->get_thresholds();
std::cout << " CPU threshold: " << thresholds.cpu_threshold << "%" << std::endl;
std::cout << " Memory threshold: " << thresholds.memory_threshold << "%" << std::endl;
std::cout << " Latency threshold: " << thresholds.latency_threshold.count() << "ms" << std::endl;
}
}
std::cout << "\n=== Example 4: Multiple Monitors ===" << std::endl;
auto monitor1 = std::make_shared<performance_monitor>("monitor1");
auto monitor2 = std::make_shared<performance_monitor>("monitor2");
std::cout << "\nMonitor 1 recording metrics..." << std::endl;
monitor1->record_counter("monitor1_metric", 100.0);
std::cout << "Monitor 2 recording metrics..." << std::endl;
monitor2->record_counter("monitor2_metric", 200.0);
auto metrics1 = monitor1->collect();
auto metrics2 = monitor2->collect();
if (metrics1.is_ok() && metrics2.is_ok()) {
const auto& snapshot1 = metrics1.value();
const auto& snapshot2 = metrics2.value();
std::cout << " Monitor 1: " << snapshot1.metrics.size() << " metrics" << std::endl;
std::cout << " Monitor 2: " << snapshot2.metrics.size() << " metrics" << std::endl;
}
}
std::cout << "\n=== Example 5: Metrics with Tags ===" << std::endl;
auto monitor = std::make_shared<performance_monitor>();
{"service", "api"},
{"region", "us-east-1"},
{"instance", "i-12345"}
};
auto result = monitor->record_histogram("request_latency", 150.0, tags);
if (result.is_ok()) {
std::cout << " Metric with tags recorded successfully" << std::endl;
}
auto all_metrics = monitor->get_all_tagged_metrics();
std::cout << " Total tagged metrics: " << all_metrics.size() << std::endl;
}
std::cout << "\n=== Example 6: Monitoring Workflow ===" << std::endl;
auto monitor = std::make_shared<performance_monitor>();
auto logger = std::make_shared<simple_console_logger>();
std::cout << "\nSimulating application workload..." << std::endl;
for (int i = 0; i < 5; ++i) {
auto result = monitor->record_gauge("requests", static_cast<double>(i * 10));
if (result.is_ok()) {
logger->log(log_level::info, "Recorded metric: requests = " + std::to_string(i * 10));
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
auto threshold_result = monitor->check_thresholds();
if (threshold_result.is_ok()) {
bool exceeded = threshold_result.value();
logger->log(log_level::info, "Thresholds exceeded: " + std::string(exceeded ? "Yes" : "No"));
}
auto metrics = monitor->collect();
if (metrics.is_ok()) {
const auto& snapshot = metrics.value();
logger->log(log_level::info, "Collected " + std::to_string(snapshot.metrics.size()) + " metrics");
}
std::cout << "\n Workflow completed successfully" << std::endl;
std::cout << " Logger events: " << logger->get_log_count() << std::endl;
}
std::cout << "========================================================" << std::endl;
std::cout << "Monitoring System - Integration Examples" << std::endl;
std::cout << "Using common_system interfaces and Result<T> pattern" << std::endl;
std::cout << "========================================================" << std::endl;
try {
std::cout << "\n========================================================" << std::endl;
std::cout << "All integration examples completed!" << std::endl;
std::cout << "Key Points:" << std::endl;
std::cout << " common_system interfaces used" << std::endl;
std::cout << " Result<T> pattern for error handling" << std::endl;
std::cout << " Interface-based loose coupling" << std::endl;
std::cout << " Comprehensive monitoring" << std::endl;
std::cout << "========================================================" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
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
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.