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

Buffers alerts and sends in batches. More...

#include <alert_notifiers.h>

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

Public Member Functions

 buffered_notifier (std::shared_ptr< alert_notifier > inner, size_t buffer_size=100, std::chrono::milliseconds flush_interval=std::chrono::seconds(30))
 Construct buffered notifier.
 
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.
 
common::VoidResult flush ()
 Flush buffered alerts.
 
size_t pending_count () const
 Get current buffer size.
 
- Public Member Functions inherited from kcenon::monitoring::alert_notifier
virtual ~alert_notifier ()=default
 

Private Member Functions

bool should_flush () const
 
common::VoidResult flush_internal ()
 

Private Attributes

std::shared_ptr< alert_notifierinner_
 
size_t buffer_size_
 
std::chrono::milliseconds flush_interval_
 
std::mutex mutex_
 
std::vector< alertbuffer_
 
std::chrono::steady_clock::time_point last_flush_
 

Detailed Description

Buffers alerts and sends in batches.

Collects alerts and sends them in batches either when the buffer is full or when flush is called.

Definition at line 451 of file alert_notifiers.h.

Constructor & Destructor Documentation

◆ buffered_notifier()

kcenon::monitoring::buffered_notifier::buffered_notifier ( std::shared_ptr< alert_notifier > inner,
size_t buffer_size = 100,
std::chrono::milliseconds flush_interval = std::chrono::seconds(30) )
inline

Construct buffered notifier.

Parameters
innerInner notifier to use for actual sending
buffer_sizeMaximum alerts before auto-flush
flush_intervalMaximum time before auto-flush
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 459 of file alert_notifiers.h.

462 : inner_(std::move(inner))
463 , buffer_size_(buffer_size)
464 , flush_interval_(flush_interval)
465 , last_flush_(std::chrono::steady_clock::now()) {}
std::shared_ptr< alert_notifier > inner_
std::chrono::milliseconds flush_interval_
std::chrono::steady_clock::time_point last_flush_

Member Function Documentation

◆ flush()

common::VoidResult kcenon::monitoring::buffered_notifier::flush ( )
inline

Flush buffered alerts.

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

Definition at line 504 of file alert_notifiers.h.

504 {
505 std::lock_guard<std::mutex> lock(mutex_);
506 return flush_internal();
507 }

References flush_internal(), and mutex_.

Here is the call graph for this function:

◆ flush_internal()

common::VoidResult kcenon::monitoring::buffered_notifier::flush_internal ( )
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 526 of file alert_notifiers.h.

526 {
527 if (buffer_.empty() || !inner_) {
528 return common::ok();
529 }
530
531 // Create a group from buffered alerts
532 alert_group group("buffered");
533 for (auto& a : buffer_) {
534 group.add_alert(std::move(a));
535 }
536 buffer_.clear();
537
538 last_flush_ = std::chrono::steady_clock::now();
539
540 return inner_->notify_group(group);
541 }

References kcenon::monitoring::alert_group::add_alert(), buffer_, inner_, and last_flush_.

Referenced by flush(), notify(), and notify_group().

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

◆ is_ready()

bool kcenon::monitoring::buffered_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 497 of file alert_notifiers.h.

497 {
498 return inner_ && inner_->is_ready();
499 }

References inner_.

◆ name()

std::string kcenon::monitoring::buffered_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 467 of file alert_notifiers.h.

467 {
468 return "buffered:" + (inner_ ? inner_->name() : "none");
469 }

References inner_.

◆ notify()

common::VoidResult kcenon::monitoring::buffered_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 471 of file alert_notifiers.h.

471 {
472 std::lock_guard<std::mutex> lock(mutex_);
473
474 buffer_.push_back(a);
475
476 if (should_flush()) {
477 return flush_internal();
478 }
479
480 return common::ok();
481 }

References buffer_, flush_internal(), mutex_, and should_flush().

Here is the call graph for this function:

◆ notify_group()

common::VoidResult kcenon::monitoring::buffered_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 483 of file alert_notifiers.h.

483 {
484 std::lock_guard<std::mutex> lock(mutex_);
485
486 for (const auto& a : group.alerts) {
487 buffer_.push_back(a);
488 }
489
490 if (should_flush()) {
491 return flush_internal();
492 }
493
494 return common::ok();
495 }

References kcenon::monitoring::alert_group::alerts, buffer_, flush_internal(), mutex_, and should_flush().

Here is the call graph for this function:

◆ pending_count()

size_t kcenon::monitoring::buffered_notifier::pending_count ( ) const
inline

Get current buffer size.

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

Definition at line 512 of file alert_notifiers.h.

512 {
513 std::lock_guard<std::mutex> lock(mutex_);
514 return buffer_.size();
515 }

References buffer_, and mutex_.

◆ should_flush()

bool kcenon::monitoring::buffered_notifier::should_flush ( ) const
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 518 of file alert_notifiers.h.

518 {
519 if (buffer_.size() >= buffer_size_) {
520 return true;
521 }
522 auto now = std::chrono::steady_clock::now();
523 return (now - last_flush_) >= flush_interval_;
524 }

References buffer_, buffer_size_, flush_interval_, and last_flush_.

Referenced by notify(), and notify_group().

Here is the caller graph for this function:

Member Data Documentation

◆ buffer_

std::vector<alert> kcenon::monitoring::buffered_notifier::buffer_
private

◆ buffer_size_

size_t kcenon::monitoring::buffered_notifier::buffer_size_
private

◆ flush_interval_

std::chrono::milliseconds kcenon::monitoring::buffered_notifier::flush_interval_
private

◆ inner_

std::shared_ptr<alert_notifier> kcenon::monitoring::buffered_notifier::inner_
private

◆ last_flush_

std::chrono::steady_clock::time_point kcenon::monitoring::buffered_notifier::last_flush_
private

◆ mutex_

std::mutex kcenon::monitoring::buffered_notifier::mutex_
mutableprivate

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