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

Monitor factory and provider pattern examples for Phase 4. 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 <unordered_map>
#include <mutex>
#include <vector>
Include dependency graph for monitor_factory_pattern_example.cpp:

Go to the source code of this file.

Classes

class  monitor_factory
 Monitor factory implementing singleton pattern with DI. More...
 
class  example_logger
 Simple logger for examples. More...
 
class  aggregating_monitor
 Example 6: Aggregating monitor pattern. More...
 

Functions

void example_1_basic_factory ()
 Example 1: Basic factory pattern.
 
void example_2_named_monitors ()
 Example 2: Named monitors via factory.
 
void example_3_factory_with_logger ()
 Example 3: Factory with shared logger.
 
void example_4_monitor_reuse ()
 Example 4: Monitor reuse via factory.
 
void example_5_provider_interface ()
 Example 5: Provider interface usage.
 
void example_6_aggregating_pattern ()
 
void example_7_factory_lifecycle ()
 Example 7: Factory cleanup and reset.
 
int main ()
 

Detailed Description

Monitor factory and provider pattern examples for Phase 4.

Definition in file monitor_factory_pattern_example.cpp.

Function Documentation

◆ example_1_basic_factory()

void example_1_basic_factory ( )

Example 1: Basic factory pattern.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 163 of file monitor_factory_pattern_example.cpp.

163 {
164 std::cout << "\n=== Example 1: Basic Factory Pattern ===" << std::endl;
165
166 auto factory = monitor_factory::instance();
167
168 std::cout << "Getting default monitor from factory..." << std::endl;
169 auto monitor = factory->get_monitor();
170
171 if (monitor) {
172 std::cout << "✓ Obtained monitor instance" << std::endl;
173
174 monitor->record_metric("test_metric", 42.0);
175
176 auto metrics = monitor->get_metrics();
177 if (metrics) {
178 std::cout << "✓ Monitor has " << metrics.value().metrics.size()
179 << " metrics" << std::endl;
180 }
181 }
182}
static std::shared_ptr< monitor_factory > instance()

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_2_named_monitors()

void example_2_named_monitors ( )

Example 2: Named monitors via factory.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 187 of file monitor_factory_pattern_example.cpp.

187 {
188 std::cout << "\n=== Example 2: Named Monitors ===" << std::endl;
189
190 auto factory = monitor_factory::instance();
191
192 // Create named monitors
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");
196
197 std::cout << "Created 3 named monitors" << std::endl;
198
199 // Use monitors
200 web_monitor->record_metric("requests", 1000.0);
201 db_monitor->record_metric("queries", 500.0);
202 cache_monitor->record_metric("hits", 750.0);
203
204 // List all monitors
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;
209 }
210}

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_3_factory_with_logger()

void example_3_factory_with_logger ( )

Example 3: Factory with shared logger.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 215 of file monitor_factory_pattern_example.cpp.

215 {
216 std::cout << "\n=== Example 3: Factory with Shared Logger ===" << std::endl;
217
218 auto factory = monitor_factory::instance();
219 auto logger = std::make_shared<example_logger>("FACTORY");
220
221 // Set shared logger for all monitors
222 factory->set_shared_logger(logger);
223
224 std::cout << "Shared logger configured for factory" << std::endl;
225
226 // All new monitors will automatically use the shared logger
227 auto monitor1 = factory->create_monitor("service_a");
228 auto monitor2 = factory->create_monitor("service_b");
229
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);
233
234 std::cout << "\nShared logger received " << logger->count()
235 << " messages from all monitors" << std::endl;
236}

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_4_monitor_reuse()

void example_4_monitor_reuse ( )

Example 4: Monitor reuse via factory.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 241 of file monitor_factory_pattern_example.cpp.

241 {
242 std::cout << "\n=== Example 4: Monitor Reuse ===" << std::endl;
243
244 auto factory = monitor_factory::instance();
245
246 // Create monitor with specific name
247 auto monitor1 = factory->create_monitor("shared_monitor");
248 monitor1->record_metric("counter", 1.0);
249
250 // Get same monitor by name (reuse)
251 auto monitor2 = factory->create_monitor("shared_monitor");
252
253 // They are the same instance
254 std::cout << "Monitor instances "
255 << (monitor1 == monitor2 ? "identical ✓" : "different ✗")
256 << std::endl;
257
258 monitor2->record_metric("counter", 2.0);
259
260 // Check metrics
261 auto metrics = monitor1->get_metrics();
262 if (metrics) {
263 std::cout << "Shared monitor has " << metrics.value().metrics.size()
264 << " metrics (accumulated from both uses)" << std::endl;
265 }
266}

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_5_provider_interface()

