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

Groups and deduplicates alerts. More...

#include <alert_pipeline.h>

Collaboration diagram for kcenon::monitoring::alert_aggregator:
Collaboration graph

Public Member Functions

 alert_aggregator (const alert_aggregator_config &config)
 Construct with configuration.
 
std::string add_alert (const alert &a)
 Add an alert for aggregation.
 
std::vector< alert_groupget_ready_groups ()
 Get groups ready for notification.
 
void mark_sent (const std::string &group_key)
 Mark a group as sent.
 
void cleanup ()
 Remove resolved alerts and clean up old groups.
 
size_t group_count () const
 Get current group count.
 
size_t total_alert_count () const
 Get total alert count across all groups.
 

Private Member Functions

std::string compute_group_key (const alert &a) const
 
alert_labels extract_common_labels (const alert &a) const
 

Private Attributes

alert_aggregator_config config_
 
std::mutex mutex_
 
std::unordered_map< std::string, alert_groupgroups_
 
std::unordered_map< std::string, std::chrono::steady_clock::time_point > first_seen_
 
std::unordered_map< std::string, std::chrono::steady_clock::time_point > last_sent_
 

Detailed Description

Groups and deduplicates alerts.

The aggregator collects alerts over time and groups them based on configured labels. This reduces notification noise by batching related alerts together.

Thread Safety:
This class is thread-safe.
Examples
alert_pipeline_example.cpp.

Definition at line 79 of file alert_pipeline.h.

Constructor & Destructor Documentation

◆ alert_aggregator()

kcenon::monitoring::alert_aggregator::alert_aggregator ( const alert_aggregator_config & config)
inlineexplicit

Construct with configuration.

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

Definition at line 84 of file alert_pipeline.h.

85 : config_(config) {}

Member Function Documentation

◆ add_alert()

std::string kcenon::monitoring::alert_aggregator::add_alert ( const alert & a)
inline

Add an alert for aggregation.

Parameters
aAlert to aggregate
Returns
Group key the alert was added to
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 92 of file alert_pipeline.h.

92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94
95 std::string group_key = compute_group_key(a);
96 auto now = std::chrono::steady_clock::now();
97
98 auto it = groups_.find(group_key);
99 if (it == groups_.end()) {
100 alert_group new_group(group_key);
101 new_group.common_labels = extract_common_labels(a);
102 new_group.add_alert(a);
103 groups_[group_key] = std::move(new_group);
104 first_seen_[group_key] = now;
105 } else {
106 // Check for duplicate
107 bool is_duplicate = false;
108 for (auto& existing : it->second.alerts) {
109 if (existing.fingerprint() == a.fingerprint()) {
110 existing = a; // Update existing alert
111 is_duplicate = true;
112 break;
113 }
114 }
115 if (!is_duplicate) {
116 it->second.add_alert(a);
117 }
118 }
119
120 return group_key;
121 }
alert_labels extract_common_labels(const alert &a) const
std::string compute_group_key(const alert &a) const
std::unordered_map< std::string, std::chrono::steady_clock::time_point > first_seen_
std::unordered_map< std::string, alert_group > groups_

References kcenon::monitoring::alert_group::add_alert(), kcenon::monitoring::alert_group::common_labels, compute_group_key(), extract_common_labels(), kcenon::monitoring::alert::fingerprint(), first_seen_, groups_, and mutex_.

Referenced by main().

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

◆ cleanup()

void kcenon::monitoring::alert_aggregator::cleanup ( )
inline

Remove resolved alerts and clean up old groups.

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

Definition at line 170 of file alert_pipeline.h.

170 {
171 std::lock_guard<std::mutex> lock(mutex_);
172
173 auto now = std::chrono::steady_clock::now();
174
175 for (auto it = groups_.begin(); it != groups_.end(); ) {
176 auto& group = it->second;
177
178 // Remove resolved alerts that have timed out
179 auto alert_it = group.alerts.begin();
180 while (alert_it != group.alerts.end()) {
181 if (alert_it->state == alert_state::resolved) {
182 auto resolved_time = alert_it->resolved_at.value_or(now);
183 if (now - resolved_time > config_.resolve_timeout) {
184 alert_it = group.alerts.erase(alert_it);
185 continue;
186 }
187 }
188 ++alert_it;
189 }
190
191 // Remove empty groups
192 if (group.empty()) {
193 first_seen_.erase(it->first);
194 last_sent_.erase(it->first);
195 it = groups_.erase(it);
196 } else {
197 ++it;
198 }
199 }
200 }
std::unordered_map< std::string, std::chrono::steady_clock::time_point > last_sent_
@ resolved
Alert condition cleared.
std::chrono::milliseconds resolve_timeout
Time before removing resolved.

