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

Routes alerts to different notifiers based on criteria. More...

#include <alert_notifiers.h>

Inheritance diagram for kcenon::monitoring::routing_notifier:
Inheritance graph
Collaboration diagram for kcenon::monitoring::routing_notifier:
Collaboration graph

Classes

struct  route
 

Public Types

using route_condition = std::function<bool(const alert&)>
 

Public Member Functions

 routing_notifier (std::string notifier_name)
 Construct routing notifier.
 
void add_route (route_condition condition, std::shared_ptr< alert_notifier > notifier)
 Add a route.
 
void set_default_route (std::shared_ptr< alert_notifier > notifier)
 Add a default route for non-matching alerts.
 
std::string name () const override
 Get notifier name.
 
common::VoidResult notify (const alert &a) override
 Send a notification for an alert.
 
common::VoidResult notify_group (const alert_group &group) override
 Send a notification for an alert group.
 
bool is_ready () const override
 Check if notifier is ready.
 
void route_by_severity (alert_severity severity, std::shared_ptr< alert_notifier > notifier)
 Route by severity.
 
void route_by_label (const std::string &key, const std::string &value, std::shared_ptr< alert_notifier > notifier)
 Route by label.
 
- Public Member Functions inherited from kcenon::monitoring::alert_notifier
virtual ~alert_notifier ()=default
 

Private Attributes

std::string name_
 
std::mutex mutex_
 
std::vector< routeroutes_
 
std::shared_ptr< alert_notifierdefault_route_
 

Detailed Description

Routes alerts to different notifiers based on criteria.

Allows configuring different notification targets based on alert properties like severity or labels.

Definition at line 559 of file alert_notifiers.h.

Member Typedef Documentation

◆ route_condition

Constructor & Destructor Documentation

◆ routing_notifier()

kcenon::monitoring::routing_notifier::routing_notifier ( std::string notifier_name)
inlineexplicit

Construct routing notifier.

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

Definition at line 566 of file alert_notifiers.h.

567 : name_(std::move(notifier_name)) {}

Member Function Documentation

◆ add_route()

void kcenon::monitoring::routing_notifier::add_route ( route_condition condition,
std::shared_ptr< alert_notifier > notifier )
inline

Add a route.

Parameters
conditionCondition for this route
notifierNotifier to use when condition matches
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 574 of file alert_notifiers.h.

575 {
576 std::lock_guard<std::mutex> lock(mutex_);
577 routes_.push_back({std::move(condition), std::move(notifier)});
578 }

References mutex_, and routes_.

Referenced by route_by_label(), and route_by_severity().

Here is the caller graph for this function:

◆ is_ready()

bool kcenon::monitoring::routing_notifier::is_ready ( ) const
inlineoverridevirtual

Check if notifier is ready.

Returns
True if ready to send notifications

Implements kcenon::monitoring::alert_notifier.

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

Definition at line 617 of file alert_notifiers.h.

617 {
618 std::lock_guard<std::mutex> lock(mutex_);
619 return !routes_.empty() || default_route_ != nullptr;
620 }
std::shared_ptr< alert_notifier > default_route_

References default_route_, mutex_, and routes_.

◆ name()

std::string kcenon::monitoring::routing_notifier::name ( ) const
inlineoverridevirtual

Get notifier name.

Returns
Notifier name

Implements kcenon::monitoring::alert_notifier.

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

Definition at line 588 of file alert_notifiers.h.

588{ return name_; }

References name_.

◆ notify()

common::VoidResult kcenon::monitoring::routing_notifier::notify ( const alert & a)
inlineoverridevirtual

Send a notification for an alert.

Parameters
aAlert to notify about
Returns
Result indicating success or failure

Implements kcenon::monitoring::alert_notifier.

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

Definition at line 590 of file alert_notifiers.h.

590 {
591 std::lock_guard<std::mutex> lock(mutex_);
592
593 for (const auto& [condition, notifier] : routes_) {
594 if (condition(a) && notifier && notifier->is_ready()) {
595 return notifier->notify(a);
596 }
597 }
598
599 if (default_route_ && default_route_->is_ready()) {
600 return default_route_->notify(a);
601 }
602
603 return common::ok();
604 }

References default_route_, mutex_, and routes_.

Referenced by notify_group().

Here is the caller graph for this function:

◆ notify_group()

common::VoidResult kcenon::monitoring::routing_notifier::notify_group ( const alert_group & group)
inlineoverridevirtual

Send a notification for an alert group.

Parameters
groupAlert group to notify about
Returns
Result indicating success or failure

Implements kcenon::monitoring::alert_notifier.

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

Definition at line 606 of file alert_notifiers.h.

606 {
607 // Route each alert individually
608 for (const auto& a : group.alerts) {
609 auto result = notify(a);
610 if (!result.is_ok()) {
611 return result;
612 }
613 }
614 return common::ok();
615 }
common::VoidResult notify(const alert &a) override
Send a notification for an alert.

References kcenon::monitoring::alert_group::alerts, and notify().

Here is the call graph for this function:

◆ route_by_label()

void kcenon::monitoring::routing_notifier::route_by_label ( const std::string & key,
const std::string & value,
std::shared_ptr< alert_notifier > notifier )
inline

Route by label.

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

Definition at line 637 of file alert_notifiers.h.

639 {
640 add_route(
641 [key, value](const alert& a) { return a.labels.get(key) == value; },
642 std::move(notifier)
643 );
644 }
void add_route(route_condition condition, std::shared_ptr< alert_notifier > notifier)
Add a route.

References add_route(), kcenon::monitoring::alert_labels::get(), and kcenon::monitoring::alert::labels.

Here is the call graph for this function:

◆ route_by_severity()

void kcenon::monitoring::routing_notifier::route_by_severity ( alert_severity severity,
std::shared_ptr< alert_notifier > notifier )
inline

Route by severity.

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

Definition at line 626 of file alert_notifiers.h.

627 {
628 add_route(
629 [severity](const alert& a) { return a.severity == severity; },
630 std::move(notifier)
631 );
632 }

References add_route(), and kcenon::monitoring::alert::severity.

Here is the call graph for this function:

◆ set_default_route()

void kcenon::monitoring::routing_notifier::set_default_route ( std::shared_ptr< alert_notifier > notifier)
inline

Add a default route for non-matching alerts.

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

Definition at line 583 of file alert_notifiers.h.

583 {
584 std::lock_guard<std::mutex> lock(mutex_);
585 default_route_ = std::move(notifier);
586 }

References default_route_, and mutex_.

Member Data Documentation

◆ default_route_

std::shared_ptr<alert_notifier> kcenon::monitoring::routing_notifier::default_route_
private

◆ mutex_

std::mutex kcenon::monitoring::routing_notifier::mutex_
mutableprivate

◆ name_

std::string kcenon::monitoring::routing_notifier::name_
private

◆ routes_

std::vector<route> kcenon::monitoring::routing_notifier::routes_
private

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