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

Defines conditions and behavior for alert triggering. More...

#include <alert_rule.h>

Collaboration diagram for kcenon::monitoring::alert_rule:
Collaboration graph

Public Member Functions

 alert_rule (std::string name)
 Construct an alert rule with a name.
 
const std::string & name () const
 Get rule name.
 
const std::string & group () const
 Get rule group.
 
alert_ruleset_group (std::string group_name)
 Set rule group.
 
alert_severity severity () const
 Get alert severity.
 
alert_ruleset_severity (alert_severity sev)
 Set alert severity.
 
const alert_labelslabels () const
 Get labels.
 
alert_ruleadd_label (const std::string &key, const std::string &value)
 Add a label.
 
const alert_annotationsannotations () const
 Get annotations.
 
alert_ruleset_summary (std::string summary)
 Set alert summary.
 
alert_ruleset_description (std::string description)
 Set alert description.
 
alert_ruleset_runbook_url (std::string url)
 Set runbook URL.
 
const alert_rule_configconfig () const
 Get configuration.
 
alert_ruleset_evaluation_interval (std::chrono::milliseconds interval)
 Set evaluation interval.
 
alert_ruleset_for_duration (std::chrono::milliseconds duration)
 Set for duration (pending time before firing)
 
alert_ruleset_repeat_interval (std::chrono::milliseconds interval)
 Set notification repeat interval.
 
bool is_enabled () const
 Check if rule is enabled.
 
alert_ruleset_enabled (bool enabled)
 Enable or disable rule.
 
alert_ruleset_trigger (std::shared_ptr< alert_trigger > trigger)
 Set the trigger for this rule.
 
std::shared_ptr< alert_triggertrigger () const
 Get the trigger.
 
alert create_alert (double value) const
 Create an alert from this rule.
 
common::VoidResult validate () const
 Validate rule configuration.
 
const std::string & metric_name () const
 Get metric name to monitor.
 
alert_ruleset_metric_name (std::string name)
 Set metric name to monitor.
 

Private Attributes

std::string name_
 
std::string group_
 
std::string metric_name_
 
alert_severity severity_ = alert_severity::warning
 
alert_labels labels_
 
alert_annotations annotations_
 
alert_rule_config config_
 
bool enabled_
 
std::shared_ptr< alert_triggertrigger_
 

Detailed Description

Defines conditions and behavior for alert triggering.

An alert rule encapsulates the logic for when alerts should be triggered, how they should be labeled and annotated, and where notifications should be routed.

Thread Safety:
This class is thread-safe for read operations after construction. Trigger evaluation may require external synchronization depending on trigger implementation.
Examples
alert_triggers_example.cpp.

Definition at line 82 of file alert_rule.h.

Constructor & Destructor Documentation

◆ alert_rule()

kcenon::monitoring::alert_rule::alert_rule ( std::string name)
inlineexplicit

Construct an alert rule with a name.

Parameters
nameUnique rule name
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 88 of file alert_rule.h.

89 : name_(std::move(name))
90 , enabled_(true) {}
const std::string & name() const
Get rule name.
Definition alert_rule.h:96

Member Function Documentation

◆ add_label()

alert_rule & kcenon::monitoring::alert_rule::add_label ( const std::string & key,
const std::string & value )
inline

Add a label.

Parameters
keyLabel key
valueLabel value
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 142 of file alert_rule.h.

142 {
143 labels_.set(key, value);
144 return *this;
145 }
void set(const std::string &key, const std::string &value)
Add or update a label.

References labels_, and kcenon::monitoring::alert_labels::set().

Referenced by main(), TEST(), and TEST().

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

◆ annotations()

const alert_annotations & kcenon::monitoring::alert_rule::annotations ( ) const
inline

Get annotations.

Returns
Alert annotations
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 151 of file alert_rule.h.

151{ return annotations_; }
alert_annotations annotations_
Definition alert_rule.h:311

References annotations_.

Referenced by TEST().

Here is the caller graph for this function:

◆ config()

const alert_rule_config & kcenon::monitoring::alert_rule::config ( ) const
inline

Get configuration.

Returns
Rule configuration
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 187 of file alert_rule.h.

187{ return config_; }

References config_.

◆ create_alert()

alert kcenon::monitoring::alert_rule::create_alert ( double value) const
inline

Create an alert from this rule.

Parameters
valueCurrent metric value
Returns
Created alert
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 256 of file alert_rule.h.

256 {
257 alert a(name_, labels_);
258 a.annotations = annotations_;
259 a.severity = severity_;
260 a.value = value;
261 a.rule_name = name_;
262 a.group_key = group_.empty() ? name_ : group_;
263 return a;
264 }