References config_, first_seen_, groups_, last_sent_, mutex_, kcenon::monitoring::alert_aggregator_config::resolve_timeout, and kcenon::monitoring::resolved.

Referenced by main().

Here is the caller graph for this function:

◆ compute_group_key()

std::string kcenon::monitoring::alert_aggregator::compute_group_key ( const alert & a) const
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 223 of file alert_pipeline.h.

223 {
224 if (config_.group_by_labels.empty()) {
225 return a.rule_name;
226 }
227
228 std::string key;
229 for (const auto& label : config_.group_by_labels) {
230 key += a.labels.get(label) + ":";
231 }
232 return key;
233 }
std::vector< std::string > group_by_labels
Labels to group by.

References config_, kcenon::monitoring::alert_labels::get(), kcenon::monitoring::alert_aggregator_config::group_by_labels, kcenon::monitoring::alert::labels, and kcenon::monitoring::alert::rule_name.

Referenced by add_alert().

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

◆ extract_common_labels()

alert_labels kcenon::monitoring::alert_aggregator::extract_common_labels ( const alert & a) const
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 235 of file alert_pipeline.h.

235 {
236 alert_labels common;
237 for (const auto& label : config_.group_by_labels) {
238 std::string val = a.labels.get(label);
239 if (!val.empty()) {
240 common.set(label, val);
241 }
242 }
243 return common;
244 }

References config_, kcenon::monitoring::alert_labels::get(), kcenon::monitoring::alert_aggregator_config::group_by_labels, kcenon::monitoring::alert::labels, and kcenon::monitoring::alert_labels::set().

Referenced by add_alert().

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

◆ get_ready_groups()

std::vector< alert_group > kcenon::monitoring::alert_aggregator::get_ready_groups ( )
inline

Get groups ready for notification.

Returns
Vector of ready groups
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 127 of file alert_pipeline.h.

127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129
130 std::vector<alert_group> ready;
131 auto now = std::chrono::steady_clock::now();
132
133 for (auto& [key, group] : groups_) {
134 if (group.empty()) {
135 continue;
136 }
137
138 // Check if first wait has elapsed
139 auto first_seen = first_seen_[key];
140 if (now - first_seen < config_.group_wait) {
141 continue;
142 }
143
144 // Check if enough time since last send
145 auto last_sent_it = last_sent_.find(key);
146 if (last_sent_it != last_sent_.end()) {
147 if (now - last_sent_it->second < config_.group_interval) {
148 continue;
149 }
150 }
151
152 ready.push_back(group);
153 }
154
155 return ready;
156 }
std::chrono::milliseconds group_interval
Interval between group sends.
std::chrono::milliseconds group_wait
Initial wait before sending.

References config_, first_seen_, kcenon::monitoring::alert_aggregator_config::group_interval, kcenon::monitoring::alert_aggregator_config::group_wait, groups_, last_sent_, and mutex_.

Referenced by main().

Here is the caller graph for this function:

◆ group_count()

size_t kcenon::monitoring::alert_aggregator::group_count ( ) const
inline

Get current group count.

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

Definition at line 205 of file alert_pipeline.h.

205 {
206 std::lock_guard<std::mutex> lock(mutex_);
207 return groups_.size();
208 }

References groups_, and mutex_.

Referenced by main().

Here is the caller graph for this function:

◆ mark_sent()

void kcenon::monitoring::alert_aggregator::mark_sent ( const std::string & group_key)
inline

Mark a group as sent.

Parameters
group_keyGroup key to mark
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 162 of file alert_pipeline.h.

162 {
163 std::lock_guard<std::mutex> lock(mutex_);
164 last_sent_[group_key] = std::chrono::steady_clock::now();
165 }

References last_sent_, and mutex_.

Referenced by main().

Here is the caller graph for this function:

◆ total_alert_count()

size_t kcenon::monitoring::alert_aggregator::total_alert_count ( ) const
inline

Get total alert count across all groups.

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

Definition at line 213 of file alert_pipeline.h.

213 {
214 std::lock_guard<std::mutex> lock(mutex_);
215 size_t count = 0;
216 for (const auto& [key, group] : groups_) {
217 count += group.size();
218 }
219 return count;
220 }

References groups_, and mutex_.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ config_

◆ first_seen_

std::unordered_map<std::string, std::chrono::steady_clock::time_point> kcenon::monitoring::alert_aggregator::first_seen_
private

◆ groups_

std::unordered_map<std::string, alert_group> kcenon::monitoring::alert_aggregator::groups_
private

◆ last_sent_

std::unordered_map<std::string, std::chrono::steady_clock::time_point> kcenon::monitoring::alert_aggregator::last_sent_
private

◆ mutex_

std::mutex kcenon::monitoring::alert_aggregator::mutex_
mutableprivate

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