Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::rule_registry Class Reference

Dynamic registry for alert rules with hot-reload support. More...

#include <alert_config.h>

Collaboration diagram for kcenon::monitoring::rule_registry:
Collaboration graph

Public Types

using rule_change_callback
 

Public Member Functions

common::VoidResult register_rule (std::shared_ptr< alert_rule > rule)
 Register a rule.
 
common::VoidResult unregister_rule (const std::string &name)
 Unregister a rule.
 
std::shared_ptr< alert_ruleget_rule (const std::string &name) const
 Get a rule by name.
 
std::vector< std::shared_ptr< alert_rule > > get_all_rules () const
 Get all registered rules.
 
std::vector< std::shared_ptr< alert_rule > > get_rules_by_group (const std::string &group) const
 Get rules in a group.
 
size_t rule_count () const
 Get rule count.
 
void on_rule_change (rule_change_callback callback)
 Register callback for rule changes.
 
common::Result< size_t > load_definitions (const std::vector< rule_definition > &definitions)
 Load rules from definitions.
 
void clear ()
 Clear all rules.
 

Private Attributes

std::mutex mutex_
 
std::unordered_map< std::string, std::shared_ptr< alert_rule > > rules_
 
std::vector< rule_change_callbackchange_callbacks_
 

Detailed Description

Dynamic registry for alert rules with hot-reload support.

Manages a collection of alert rules and supports runtime updates without service interruption.

Definition at line 387 of file alert_config.h.

Member Typedef Documentation

◆ rule_change_callback

Initial value:
std::function<void(
const std::string& rule_name,
const std::shared_ptr<alert_rule>& rule,
bool is_removal)>
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 389 of file alert_config.h.

Member Function Documentation

◆ clear()

void kcenon::monitoring::rule_registry::clear ( )
inline

Clear all rules.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 530 of file alert_config.h.

530 {
531 std::lock_guard<std::mutex> lock(mutex_);
532
533 for (const auto& [name, rule] : rules_) {
534 for (const auto& callback : change_callbacks_) {
535 callback(name, rule, true);
536 }
537 }
538
539 rules_.clear();
540 }
std::unordered_map< std::string, std::shared_ptr< alert_rule > > rules_
std::vector< rule_change_callback > change_callbacks_

References change_callbacks_, mutex_, and rules_.

◆ get_all_rules()

std::vector< std::shared_ptr< alert_rule > > kcenon::monitoring::rule_registry::get_all_rules ( ) const
inline

Get all registered rules.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 454 of file alert_config.h.

454 {
455 std::lock_guard<std::mutex> lock(mutex_);
456 std::vector<std::shared_ptr<alert_rule>> result;
457 result.reserve(rules_.size());
458 for (const auto& [name, rule] : rules_) {
459 result.push_back(rule);
460 }
461 return result;
462 }

References mutex_, and rules_.

◆ get_rule()

std::shared_ptr< alert_rule > kcenon::monitoring::rule_registry::get_rule ( const std::string & name) const
inline

Get a rule by name.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 445 of file alert_config.h.

445 {
446 std::lock_guard<std::mutex> lock(mutex_);
447 auto it = rules_.find(name);
448 return it != rules_.end() ? it->second : nullptr;
449 }

References mutex_, and rules_.

◆ get_rules_by_group()

std::vector< std::shared_ptr< alert_rule > > kcenon::monitoring::rule_registry::get_rules_by_group ( const std::string & group) const
inline

Get rules in a group.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 467 of file alert_config.h.

468 {
469 std::lock_guard<std::mutex> lock(mutex_);
470 std::vector<std::shared_ptr<alert_rule>> result;
471 for (const auto& [name, rule] : rules_) {
472 if (rule->group() == group) {
473 result.push_back(rule);
474 }
475 }
476 return result;
477 }

References mutex_, and rules_.

◆ load_definitions()

common::Result< size_t > kcenon::monitoring::rule_registry::load_definitions ( const std::vector< rule_definition > & definitions)
inline

Load rules from definitions.