void example_5_provider_interface ( )

Example 5: Provider interface usage.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 271 of file monitor_factory_pattern_example.cpp.

271 {
272 std::cout << "\n=== Example 5: IMonitorProvider Interface ===" << std::endl;
273
274 // Use factory through IMonitorProvider interface
275 std::shared_ptr<IMonitorProvider> provider = monitor_factory::instance();
276
277 std::cout << "Using factory via IMonitorProvider interface" << std::endl;
278
279 auto monitor = provider->get_monitor();
280 if (monitor) {
281 std::cout << "✓ Retrieved monitor through provider interface" << std::endl;
282
283 monitor->record_metric("provider_test", 99.0);
284
285 auto health = monitor->check_health();
286 if (health) {
287 std::cout << "✓ Monitor health: "
288 << to_string(health.value().status) << std::endl;
289 }
290 }
291
292 auto named = provider->create_monitor("provider_created");
293 if (named) {
294 std::cout << "✓ Created named monitor through provider" << std::endl;
295 }
296}
auto to_string(plugin_load_error error) -> std::string
Convert plugin_load_error to string.

References monitor_factory::instance(), and kcenon::monitoring::to_string().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_6_aggregating_pattern()

void example_6_aggregating_pattern ( )
Examples
monitor_factory_pattern_example.cpp.

Definition at line 374 of file monitor_factory_pattern_example.cpp.

374 {
375 std::cout << "\n=== Example 6: Aggregating Monitor Pattern ===" << std::endl;
376
377 auto factory = monitor_factory::instance();
378 auto aggregator = std::make_shared<aggregating_monitor>();
379
380 // Create multiple monitors and add to aggregator
381 auto mon1 = factory->create_monitor("service_1");
382 auto mon2 = factory->create_monitor("service_2");
383 auto mon3 = factory->create_monitor("service_3");
384
385 aggregator->add_monitor(mon1);
386 aggregator->add_monitor(mon2);
387 aggregator->add_monitor(mon3);
388
389 std::cout << "Aggregator managing " << aggregator->monitor_count()
390 << " monitors" << std::endl;
391
392 // Record to aggregator - broadcasts to all
393 aggregator->record_metric("broadcast_metric", 100.0);
394
395 std::cout << "\nMetric broadcasted to all monitors" << std::endl;
396
397 // Get combined metrics
398 auto metrics = aggregator->get_metrics();
399 if (metrics) {
400 std::cout << "Combined metrics count: "
401 << metrics.value().metrics.size() << std::endl;
402 }
403}

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ example_7_factory_lifecycle()

void example_7_factory_lifecycle ( )

Example 7: Factory cleanup and reset.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 408 of file monitor_factory_pattern_example.cpp.

408 {
409 std::cout << "\n=== Example 7: Factory Lifecycle Management ===" << std::endl;
410
411 auto factory = monitor_factory::instance();
412
413 std::cout << "Initial monitor count: " << factory->monitor_count() << std::endl;
414
415 // Create some monitors
416 factory->create_monitor("temp_1");
417 factory->create_monitor("temp_2");
418
419 std::cout << "After creation: " << factory->monitor_count() << std::endl;
420
421 // Reset factory
422 factory->reset();
423
424 std::cout << "After reset: " << factory->monitor_count() << std::endl;
425 std::cout << "✓ Factory lifecycle managed successfully" << std::endl;
426}

References monitor_factory::instance().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 428 of file monitor_factory_pattern_example.cpp.

428 {
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;
433
434 try {
442
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;
453
454 } catch (const std::exception& e) {
455 std::cerr << "Error: " << e.what() << std::endl;
456 return 1;
457 }
458
459 return 0;
460}
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.

References example_1_basic_factory(), example_2_named_monitors(), example_3_factory_with_logger(), example_4_monitor_reuse(), example_5_provider_interface(), example_6_aggregating_pattern(), and example_7_factory_lifecycle().

Here is the call graph for this function: