22#include <unordered_map>
53 default:
return "unknown";
89 default:
return "unknown";
101 std::unordered_map<std::string, std::string>
labels;
105 explicit alert_labels(std::unordered_map<std::string, std::string> lbl)
106 :
labels(std::move(lbl)) {}
113 void set(
const std::string& key,
const std::string& value) {
122 std::string
get(
const std::string& key)
const {
123 auto it =
labels.find(key);
124 return it !=
labels.end() ? it->second :
"";
132 bool has(
const std::string& key)
const {
142 std::vector<std::pair<std::string, std::string>> sorted_labels(
144 std::sort(sorted_labels.begin(), sorted_labels.end());
145 for (
const auto& [key, value] : sorted_labels) {
146 result += key +
"=" + value +
",";
167 std::unordered_map<std::string, std::string>
custom;
195 std::optional<std::chrono::steady_clock::time_point>
started_at;
206 :
created_at(std::chrono::steady_clock::now())
216 :
name(std::move(alert_name))
218 ,
created_at(std::chrono::steady_clock::now())
243 return std::chrono::steady_clock::now() -
updated_at;
252 return std::chrono::steady_clock::now() - *
started_at;
254 return std::chrono::steady_clock::duration::zero();
267 auto now = std::chrono::steady_clock::now();
282 static std::atomic<uint64_t>
counter{0};
283 return counter.fetch_add(1, std::memory_order_relaxed);
330 :
created_at(std::chrono::steady_clock::now())
335 ,
created_at(std::chrono::steady_clock::now())
343 alerts.push_back(std::move(a));
344 updated_at = std::chrono::steady_clock::now();
369 for (
const auto& a :
alerts) {
370 if (
static_cast<uint8_t
>(a.severity) >
static_cast<uint8_t
>(max_sev)) {
371 max_sev = a.severity;
391 std::chrono::steady_clock::time_point
ends_at;
395 ,
starts_at(std::chrono::steady_clock::now())
403 auto now = std::chrono::steady_clock::now();
428 static std::atomic<uint64_t>
counter{0};
429 return counter.fetch_add(1, std::memory_order_relaxed);
@ counter
Monotonically increasing counter.
alert_severity
Severity levels for alerts.
@ warning
Warning condition, may require attention.
@ critical
Critical condition, immediate attention required.
@ emergency
Emergency condition, system-wide impact.
@ info
Informational, no action required.
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.
alert_state
State machine states for alert lifecycle.
@ inactive
Alert condition not met.
@ suppressed
Alert is silenced.
@ pending
Condition met, waiting for duration threshold.
@ firing
Alert is active and notifications sent.
@ resolved
Alert condition cleared.
Additional metadata for alert context.
std::string description
Detailed description.
std::unordered_map< std::string, std::string > custom
Custom annotations.
alert_annotations(std::string sum, std::string desc)
alert_annotations()=default
std::string summary
Brief description.
std::optional< std::string > runbook_url
Link to runbook.
Group of related alerts for batch notification.
std::string group_key
Common grouping key.
alert_labels common_labels
Labels shared by all alerts.
bool empty() const
Check if group is empty.
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_group(std::string key)
std::chrono::steady_clock::time_point created_at
Group creation time.
std::chrono::steady_clock::time_point updated_at
Last modification time.
alert_severity max_severity() const
Get highest severity in the group.
Key-value labels for alert identification and routing.
bool operator==(const alert_labels &other) const
std::string fingerprint() const
Generate a fingerprint for deduplication.
alert_labels(std::unordered_map< std::string, std::string > lbl)
std::unordered_map< std::string, std::string > labels
void set(const std::string &key, const std::string &value)
Add or update a label.
bool has(const std::string &key) const
Check if a label exists.
std::string get(const std::string &key) const
Get a label value.
Silence configuration to suppress alerts.
bool is_active() const
Check if silence is currently active.
static uint64_t generate_id()
std::chrono::steady_clock::time_point ends_at
Silence end time.
alert_labels matchers
Labels to match.
std::string comment
Reason for silence.
std::chrono::steady_clock::time_point starts_at
Silence start time.
std::string created_by
Creator identifier.
bool matches(const alert &a) const
Check if an alert matches this silence.
Core alert data structure.
alert_state state
Current state.
static bool is_valid_transition(alert_state from, alert_state to)
Check if state transition is valid.
std::optional< std::chrono::steady_clock::time_point > started_at
When firing started.
std::chrono::steady_clock::time_point created_at
When alert was created.
alert()
Default constructor.
double value
Current metric value.
std::optional< std::chrono::steady_clock::time_point > resolved_at
When resolved.
alert(std::string alert_name, alert_labels lbl)
Construct with name and labels.
alert_severity severity
Alert severity level.
std::chrono::steady_clock::time_point updated_at
Last state change.
std::string rule_name
Name of triggering rule.
std::chrono::steady_clock::duration state_duration() const
Get duration in current state.
static uint64_t generate_id()
uint64_t id
Unique alert ID.
std::string group_key
Grouping key for dedup.
bool transition_to(alert_state new_state)
Transition to a new state.
std::string name
Alert name/identifier.
bool is_active() const
Check if alert is currently active (pending or firing)
alert_labels labels
Identifying labels.
alert_annotations annotations
Descriptive annotations.
std::chrono::steady_clock::duration firing_duration() const
Get firing duration (if currently firing)
std::string fingerprint() const
Get alert fingerprint for deduplication.