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

Sends alerts to a webhook endpoint. More...

#include <alert_notifiers.h>

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

Public Types

using http_sender_func
 

Public Member Functions

 webhook_notifier (const webhook_config &config, std::shared_ptr< alert_formatter > formatter=nullptr)
 Construct webhook 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.
 
void set_http_sender (http_sender_func sender)
 Set HTTP sender function for actual HTTP calls.
 
const webhook_configconfig () const
 Get configuration.
 
- Public Member Functions inherited from kcenon::monitoring::alert_notifier
virtual ~alert_notifier ()=default
 

Private Member Functions

common::VoidResult send_with_retry (const std::string &payload)
 

Private Attributes

webhook_config config_
 
std::shared_ptr< alert_formatterformatter_
 
http_sender_func http_sender_
 

Detailed Description

Sends alerts to a webhook endpoint.

Note: Actual HTTP implementation requires network_system integration or an external HTTP client. This implementation provides the interface and can be extended with actual HTTP support.

Definition at line 197 of file alert_notifiers.h.

Member Typedef Documentation

◆ http_sender_func

Initial value:
std::function<common::VoidResult(
const std::string& url,
const std::string& method,
const std::unordered_map<std::string, std::string>& headers,
const std::string& body
)>
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 199 of file alert_notifiers.h.

Constructor & Destructor Documentation

◆ webhook_notifier()

kcenon::monitoring::webhook_notifier::webhook_notifier ( const webhook_config & config,
std::shared_ptr< alert_formatter > formatter = nullptr )
inlineexplicit

Construct webhook notifier.

Parameters
configWebhook configuration
formatterAlert formatter (default: JSON)
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 211 of file alert_notifiers.h.

213 : config_(config)
214 , formatter_(formatter ? formatter : std::make_shared<json_alert_formatter>()) {}
std::shared_ptr< alert_formatter > formatter_
const webhook_config & config() const
Get configuration.

Member Function Documentation

◆ config()

const webhook_config & kcenon::monitoring::webhook_notifier::config ( ) const
inline

◆ is_ready()

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

234 {
235 return config_.validate() && http_sender_ != nullptr;
236 }
bool validate() const
Validate configuration.

References config_, http_sender_, and kcenon::monitoring::webhook_config::validate().

Here is the call graph for this function:

◆ name()

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

216 {
217 return "webhook:" + config_.url;
218 }

References config_, and kcenon::monitoring::webhook_config::url.

◆ notify()

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

220 {
221 if (!config_.send_resolved && a.state == alert_state::resolved) {
222 return common::ok();
223 }
224
225 std::string payload = formatter_->format(a);
226 return send_with_retry(payload);
227 }
common::VoidResult send_with_retry(const std::string &payload)
@ resolved
Alert condition cleared.
bool send_resolved
Send resolved notifications.

References config_, formatter_, kcenon::monitoring::resolved, kcenon::monitoring::webhook_config::send_resolved, send_with_retry(), and kcenon::monitoring::alert::state.

Here is the call graph for this function:

◆ notify_group()

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

229 {
230 std::string payload = formatter_->format_group(group);
231 return send_with_retry(payload);
232 }

References formatter_, and send_with_retry().

Here is the call graph for this function:

◆ send_with_retry()

common::VoidResult kcenon::monitoring::webhook_notifier::send_with_retry ( const std::string & payload)
inlineprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 263 of file alert_notifiers.h.

263 {
264 if (!http_sender_) {
265 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed, "No HTTP sender configured").to_common_error());
266 }
267
268 auto headers = config_.headers;
269 headers["Content-Type"] = config_.content_type;
270
271 for (size_t attempt = 0; attempt <= config_.max_retries; ++attempt) {
272 auto result = http_sender_(config_.url, config_.method, headers, payload);
273 if (result.is_ok()) {
274 return result;
275 }
276
277 if (attempt < config_.max_retries) {
278 std::this_thread::sleep_for(config_.retry_delay);
279 }
280 }
281
282 return common::VoidResult::err(static_cast<int>(monitoring_error_code::retry_attempts_exhausted),
283 "Failed to send webhook after " +
284 std::to_string(config_.max_retries) + " retries");
285 }
std::chrono::milliseconds retry_delay
Delay between retries.
size_t max_retries
Maximum retry attempts.
std::string content_type
Content type header.
std::unordered_map< std::string, std::string > headers
Custom headers.

References config_, kcenon::monitoring::webhook_config::content_type, kcenon::monitoring::webhook_config::headers, http_sender_, kcenon::monitoring::webhook_config::max_retries, kcenon::monitoring::webhook_config::method, kcenon::monitoring::operation_failed, kcenon::monitoring::retry_attempts_exhausted, kcenon::monitoring::webhook_config::retry_delay, and kcenon::monitoring::webhook_config::url.

Referenced by notify(), and notify_group().

Here is the caller graph for this function:

◆ set_http_sender()

void kcenon::monitoring::webhook_notifier::set_http_sender ( http_sender_func sender)
inline

Set HTTP sender function for actual HTTP calls.

Parameters
senderFunction to send HTTP requests

This allows injecting a real HTTP implementation:

notifier->set_http_sender([](const std::string& url,
const std::string& method,
const auto& headers,
const std::string& body) {
// Use network_system or other HTTP client
return http_client.request(url, method, headers, body);
});
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_notifiers.h.

Definition at line 253 of file alert_notifiers.h.

253 {
254 http_sender_ = std::move(sender);
255 }

References http_sender_.

Member Data Documentation

◆ config_

webhook_config kcenon::monitoring::webhook_notifier::config_
private

◆ formatter_

std::shared_ptr<alert_formatter> kcenon::monitoring::webhook_notifier::formatter_
private

◆ http_sender_

http_sender_func kcenon::monitoring::webhook_notifier::http_sender_
private

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