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

Template engine for alert messages. More...

#include <alert_config.h>

Collaboration diagram for kcenon::monitoring::alert_template:
Collaboration graph

Public Member Functions

 alert_template (std::string template_str)
 Construct with template string.
 
void set (const std::string &key, const std::string &value)
 Set a custom variable value.
 
std::string render (const alert &a) const
 Render template with alert data.
 
const std::string & template_string () const
 Get template string.
 
common::VoidResult validate () const
 Validate template syntax.
 

Static Private Member Functions

static std::string format_value (double value)
 
static std::string substitute_variables (const std::string &input, const std::unordered_map< std::string, std::string > &vars)
 

Private Attributes

std::string template_str_
 
std::unordered_map< std::string, std::string > custom_vars_
 

Detailed Description

Template engine for alert messages.

Supports variable substitution in alert summaries and descriptions using ${variable} syntax. Variables can include alert properties, labels, and annotations.

Built-in variables:

  • ${name} - Alert name
  • ${state} - Alert state (pending, firing, resolved)
  • ${severity} - Alert severity
  • ${value} - Current metric value
  • ${threshold} - Trigger threshold (if applicable)
  • ${labels.X} - Label value for key X
  • ${annotations.X} - Annotation value for key X

Definition at line 58 of file alert_config.h.

Constructor & Destructor Documentation

◆ alert_template()

kcenon::monitoring::alert_template::alert_template ( std::string template_str)
inlineexplicit

Construct with template string.

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

Definition at line 63 of file alert_config.h.

64 : template_str_(std::move(template_str)) {}

Member Function Documentation

◆ format_value()

static std::string kcenon::monitoring::alert_template::format_value ( double value)
inlinestaticprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 138 of file alert_config.h.

138 {
139 std::ostringstream oss;
140 oss << std::fixed << std::setprecision(2) << value;
141 return oss.str();
142 }

Referenced by render().

Here is the caller graph for this function:

◆ render()

std::string kcenon::monitoring::alert_template::render ( const alert & a) const
inline

Render template with alert data.

Parameters
aAlert providing context
Returns
Rendered string
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 78 of file alert_config.h.

78 {
79 std::string result = template_str_;
80
81 // Build variable map
82 std::unordered_map<std::string, std::string> vars;
83
84 // Alert properties
85 vars["name"] = a.name;
86 vars["state"] = alert_state_to_string(a.state);
87 vars["severity"] = alert_severity_to_string(a.severity);
88 vars["value"] = format_value(a.value);
89 vars["fingerprint"] = a.fingerprint();
90 vars["rule_name"] = a.rule_name;
91 vars["group_key"] = a.group_key;
92
93 // Labels
94 for (const auto& [key, value] : a.labels.labels) {
95 vars["labels." + key] = value;
96 }
97
98 // Annotations
99 vars["annotations.summary"] = a.annotations.summary;
100 vars["annotations.description"] = a.annotations.description;
101 if (a.annotations.runbook_url) {
102 vars["annotations.runbook_url"] = *a.annotations.runbook_url;
103 }
104 for (const auto& [key, value] : a.annotations.custom) {
105 vars["annotations." + key] = value;
106 }
107
108 // Custom variables
109 for (const auto& [key, value] : custom_vars_) {
110 vars[key] = value;
111 }
112
113 // Substitute variables
114 result = substitute_variables(result, vars);
115
116 return result;
117 }
static std::string format_value(double value)
static std::string substitute_variables(const std::string &input, const std::unordered_map< std::string, std::string > &vars)
std::unordered_map< std::string, std::string > custom_vars_
constexpr const char * alert_state_to_string(alert_state state) noexcept
Convert alert state to string.
Definition alert_types.h:82
constexpr const char * alert_severity_to_string(alert_severity severity) noexcept
Convert alert severity to string.
Definition alert_types.h:47

References kcenon::monitoring::alert_severity_to_string(), kcenon::monitoring::alert_state_to_string(), kcenon::monitoring::alert::annotations, kcenon::monitoring::alert_annotations::custom, custom_vars_, kcenon::monitoring::alert_annotations::description, kcenon::monitoring::alert::fingerprint(), format_value(), kcenon::monitoring::alert::group_key, kcenon::monitoring::alert::labels, kcenon::monitoring::alert_labels::labels, kcenon::monitoring::alert::name, kcenon::monitoring::alert::rule_name, kcenon::monitoring::alert_annotations::runbook_url, kcenon::monitoring::alert::severity, kcenon::monitoring::alert::state, substitute_variables(), kcenon::monitoring::alert_annotations::summary, template_str_, and kcenon::monitoring::alert::value.

Here is the call graph for this function:

◆ set()

void kcenon::monitoring::alert_template::set ( const std::string & key,
const std::string & value )
inline

Set a custom variable value.

Definition at line 69 of file alert_config.h.

69 {
70 custom_vars_[key] = value;
71 }

References custom_vars_.

◆ substitute_variables()

static std::string kcenon::monitoring::alert_template::substitute_variables ( const std::string & input,
const std::unordered_map< std::string, std::string > & vars )
inlinestaticprivate
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 144 of file alert_config.h.

146 {
147 std::string result = input;
148 std::regex pattern(R"(\$\{([^}]+)\})");
149
150 std::string::const_iterator start = result.cbegin();
151 std::smatch match;
152 std::string output;
153
154 while (std::regex_search(start, result.cend(), match, pattern)) {
155 output += std::string(start, match[0].first);
156
157 std::string var_name = match[1].str();
158 auto it = vars.find(var_name);
159 if (it != vars.end()) {
160 output += it->second;
161 } else {
162 output += match[0].str(); // Keep original if not found
163 }
164
165 start = match[0].second;
166 }
167 output += std::string(start, result.cend());
168
169 return output;
170 }

Referenced by render().

Here is the caller graph for this function:

◆ template_string()

const std::string & kcenon::monitoring::alert_template::template_string ( ) const
inline

◆ validate()

common::VoidResult kcenon::monitoring::alert_template::validate ( ) const
inline

Validate template syntax.

Returns
Result with validation errors if any
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/alert/alert_config.h.

Definition at line 128 of file alert_config.h.

128 {
129 // Check for unclosed variable references
130 std::regex pattern(R"(\$\{[^}]*$)");
131 if (std::regex_search(template_str_, pattern)) {
132 return common::VoidResult::err(error_info(monitoring_error_code::validation_failed, "Unclosed variable reference in template").to_common_error());
133 }
134 return common::ok();
135 }

References template_str_, and kcenon::monitoring::validation_failed.

Member Data Documentation

◆ custom_vars_

std::unordered_map<std::string, std::string> kcenon::monitoring::alert_template::custom_vars_
private

◆ template_str_

std::string kcenon::monitoring::alert_template::template_str_
private

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