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

Deduplicates alerts based on fingerprint. More...

#include <alert_pipeline.h>

Collaboration diagram for kcenon::monitoring::alert_deduplicator:
Collaboration graph

Public Member Functions

 alert_deduplicator (std::chrono::milliseconds cache_duration)
 Construct with cache duration.
 
bool is_duplicate (const alert &a)
 Check if alert is a duplicate.
 
void mark_seen (const alert &a)
 Mark alert as seen.
 
void reset ()
 Clear deduplication cache.
 

Private Member Functions

void cleanup_expired ()
 

Private Attributes

std::chrono::milliseconds cache_duration_
 
std::mutex mutex_
 
std::unordered_map< std::string, std::chrono::steady_clock::time_point > seen_
 
std::unordered_map< std::string, alert_statelast_state_
 

Detailed Description

Deduplicates alerts based on fingerprint.

Maintains a cache of recently seen alerts to prevent duplicate notifications for the same alert condition.

Examples
alert_pipeline_example.cpp.

Definition at line 511 of file alert_pipeline.h.

Constructor & Destructor Documentation

◆ alert_deduplicator()

kcenon::monitoring::alert_deduplicator::alert_deduplicator ( std::chrono::milliseconds cache_duration)
inlineexplicit

Construct with cache duration.

Parameters
cache_durationHow long to remember alerts
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 517 of file alert_pipeline.h.

518 : cache_duration_(cache_duration) {}
std::chrono::milliseconds cache_duration_

Member Function Documentation

◆ cleanup_expired()

void kcenon::monitoring::alert_deduplicator::cleanup_expired ( )
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 569 of file alert_pipeline.h.

569 {
570 auto now = std::chrono::steady_clock::now();
571 for (auto it = seen_.begin(); it != seen_.end(); ) {
572 if (now - it->second > cache_duration_) {
573 last_state_.erase(it->first);
574 it = seen_.erase(it);
575 } else {
576 ++it;
577 }
578 }
579 }
std::unordered_map< std::string, alert_state > last_state_
std::unordered_map< std::string, std::chrono::steady_clock::time_point > seen_

References cache_duration_, last_state_, and seen_.

Referenced by is_duplicate().

Here is the caller graph for this function:

◆ is_duplicate()

bool kcenon::monitoring::alert_deduplicator::is_duplicate ( const alert & a)
inline

Check if alert is a duplicate.

Parameters
aAlert to check
Returns
True if duplicate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_pipeline.h.

Definition at line 525 of file alert_pipeline.h.

525 {
526 std::lock_guard<std::mutex> lock(mutex_);
527
529
530 auto fingerprint = a.fingerprint();
531 auto it = seen_.find(fingerprint);
532
533 if (it == seen_.end()) {
534 seen_[fingerprint] = std::chrono::steady_clock::now();
535 return false;
536 }
537
538 // Same state is duplicate
539 auto state_it = last_state_.find(fingerprint);
540 if (state_it != last_state_.end() && state_it->second == a.state) {
541 return true;
542 }
543
544 // State changed - not duplicate
545 last_state_[fingerprint] = a.state;
546 return false;
547 }

References cleanup_expired(), kcenon::monitoring::alert::fingerprint(), last_state_, mutex_, seen_, and kcenon::monitoring::alert::state.

Referenced by main().

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

◆ mark_seen()

void kcenon::monitoring::alert_deduplicator::mark_seen ( const alert & a)
inline

Mark alert as seen.

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

Definition at line 552 of file alert_pipeline.h.

552 {
553 std::lock_guard<std::mutex> lock(mutex_);
554 auto fingerprint = a.fingerprint();
555 seen_[fingerprint] = std::chrono::steady_clock::now();
556 last_state_[fingerprint] = a.state;
557 }

References kcenon::monitoring::alert::fingerprint(), last_state_, mutex_, seen_, and kcenon::monitoring::alert::state.

Here is the call graph for this function:

◆ reset()

void kcenon::monitoring::alert_deduplicator::reset ( )
inline

Clear deduplication cache.

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

Definition at line 562 of file alert_pipeline.h.

562 {
563 std::lock_guard<std::mutex> lock(mutex_);
564 seen_.clear();
565 last_state_.clear();
566 }

References last_state_, mutex_, and seen_.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ cache_duration_

std::chrono::milliseconds kcenon::monitoring::alert_deduplicator::cache_duration_
private

◆ last_state_

std::unordered_map<std::string, alert_state> kcenon::monitoring::alert_deduplicator::last_state_
private

◆ mutex_

std::mutex kcenon::monitoring::alert_deduplicator::mutex_
mutableprivate

◆ seen_

std::unordered_map<std::string, std::chrono::steady_clock::time_point> kcenon::monitoring::alert_deduplicator::seen_
private

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