41 std::unordered_map<std::string, std::string>
headers;
96 std::ostringstream oss;
101 oss <<
"\"value\":" << a.
value <<
",";
104 oss <<
"\"fingerprint\":\"" << a.
fingerprint() <<
"\",";
105 oss <<
"\"labels\":{";
108 if (!first) oss <<
",";
117 std::ostringstream oss;
121 oss <<
"\"alert_count\":" << group.
size() <<
",";
122 oss <<
"\"alerts\":[";
124 for (
const auto& a : group.
alerts) {
125 if (!first) oss <<
",";
135 std::ostringstream oss;
138 case '"': oss <<
"\\\"";
break;
139 case '\\': oss <<
"\\\\";
break;
140 case '\n': oss <<
"\\n";
break;
141 case '\r': oss <<
"\\r";
break;
142 case '\t': oss <<
"\\t";
break;
143 default: oss << c;
break;
157 std::ostringstream oss;
161 <<
" Value: " << a.
value <<
"\n"
167 std::ostringstream oss;
168 oss <<
"Alert Group: " << group.
group_key <<
"\n"
169 <<
" Total alerts: " << group.
size() <<
"\n"
172 for (
const auto& a : group.
alerts) {
200 const std::string& url,
201 const std::string& method,
202 const std::unordered_map<std::string, std::string>& headers,
203 const std::string& body
212 std::shared_ptr<alert_formatter> formatter =
nullptr)
216 std::string
name()
const override {
230 std::string payload =
formatter_->format_group(group);
273 if (result.is_ok()) {
283 "Failed to send webhook after " +
307 std::shared_ptr<alert_formatter> formatter =
nullptr)
311 std::string
name()
const override {
329 std::lock_guard<std::mutex> lock(
mutex_);
331 std::ofstream file(
file_path_, std::ios::app);
337 auto now = std::chrono::system_clock::now();
338 auto time_t_now = std::chrono::system_clock::to_time_t(now);
342 ctime_s(time_buf,
sizeof(time_buf), &time_t_now);
344 std::strncpy(time_buf, std::ctime(&time_t_now),
sizeof(time_buf) - 1);
345 time_buf[
sizeof(time_buf) - 1] =
'\0';
347 file <<
"=== " << time_buf;
348 file << content <<
"\n\n";
371 :
name_(std::move(notifier_name)) {}
377 std::lock_guard<std::mutex> lock(
mutex_);
384 std::lock_guard<std::mutex> lock(
mutex_);
386 std::vector<std::string> failures;
388 if (notifier && notifier->is_ready()) {
389 auto result = notifier->notify(a);
390 if (!result.is_ok()) {
391 failures.push_back(notifier->name());
396 if (!failures.empty()) {
404 std::lock_guard<std::mutex> lock(
mutex_);
406 std::vector<std::string> failures;
408 if (notifier && notifier->is_ready()) {
409 auto result = notifier->notify_group(group);
410 if (!result.is_ok()) {
411 failures.push_back(notifier->name());
416 if (!failures.empty()) {
424 std::lock_guard<std::mutex> lock(
mutex_);
429 static std::string
join_strings(
const std::vector<std::string>& strings,
430 const std::string& delimiter) {
431 std::ostringstream oss;
432 for (
size_t i = 0; i < strings.size(); ++i) {
433 if (i > 0) oss << delimiter;
460 size_t buffer_size = 100,
461 std::chrono::milliseconds flush_interval = std::chrono::seconds(30))
462 :
inner_(std::move(inner))
467 std::string
name()
const override {
468 return "buffered:" + (
inner_ ?
inner_->name() :
"none");
472 std::lock_guard<std::mutex> lock(
mutex_);
484 std::lock_guard<std::mutex> lock(
mutex_);
486 for (
const auto& a : group.
alerts) {
505 std::lock_guard<std::mutex> lock(
mutex_);
513 std::lock_guard<std::mutex> lock(
mutex_);
522 auto now = std::chrono::steady_clock::now();
540 return inner_->notify_group(group);
567 :
name_(std::move(notifier_name)) {}
575 std::shared_ptr<alert_notifier> notifier) {
576 std::lock_guard<std::mutex> lock(
mutex_);
577 routes_.push_back({std::move(condition), std::move(notifier)});
584 std::lock_guard<std::mutex> lock(
mutex_);
591 std::lock_guard<std::mutex> lock(
mutex_);
593 for (
const auto& [condition, notifier] :
routes_) {
594 if (condition(a) && notifier && notifier->is_ready()) {
595 return notifier->notify(a);
608 for (
const auto& a : group.
alerts) {
610 if (!result.is_ok()) {
618 std::lock_guard<std::mutex> lock(
mutex_);
627 std::shared_ptr<alert_notifier> notifier) {
629 [severity](
const alert& a) {
return a.
severity == severity; },
638 const std::string& value,
639 std::shared_ptr<alert_notifier> notifier) {
641 [key, value](
const alert& a) {
return a.
labels.
get(key) == value; },
Central coordinator for alert lifecycle management.
Core alert data structures for the monitoring system.
Base class for alert notification handlers.
Buffers alerts and sends in batches.
std::string name() const override
Get notifier name.
common::VoidResult notify_group(const alert_group &group) override
Send a notification for an alert group.
bool should_flush() const
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.
common::VoidResult flush_internal()
size_t pending_count() const
Get current buffer size.
common::VoidResult flush()
Flush buffered alerts.
std::shared_ptr< alert_notifier > inner_
std::vector< alert > buffer_
std::chrono::milliseconds flush_interval_
bool is_ready() const override
Check if notifier is ready.
common::VoidResult notify(const alert &a) override
Send a notification for an alert.
std::chrono::steady_clock::time_point last_flush_
common::VoidResult notify(const alert &a) override
Send a notification for an alert.
common::VoidResult write_to_file(const std::string &content)
bool is_ready() const override
Check if notifier is ready.
std::string name() const override
Get notifier name.
file_notifier(std::string file_path, std::shared_ptr< alert_formatter > formatter=nullptr)
Construct file notifier.
std::shared_ptr< alert_formatter > formatter_
common::VoidResult notify_group(const alert_group &group) override
Send a notification for an alert group.
Sends alerts to multiple notifiers.
multi_notifier(std::string notifier_name)
Construct with name.
void add_notifier(std::shared_ptr< alert_notifier > notifier)
Add a child notifier.
common::VoidResult notify_group(const alert_group &group) override
Send a notification for an alert group.
common::VoidResult notify(const alert &a) override
Send a notification for an alert.
static std::string join_strings(const std::vector< std::string > &strings, const std::string &delimiter)
std::vector< std::shared_ptr< alert_notifier > > notifiers_
std::string name() const override
Get notifier name.
bool is_ready() const override
Check if notifier is ready.
Routes alerts to different notifiers based on criteria.
std::vector< route > routes_
common::VoidResult notify(const alert &a) override
Send a notification for an alert.
routing_notifier(std::string notifier_name)
Construct routing notifier.
void add_route(route_condition condition, std::shared_ptr< alert_notifier > notifier)
Add a route.
void route_by_label(const std::string &key, const std::string &value, std::shared_ptr< alert_notifier > notifier)
Route by label.
bool is_ready() const override
Check if notifier is ready.
void route_by_severity(alert_severity severity, std::shared_ptr< alert_notifier > notifier)
Route by severity.
std::string name() const override
Get notifier name.
common::VoidResult notify_group(const alert_group &group) override
Send a notification for an alert group.
std::shared_ptr< alert_notifier > default_route_
std::function< bool(const alert &)> route_condition
void set_default_route(std::shared_ptr< alert_notifier > notifier)
Add a default route for non-matching alerts.
Formats alerts as human-readable text.
std::string format_group(const alert_group &group) const override
Format an alert group.
std::string format(const alert &a) const override
Format a single alert.
Sends alerts to a webhook endpoint.
std::shared_ptr< alert_formatter > formatter_
webhook_notifier(const webhook_config &config, std::shared_ptr< alert_formatter > formatter=nullptr)
Construct webhook notifier.
common::VoidResult send_with_retry(const std::string &payload)
std::string name() const override
Get notifier name.
void set_http_sender(http_sender_func sender)
Set HTTP sender function for actual HTTP calls.
const webhook_config & config() const
Get configuration.
common::VoidResult notify_group(const alert_group &group) override
Send a notification for an alert group.
http_sender_func http_sender_
bool is_ready() const override
Check if notifier is ready.
common::VoidResult notify(const alert &a) override
Send a notification for an alert.
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)> http_sender_func
@ retry_attempts_exhausted
alert_severity
Severity levels for alerts.
constexpr const char * alert_state_to_string(alert_state state) noexcept
Convert alert state to string.
constexpr const char * alert_severity_to_string(alert_severity severity) noexcept
Convert alert severity to string.
@ resolved
Alert condition cleared.
Result pattern type definitions for monitoring system.
std::string description
Detailed description.
std::string summary
Brief description.
Group of related alerts for batch notification.
std::string group_key
Common grouping key.
void add_alert(alert a)
Add an alert to the group.
std::vector< alert > alerts
Alerts in this group.
size_t size() const
Get count of alerts in the group.
alert_severity max_severity() const
Get highest severity in the group.
std::unordered_map< std::string, std::string > labels
std::string get(const std::string &key) const
Get a label value.
Core alert data structure.
alert_state state
Current state.
double value
Current metric value.
alert_severity severity
Alert severity level.
std::string name
Alert name/identifier.
alert_labels labels
Identifying labels.
alert_annotations annotations
Descriptive annotations.
std::string fingerprint() const
Get alert fingerprint for deduplication.
Extended error information with context.
route_condition condition
std::shared_ptr< alert_notifier > notifier
Configuration for webhook notifier.
std::chrono::milliseconds retry_delay
Delay between retries.
std::chrono::milliseconds timeout
Request timeout.
bool send_resolved
Send resolved notifications.
size_t max_retries
Maximum retry attempts.
std::string content_type
Content type header.
bool validate() const
Validate configuration.
webhook_config & add_header(const std::string &key, const std::string &value)
Add a custom header.
std::unordered_map< std::string, std::string > headers
Custom headers.
std::string url
Webhook URL.
std::string method
HTTP method.