Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
alert_rule.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
15#include <chrono>
16#include <functional>
17#include <memory>
18#include <optional>
19#include <string>
20#include <vector>
21
22#include "alert_types.h"
24
25namespace kcenon::monitoring {
26
27// Forward declarations
28class alert_trigger;
29
37 std::chrono::milliseconds evaluation_interval{15000};
38 std::chrono::milliseconds for_duration{0};
39 std::chrono::milliseconds repeat_interval{300000};
40 bool keep_firing_for{false};
41 std::chrono::milliseconds keep_firing_duration{300000};
42
47 bool validate() const {
48 if (evaluation_interval.count() <= 0) {
49 return false;
50 }
51 if (repeat_interval.count() <= 0) {
52 return false;
53 }
54 return true;
55 }
56};
57
83public:
88 explicit alert_rule(std::string name)
89 : name_(std::move(name))
90 , enabled_(true) {}
91
96 const std::string& name() const { return name_; }
97
102 const std::string& group() const { return group_; }
103
109 alert_rule& set_group(std::string group_name) {
110 group_ = std::move(group_name);
111 return *this;
112 }
113
119
126 severity_ = sev;
127 return *this;
128 }
129
134 const alert_labels& labels() const { return labels_; }
135
142 alert_rule& add_label(const std::string& key, const std::string& value) {
143 labels_.set(key, value);
144 return *this;
145 }
146
151 const alert_annotations& annotations() const { return annotations_; }
152
159 annotations_.summary = std::move(summary);
160 return *this;
161 }
162
168 alert_rule& set_description(std::string description) {
169 annotations_.description = std::move(description);
170 return *this;
171 }
172
178 alert_rule& set_runbook_url(std::string url) {
179 annotations_.runbook_url = std::move(url);
180 return *this;
181 }
182
187 const alert_rule_config& config() const { return config_; }
188
194 alert_rule& set_evaluation_interval(std::chrono::milliseconds interval) {
195 config_.evaluation_interval = interval;
196 return *this;
197 }
198
204 alert_rule& set_for_duration(std::chrono::milliseconds duration) {
205 config_.for_duration = duration;
206 return *this;
207 }
208
214 alert_rule& set_repeat_interval(std::chrono::milliseconds interval) {
215 config_.repeat_interval = interval;
216 return *this;
217 }
218
223 bool is_enabled() const { return enabled_; }
224
230 alert_rule& set_enabled(bool enabled) {
231 enabled_ = enabled;
232 return *this;
233 }
234
240 alert_rule& set_trigger(std::shared_ptr<alert_trigger> trigger) {
241 trigger_ = std::move(trigger);
242 return *this;
243 }
244
249 std::shared_ptr<alert_trigger> trigger() const { return trigger_; }
250
256 alert create_alert(double value) const {
257 alert a(name_, labels_);
260 a.value = value;
261 a.rule_name = name_;
262 a.group_key = group_.empty() ? name_ : group_;
263 return a;
264 }
265
270 common::VoidResult validate() const {
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 }
288
293 const std::string& metric_name() const { return metric_name_; }
294
301 metric_name_ = std::move(name);
302 return *this;
303 }
304
305private:
306 std::string name_;
307 std::string group_;
308 std::string metric_name_;
314 std::shared_ptr<alert_trigger> trigger_;
315};
316
326public:
327 virtual ~alert_trigger() = default;
328
334 virtual bool evaluate(double value) const = 0;
335
340 virtual std::string type_name() const = 0;
341
346 virtual std::string description() const = 0;
347};
348
357public:
362 explicit alert_rule_group(std::string name)
363 : name_(std::move(name)) {}
364
369 const std::string& name() const { return name_; }
370
375 void add_rule(std::shared_ptr<alert_rule> rule) {
376 if (rule) {
377 rule->set_group(name_);
378 rules_.push_back(std::move(rule));
379 }
380 }
381
386 const std::vector<std::shared_ptr<alert_rule>>& rules() const {
387 return rules_;
388 }
389
394 size_t size() const { return rules_.size(); }
395
400 bool empty() const { return rules_.empty(); }
401
406 void set_common_interval(std::chrono::milliseconds interval) {
407 common_interval_ = interval;
408 for (auto& rule : rules_) {
409 rule->set_evaluation_interval(interval);
410 }
411 }
412
417 std::optional<std::chrono::milliseconds> common_interval() const {
418 return common_interval_;
419 }
420
421private:
422 std::string name_;
423 std::vector<std::shared_ptr<alert_rule>> rules_;
424 std::optional<std::chrono::milliseconds> common_interval_;
425};
426
427} // namespace kcenon::monitoring
Core alert data structures for the monitoring system.
A group of related alert rules.
Definition alert_rule.h:356
std::optional< std::chrono::milliseconds > common_interval() const
Get common evaluation interval.
Definition alert_rule.h:417
std::optional< std::chrono::milliseconds > common_interval_
Definition alert_rule.h:424
bool empty() const
Check if group is empty.
Definition alert_rule.h:400
alert_rule_group(std::string name)
Construct a rule group.
Definition alert_rule.h:362
size_t size() const
Get number of rules.
Definition alert_rule.h:394
void set_common_interval(std::chrono::milliseconds interval)
Set common evaluation interval for all rules.
Definition alert_rule.h:406
std::vector< std::shared_ptr< alert_rule > > rules_
Definition alert_rule.h:423
void add_rule(std::shared_ptr< alert_rule > rule)
Add a rule to the group.
Definition alert_rule.h:375
const std::string & name() const
Get group name.
Definition alert_rule.h:369
const std::vector< std::shared_ptr< alert_rule > > & rules() const
Get all rules in the group.
Definition alert_rule.h:386
Defines conditions and behavior for alert triggering.
Definition alert_rule.h:82
const alert_rule_config & config() const
Get configuration.
Definition alert_rule.h:187
alert_rule & set_evaluation_interval(std::chrono::milliseconds interval)
Set evaluation interval.
Definition alert_rule.h:194
alert_rule & set_trigger(std::shared_ptr< alert_trigger > trigger)
Set the trigger for this rule.
Definition alert_rule.h:240
std::shared_ptr< alert_trigger > trigger() const
Get the trigger.
Definition alert_rule.h:249
alert_rule(std::string name)
Construct an alert rule with a name.
Definition alert_rule.h:88
const std::string & group() const
Get rule group.
Definition alert_rule.h:102
const std::string & name() const
Get rule name.
Definition alert_rule.h:96
std::shared_ptr< alert_trigger > trigger_
Definition alert_rule.h:314
const alert_annotations & annotations() const
Get annotations.
Definition alert_rule.h:151
alert_rule & set_group(std::string group_name)
Set rule group.
Definition alert_rule.h:109
common::VoidResult validate() const
Validate rule configuration.
Definition alert_rule.h:270
alert_rule & set_summary(std::string summary)
Set alert summary.
Definition alert_rule.h:158
alert create_alert(double value) const
Create an alert from this rule.
Definition alert_rule.h:256
alert_rule & set_for_duration(std::chrono::milliseconds duration)
Set for duration (pending time before firing)
Definition alert_rule.h:204
const std::string & metric_name() const
Get metric name to monitor.
Definition alert_rule.h:293
alert_rule & set_description(std::string description)
Set alert description.
Definition alert_rule.h:168
alert_rule & set_enabled(bool enabled)
Enable or disable rule.
Definition alert_rule.h:230
alert_rule & set_severity(alert_severity sev)
Set alert severity.
Definition alert_rule.h:125
bool is_enabled() const
Check if rule is enabled.
Definition alert_rule.h:223
alert_severity severity() const
Get alert severity.
Definition alert_rule.h:118
alert_rule & set_runbook_url(std::string url)
Set runbook URL.
Definition alert_rule.h:178
alert_rule & add_label(const std::string &key, const std::string &value)
Add a label.
Definition alert_rule.h:142
alert_annotations annotations_
Definition alert_rule.h:311
alert_rule & set_repeat_interval(std::chrono::milliseconds interval)
Set notification repeat interval.
Definition alert_rule.h:214
const alert_labels & labels() const
Get labels.
Definition alert_rule.h:134
alert_rule & set_metric_name(std::string name)
Set metric name to monitor.
Definition alert_rule.h:300
Base class for alert trigger conditions.
Definition alert_rule.h:325
virtual std::string description() const =0
Get human-readable description.
virtual bool evaluate(double value) const =0
Evaluate the trigger condition.
virtual std::string type_name() const =0
Get trigger type name.
@ summary
Pre-calculated quantiles and count/sum.
alert_severity
Severity levels for alerts.
Definition alert_types.h:35
@ warning
Warning condition, may require attention.
Result pattern type definitions for monitoring system.
Additional metadata for alert context.
std::string description
Detailed description.
std::string summary
Brief description.
std::optional< std::string > runbook_url
Link to runbook.
Key-value labels for alert identification and routing.
void set(const std::string &key, const std::string &value)
Add or update a label.
Configuration for an alert rule.
Definition alert_rule.h:36
bool validate() const
Validate configuration.
Definition alert_rule.h:47
std::chrono::milliseconds for_duration
Duration before firing.
Definition alert_rule.h:38
std::chrono::milliseconds keep_firing_duration
Duration to keep firing.
Definition alert_rule.h:41
bool keep_firing_for
Keep firing after resolve.
Definition alert_rule.h:40
std::chrono::milliseconds repeat_interval
Notification repeat interval.
Definition alert_rule.h:39
std::chrono::milliseconds evaluation_interval
How often to evaluate.
Definition alert_rule.h:37
Core alert data structure.
double value
Current metric value.
alert_severity severity
Alert severity level.
std::string rule_name
Name of triggering rule.
std::string group_key
Grouping key for dedup.
alert_annotations annotations
Descriptive annotations.
Extended error information with context.
common::error_info to_common_error() const
Convert to common_system error_info.