References kcenon::monitoring::alert::annotations, annotations_, group_, kcenon::monitoring::alert::group_key, labels_, name_, kcenon::monitoring::alert::rule_name, kcenon::monitoring::alert::severity, severity_, and kcenon::monitoring::alert::value.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ group()

const std::string & kcenon::monitoring::alert_rule::group ( ) const
inline

Get rule group.

Returns
Rule group name
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 102 of file alert_rule.h.

102{ return group_; }

References group_.

Referenced by TEST().

Here is the caller graph for this function:

◆ is_enabled()

bool kcenon::monitoring::alert_rule::is_enabled ( ) const
inline

Check if rule is enabled.

Returns
True if enabled
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 223 of file alert_rule.h.

223{ return enabled_; }

References enabled_.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ labels()

const alert_labels & kcenon::monitoring::alert_rule::labels ( ) const
inline

Get labels.

Returns
Alert labels
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 134 of file alert_rule.h.

134{ return labels_; }

References labels_.

Referenced by TEST().

Here is the caller graph for this function:

◆ metric_name()

const std::string & kcenon::monitoring::alert_rule::metric_name ( ) const
inline

Get metric name to monitor.

Returns
Metric name
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 293 of file alert_rule.h.

293{ return metric_name_; }

References metric_name_.

Referenced by TEST().

Here is the caller graph for this function:

◆ name()

const std::string & kcenon::monitoring::alert_rule::name ( ) const
inline

Get rule name.

Returns
Rule name
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 96 of file alert_rule.h.

96{ return name_; }

References name_.

Referenced by main(), set_metric_name(), and TEST().

Here is the caller graph for this function:

◆ set_description()

alert_rule & kcenon::monitoring::alert_rule::set_description ( std::string description)
inline

Set alert description.

Parameters
descriptionDescription text
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 168 of file alert_rule.h.

168 {
169 annotations_.description = std::move(description);
170 return *this;
171 }
std::string description
Detailed description.

References annotations_, and kcenon::monitoring::alert_annotations::description.

Referenced by main(), and TEST().

Here is the caller graph for this function:

◆ set_enabled()

alert_rule & kcenon::monitoring::alert_rule::set_enabled ( bool enabled)
inline

Enable or disable rule.

Parameters
enabledEnable state
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 230 of file alert_rule.h.

230 {
231 enabled_ = enabled;
232 return *this;
233 }

References enabled_.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ set_evaluation_interval()

alert_rule & kcenon::monitoring::alert_rule::set_evaluation_interval ( std::chrono::milliseconds interval)
inline

Set evaluation interval.

Parameters
intervalEvaluation interval
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 194 of file alert_rule.h.

194 {
195 config_.evaluation_interval = interval;
196 return *this;
197 }
std::chrono::milliseconds evaluation_interval
How often to evaluate.
Definition alert_rule.h:37

References config_, and kcenon::monitoring::alert_rule_config::evaluation_interval.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ set_for_duration()

alert_rule & kcenon::monitoring::alert_rule::set_for_duration ( std::chrono::milliseconds duration)
inline

Set for duration (pending time before firing)

Parameters
durationDuration before firing
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 204 of file alert_rule.h.

204 {
205 config_.for_duration = duration;
206 return *this;
207 }
std::chrono::milliseconds for_duration
Duration before firing.
Definition alert_rule.h:38

References config_, and kcenon::monitoring::alert_rule_config::for_duration.

Referenced by TEST().

Here is the caller graph for this function:

◆ set_group()

alert_rule & kcenon::monitoring::alert_rule::set_group ( std::string group_name)
inline

Set rule group.

Parameters
group_nameGroup name
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 109 of file alert_rule.h.

109 {
110 group_ = std::move(group_name);
111 return *this;
112 }

References group_.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ set_metric_name()

alert_rule & kcenon::monitoring::alert_rule::set_metric_name ( std::string name)
inline

Set metric name to monitor.

Parameters
nameMetric name
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 300 of file alert_rule.h.

300 {
301 metric_name_ = std::move(name);
302 return *this;
303 }

References metric_name_, and name().

Referenced by main(), TEST(), and TEST().

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

◆ set_repeat_interval()

alert_rule & kcenon::monitoring::alert_rule::set_repeat_interval ( std::chrono::milliseconds interval)
inline

Set notification repeat interval.

Parameters
intervalRepeat interval
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 214 of file alert_rule.h.

214 {
215 config_.repeat_interval = interval;
216 return *this;
217 }
std::chrono::milliseconds repeat_interval
Notification repeat interval.
Definition alert_rule.h:39

References config_, and kcenon::monitoring::alert_rule_config::repeat_interval.

Referenced by TEST().

Here is the caller graph for this function:

◆ set_runbook_url()

alert_rule & kcenon::monitoring::alert_rule::set_runbook_url ( std::string url)
inline

Set runbook URL.

Parameters
urlRunbook URL
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 178 of file alert_rule.h.

178 {
179 annotations_.runbook_url = std::move(url);
180 return *this;
181 }
std::optional< std::string > runbook_url
Link to runbook.

References annotations_, and kcenon::monitoring::alert_annotations::runbook_url.

Referenced by TEST().

Here is the caller graph for this function:

◆ set_severity()

alert_rule & kcenon::monitoring::alert_rule::set_severity ( alert_severity sev)
inline

Set alert severity.

Parameters
sevSeverity level
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 125 of file alert_rule.h.

125 {
126 severity_ = sev;
127 return *this;
128 }

References severity_.

Referenced by main(), TEST(), and TEST().

Here is the caller graph for this function:

◆ set_summary()

alert_rule & kcenon::monitoring::alert_rule::set_summary ( std::string summary)
inline

Set alert summary.

Parameters
summarySummary text
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 158 of file alert_rule.h.

158 {
159 annotations_.summary = std::move(summary);
160 return *this;
161 }
@ summary
Pre-calculated quantiles and count/sum.
std::string summary
Brief description.

References annotations_, kcenon::monitoring::alert_annotations::summary, and kcenon::monitoring::summary.

Referenced by main(), TEST(), and TEST().

Here is the caller graph for this function:

◆ set_trigger()

alert_rule & kcenon::monitoring::alert_rule::set_trigger ( std::shared_ptr< alert_trigger > trigger)
inline

Set the trigger for this rule.

Parameters
triggerTrigger implementation
Returns
Reference to this for chaining
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 240 of file alert_rule.h.

240 {
241 trigger_ = std::move(trigger);
242 return *this;
243 }
std::shared_ptr< alert_trigger > trigger() const
Get the trigger.
Definition alert_rule.h:249
std::shared_ptr< alert_trigger > trigger_
Definition alert_rule.h:314

References trigger(), and trigger_.

Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

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

◆ severity()

alert_severity kcenon::monitoring::alert_rule::severity ( ) const
inline

Get alert severity.

Returns
Severity level
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 118 of file alert_rule.h.

118{ return severity_; }

References severity_.

Referenced by TEST(), and TEST().

Here is the caller graph for this function:

◆ trigger()

std::shared_ptr< alert_trigger > kcenon::monitoring::alert_rule::trigger ( ) const
inline

Get the trigger.

Returns
Trigger implementation
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 249 of file alert_rule.h.

249{ return trigger_; }

References trigger_.

Referenced by main(), set_trigger(), and TEST().

Here is the caller graph for this function:

◆ validate()

common::VoidResult kcenon::monitoring::alert_rule::validate ( ) const
inline

Validate rule configuration.

Returns
Result indicating if configuration is valid
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_rule.h.

Definition at line 270 of file alert_rule.h.

270 {
271 if (name_.empty()) {
273 "Rule name cannot be empty");
274 return common::VoidResult::err(err.to_common_error());
275 }
276 if (!config_.validate()) {
278 "Rule configuration is invalid");
279 return common::VoidResult::err(err.to_common_error());
280 }
281 if (!trigger_) {
283 "Rule must have a trigger");
284 return common::VoidResult::err(err.to_common_error());
285 }
286 return common::ok();
287 }
bool validate() const
Validate configuration.
Definition alert_rule.h:47

References config_, kcenon::monitoring::invalid_argument, kcenon::monitoring::invalid_configuration, name_, kcenon::monitoring::error_info::to_common_error(), trigger_, and kcenon::monitoring::alert_rule_config::validate().

Referenced by main(), TEST(), TEST(), TEST(), and TEST().

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

Member Data Documentation

◆ annotations_

◆ config_

◆ enabled_

bool kcenon::monitoring::alert_rule::enabled_
private

◆ group_

std::string kcenon::monitoring::alert_rule::group_
private

◆ labels_

alert_labels kcenon::monitoring::alert_rule::labels_
private

◆ metric_name_

std::string kcenon::monitoring::alert_rule::metric_name_
private

◆ name_

std::string kcenon::monitoring::alert_rule::name_
private

◆ severity_

◆ trigger_

std::shared_ptr<alert_trigger> kcenon::monitoring::alert_rule::trigger_
private

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