Parameters
definitionsVector of rule definitions
Returns
Result with count of successfully loaded rules
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 500 of file alert_config.h.

500 {
501 size_t loaded = 0;
502 std::vector<std::string> errors;
503
504 for (const auto& def : definitions) {
505 auto rule_result = rule_builder::build(def);
506 if (rule_result.is_ok()) {
507 auto reg_result = register_rule(rule_result.value());
508 if (reg_result.is_ok()) {
509 ++loaded;
510 } else {
511 errors.push_back(def.name + ": " + reg_result.error().message);
512 }
513 } else {
514 errors.push_back(def.name + ": " + rule_result.error().message);
515 }
516 }
517
518 if (!errors.empty() && loaded == 0) {
519 return common::make_error<size_t>(
521 "Failed to load any rules: " + errors.front());
522 }
523
524 return common::ok(loaded);
525 }
static common::Result< std::shared_ptr< alert_rule > > build(const rule_definition &def)
Build alert_rule from definition.
common::VoidResult register_rule(std::shared_ptr< alert_rule > rule)
Register a rule.

References kcenon::monitoring::rule_builder::build(), kcenon::monitoring::configuration_parse_error, and register_rule().

Here is the call graph for this function:

◆ on_rule_change()

void kcenon::monitoring::rule_registry::on_rule_change ( rule_change_callback callback)
inline

Register callback for rule changes.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 490 of file alert_config.h.

490 {
491 std::lock_guard<std::mutex> lock(mutex_);
492 change_callbacks_.push_back(std::move(callback));
493 }

References change_callbacks_, and mutex_.

◆ register_rule()

common::VoidResult kcenon::monitoring::rule_registry::register_rule ( std::shared_ptr< alert_rule > rule)
inline

Register a rule.

Parameters
ruleRule to register
Returns
Result indicating success or failure
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 399 of file alert_config.h.

399 {
400 if (!rule) {
401 return common::VoidResult::err(error_info(monitoring_error_code::invalid_argument, "Rule cannot be null").to_common_error());
402 }
403
404 std::lock_guard<std::mutex> lock(mutex_);
405
406 std::string name = rule->name();
407 rules_[name] = rule;
408
409 // Notify listeners
410 for (const auto& callback : change_callbacks_) {
411 callback(name, rule, false);
412 }
413
414 return common::ok();
415 }

References change_callbacks_, kcenon::monitoring::invalid_argument, mutex_, and rules_.

Referenced by load_definitions().

Here is the caller graph for this function:

◆ rule_count()

size_t kcenon::monitoring::rule_registry::rule_count ( ) const
inline

Get rule count.

Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 482 of file alert_config.h.

482 {
483 std::lock_guard<std::mutex> lock(mutex_);
484 return rules_.size();
485 }

References mutex_, and rules_.

◆ unregister_rule()

common::VoidResult kcenon::monitoring::rule_registry::unregister_rule ( const std::string & name)
inline

Unregister a rule.

Parameters
nameRule name
Returns
Result indicating success or failure
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 422 of file alert_config.h.

422 {
423 std::lock_guard<std::mutex> lock(mutex_);
424
425 auto it = rules_.find(name);
426 if (it == rules_.end()) {
427 return common::VoidResult::err(static_cast<int>(monitoring_error_code::not_found),
428 "Rule not found: " + name);
429 }
430
431 auto rule = it->second;
432 rules_.erase(it);
433
434 // Notify listeners
435 for (const auto& callback : change_callbacks_) {
436 callback(name, rule, true);
437 }
438
439 return common::ok();
440 }

References change_callbacks_, mutex_, kcenon::monitoring::not_found, and rules_.

Member Data Documentation

◆ change_callbacks_

std::vector<rule_change_callback> kcenon::monitoring::rule_registry::change_callbacks_
private

◆ mutex_

◆ rules_

std::unordered_map<std::string, std::shared_ptr<alert_rule> > kcenon::monitoring::rule_registry::rules_
private

The documentation for this class was generated from the following file: