16#include <kcenon/common/interfaces/logger_interface.h>
17#include <kcenon/common/interfaces/monitoring_interface.h>
20#include <unordered_map>
25using namespace common::interfaces;
43 static std::shared_ptr<monitor_factory>
instance() {
78 auto monitor = std::make_shared<performance_monitor>();
85 std::vector<std::string> names;
89 names.push_back(name);
123 common::VoidResult
log(log_level level,
const std::string& message)
override {
125 << message << std::endl;
130 common::VoidResult
log(log_level level,
const std::string& message,
131 const std::string&,
int,
const std::string&)
override {
132 return log(level, message);
135 common::VoidResult
log(
const log_entry& entry)
override {
136 return log(entry.level, entry.message);
140 return static_cast<int>(level) >=
static_cast<int>(
min_level_);
143 common::VoidResult
set_level(log_level level)
override {
152 common::VoidResult
flush()
override {
153 std::cout << std::flush;
164 std::cout <<
"\n=== Example 1: Basic Factory Pattern ===" << std::endl;
168 std::cout <<
"Getting default monitor from factory..." << std::endl;
169 auto monitor = factory->get_monitor();
172 std::cout <<
"✓ Obtained monitor instance" << std::endl;
174 monitor->record_metric(
"test_metric", 42.0);
176 auto metrics = monitor->get_metrics();
178 std::cout <<
"✓ Monitor has " << metrics.value().metrics.size()
179 <<
" metrics" << std::endl;
188 std::cout <<
"\n=== Example 2: Named Monitors ===" << std::endl;
193 auto web_monitor = factory->create_monitor(
"web_server");
194 auto db_monitor = factory->create_monitor(
"database");
195 auto cache_monitor = factory->create_monitor(
"cache");
197 std::cout <<
"Created 3 named monitors" << std::endl;
200 web_monitor->record_metric(
"requests", 1000.0);
201 db_monitor->record_metric(
"queries", 500.0);
202 cache_monitor->record_metric(
"hits", 750.0);
205 auto names = factory->list_monitors();
206 std::cout <<
"\nRegistered monitors (" << names.size() <<
"):" << std::endl;
207 for (
const auto& name : names) {
208 std::cout <<
" - " << name << std::endl;
216 std::cout <<
"\n=== Example 3: Factory with Shared Logger ===" << std::endl;
219 auto logger = std::make_shared<example_logger>(
"FACTORY");
222 factory->set_shared_logger(logger);
224 std::cout <<
"Shared logger configured for factory" << std::endl;
227 auto monitor1 = factory->create_monitor(
"service_a");
228 auto monitor2 = factory->create_monitor(
"service_b");
230 std::cout <<
"\nRecording metrics with shared logger..." << std::endl;
231 monitor1->record_metric(
"metric_a", 10.0);
232 monitor2->record_metric(
"metric_b", 20.0);
234 std::cout <<
"\nShared logger received " << logger->count()
235 <<
" messages from all monitors" << std::endl;
242 std::cout <<
"\n=== Example 4: Monitor Reuse ===" << std::endl;
247 auto monitor1 = factory->create_monitor(
"shared_monitor");
248 monitor1->record_metric(
"counter", 1.0);
251 auto monitor2 = factory->create_monitor(
"shared_monitor");
254 std::cout <<
"Monitor instances "
255 << (monitor1 == monitor2 ?
"identical ✓" :
"different ✗")
258 monitor2->record_metric(
"counter", 2.0);
261 auto metrics = monitor1->get_metrics();
263 std::cout <<
"Shared monitor has " << metrics.value().metrics.size()
264 <<
" metrics (accumulated from both uses)" << std::endl;
272 std::cout <<
"\n=== Example 5: IMonitorProvider Interface ===" << std::endl;
277 std::cout <<
"Using factory via IMonitorProvider interface" << std::endl;
279 auto monitor = provider->get_monitor();
281 std::cout <<
"✓ Retrieved monitor through provider interface" << std::endl;
283 monitor->record_metric(
"provider_test", 99.0);
285 auto health = monitor->check_health();
287 std::cout <<
"✓ Monitor health: "
288 <<
to_string(health.value().status) << std::endl;
292 auto named = provider->create_monitor(
"provider_created");
294 std::cout <<
"✓ Created named monitor through provider" << std::endl;
308 std::lock_guard<std::mutex> lock(
mutex_);
312 common::VoidResult
record_metric(
const std::string& name,
double value)
override {
313 std::lock_guard<std::mutex> lock(
mutex_);
315 monitor->record_metric(name, value);
321 const std::string& name,
323 const std::unordered_map<std::string, std::string>& tags)
override
325 std::lock_guard<std::mutex> lock(
mutex_);
327 monitor->record_metric(name, value, tags);
333 std::lock_guard<std::mutex> lock(
mutex_);
336 combined.
source_id =
"aggregating_monitor";
337 combined.
capture_time = std::chrono::system_clock::now();
340 auto result = monitor->get_metrics();
342 for (
const auto&
metric : result.
value().metrics) {
348 return common::ok(std::move(combined));
353 result.
timestamp = std::chrono::system_clock::now();
354 result.
status = health_status::healthy;
355 result.
message =
"Aggregating monitor";
357 return common::ok(std::move(result));
360 common::VoidResult
reset()
override {
361 std::lock_guard<std::mutex> lock(
mutex_);
369 std::lock_guard<std::mutex> lock(
mutex_);
375 std::cout <<
"\n=== Example 6: Aggregating Monitor Pattern ===" << std::endl;
378 auto aggregator = std::make_shared<aggregating_monitor>();
381 auto mon1 = factory->create_monitor(
"service_1");
382 auto mon2 = factory->create_monitor(
"service_2");
383 auto mon3 = factory->create_monitor(
"service_3");
385 aggregator->add_monitor(mon1);
386 aggregator->add_monitor(mon2);
387 aggregator->add_monitor(mon3);
389 std::cout <<
"Aggregator managing " << aggregator->monitor_count()
390 <<
" monitors" << std::endl;
393 aggregator->record_metric(
"broadcast_metric", 100.0);
395 std::cout <<
"\nMetric broadcasted to all monitors" << std::endl;
398 auto metrics = aggregator->get_metrics();
400 std::cout <<
"Combined metrics count: "
401 << metrics.value().metrics.size() << std::endl;
409 std::cout <<
"\n=== Example 7: Factory Lifecycle Management ===" << std::endl;
413 std::cout <<
"Initial monitor count: " << factory->monitor_count() << std::endl;
416 factory->create_monitor(
"temp_1");
417 factory->create_monitor(
"temp_2");
419 std::cout <<
"After creation: " << factory->monitor_count() << std::endl;
424 std::cout <<
"After reset: " << factory->monitor_count() << std::endl;
425 std::cout <<
"✓ Factory lifecycle managed successfully" << std::endl;
429 std::cout <<
"========================================================" << std::endl;
430 std::cout <<
"Monitor Factory Pattern Examples (Phase 4)" << std::endl;
431 std::cout <<
"Advanced DI Patterns for Monitoring System" << std::endl;
432 std::cout <<
"========================================================" << std::endl;
443 std::cout <<
"\n========================================================" << std::endl;
444 std::cout <<
"All factory pattern examples completed!" << std::endl;
445 std::cout <<
"Key Patterns Demonstrated:" << std::endl;
446 std::cout <<
" ✓ Singleton factory pattern" << std::endl;
447 std::cout <<
" ✓ Named monitor management" << std::endl;
448 std::cout <<
" ✓ Shared logger injection" << std::endl;
449 std::cout <<
" ✓ Monitor reuse and lifecycle" << std::endl;
450 std::cout <<
" ✓ IMonitorProvider interface" << std::endl;
451 std::cout <<
" ✓ Aggregating monitor pattern" << std::endl;
452 std::cout <<
"========================================================" << std::endl;
454 }
catch (
const std::exception& e) {
455 std::cerr <<
"Error: " << e.what() << std::endl;
Example 6: Aggregating monitor pattern.
common::VoidResult record_metric(const std::string &name, double value) override
void add_monitor(std::shared_ptr< IMonitor > monitor)
common::Result< metrics_snapshot > get_metrics() override
common::VoidResult record_metric(const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags) override
common::Result< health_check_result > check_health() override
std::vector< std::shared_ptr< IMonitor > > monitors_
size_t monitor_count() const
common::VoidResult reset() override
Simple logger for examples.
log_level get_level() const override
common::VoidResult log(log_level level, const std::string &message, const std::string &, int, const std::string &) override
bool is_enabled(log_level level) const override
common::VoidResult set_level(log_level level) override
example_logger(const std::string &prefix="LOG")
common::VoidResult flush() override
std::atomic< size_t > count_
common::VoidResult log(log_level level, const std::string &message) override
common::VoidResult log(const log_entry &entry) override
Monitor factory implementing singleton pattern with DI.
std::unordered_map< std::string, std::shared_ptr< IMonitor > > named_monitors_
std::mutex factory_mutex_
std::shared_ptr< IMonitor > create_monitor(const std::string &name) override
static std::shared_ptr< monitor_factory > instance_
monitor_factory()=default
void set_shared_logger(std::shared_ptr< ILogger > logger)
std::vector< std::string > list_monitors() const
std::shared_ptr< ILogger > shared_logger_
static std::mutex instance_mutex_
std::shared_ptr< IMonitor > default_monitor_
std::shared_ptr< IMonitor > get_monitor() override
size_t monitor_count() const
static std::shared_ptr< monitor_factory > instance()
void example_2_named_monitors()
Example 2: Named monitors via factory.
void example_5_provider_interface()
Example 5: Provider interface usage.
void example_6_aggregating_pattern()
void example_4_monitor_reuse()
Example 4: Monitor reuse via factory.
void example_3_factory_with_logger()
Example 3: Factory with shared logger.
void example_1_basic_factory()
Example 1: Basic factory pattern.
void example_7_factory_lifecycle()
Example 7: Factory cleanup and reset.
auto to_string(plugin_load_error error) -> std::string
Convert plugin_load_error to string.
Result pattern type definitions for monitoring system.
Result of a health check operation.
std::chrono::system_clock::time_point timestamp
Basic metric structure for interface compatibility.
std::variant< double, int64_t, std::string > value
Complete snapshot of metrics at a point in time.
std::chrono::system_clock::time_point capture_time
std::vector< metric_value > metrics