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

Prometheus-specific metric representation. More...

#include <metric_exporters.h>

Collaboration diagram for kcenon::monitoring::prometheus_metric_data:
Collaboration graph

Public Member Functions

std::string to_prometheus_text () const
 Convert to Prometheus text format.
 

Public Attributes

std::string name
 
metric_type type
 
double value
 
std::chrono::system_clock::time_point timestamp
 
std::unordered_map< std::string, std::string > labels
 
std::string help_text
 

Private Member Functions

std::string escape_label_value (const std::string &label_value) const
 

Detailed Description

Prometheus-specific metric representation.

Definition at line 122 of file metric_exporters.h.

Member Function Documentation

◆ escape_label_value()

std::string kcenon::monitoring::prometheus_metric_data::escape_label_value ( const std::string & label_value) const
inlineprivate

Definition at line 180 of file metric_exporters.h.

180 {
181 std::string escaped = label_value;
182 // Escape backslashes, quotes, and newlines
183 escaped = std::regex_replace(escaped, std::regex("\\\\"), "\\\\");
184 escaped = std::regex_replace(escaped, std::regex("\""), "\\\"");
185 escaped = std::regex_replace(escaped, std::regex("\n"), "\\n");
186 return escaped;
187 }

Referenced by to_prometheus_text().

Here is the caller graph for this function:

◆ to_prometheus_text()

std::string kcenon::monitoring::prometheus_metric_data::to_prometheus_text ( ) const
inline

Convert to Prometheus text format.

Definition at line 133 of file metric_exporters.h.

133 {
134 std::ostringstream ss;
135
136 // Add HELP line
137 if (!help_text.empty()) {
138 ss << "# HELP " << name << " " << help_text << "\n";
139 }
140
141 // Add TYPE line
142 std::string type_str;
143 switch (type) {
144 case metric_type::counter: type_str = "counter"; break;
145 case metric_type::gauge: type_str = "gauge"; break;
146 case metric_type::histogram: type_str = "histogram"; break;
147 case metric_type::summary: type_str = "summary"; break;
148 case metric_type::timer: type_str = "gauge"; break; // Timer as gauge in Prometheus
149 }
150 ss << "# TYPE " << name << " " << type_str << "\n";
151
152 // Add metric line
153 ss << name;
154
155 if (!labels.empty()) {
156 ss << "{";
157 bool first = true;
158 for (const auto& [key, label_value] : labels) {
159 if (!first) ss << ",";
160 ss << key << "=\"" << escape_label_value(label_value) << "\"";
161 first = false;
162 }
163 ss << "}";
164 }
165
166 ss << " " << value;
167
168 // Add timestamp if available
169 if (timestamp != std::chrono::system_clock::time_point{}) {
170 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
171 timestamp.time_since_epoch()).count();
172 ss << " " << ms;
173 }
174
175 ss << "\n";
176 return ss.str();
177 }
@ timer
StatsD-specific timer metric.
@ gauge
Instantaneous value that can go up and down.
@ counter
Monotonically increasing counter.
@ summary
Pre-calculated quantiles and count/sum.
@ histogram
Distribution of values with buckets.
std::chrono::system_clock::time_point timestamp
std::unordered_map< std::string, std::string > labels
std::string escape_label_value(const std::string &label_value) const

References kcenon::monitoring::counter, escape_label_value(), kcenon::monitoring::gauge, help_text, kcenon::monitoring::histogram, labels, name, kcenon::monitoring::summary, kcenon::monitoring::timer, timestamp, type, and value.

Here is the call graph for this function:

Member Data Documentation

◆ help_text

std::string kcenon::monitoring::prometheus_metric_data::help_text

Definition at line 128 of file metric_exporters.h.

Referenced by to_prometheus_text().

◆ labels

std::unordered_map<std::string, std::string> kcenon::monitoring::prometheus_metric_data::labels

Definition at line 127 of file metric_exporters.h.

Referenced by to_prometheus_text().

◆ name

std::string kcenon::monitoring::prometheus_metric_data::name

Definition at line 123 of file metric_exporters.h.

Referenced by to_prometheus_text().

◆ timestamp

std::chrono::system_clock::time_point kcenon::monitoring::prometheus_metric_data::timestamp

Definition at line 126 of file metric_exporters.h.

Referenced by to_prometheus_text().

◆ type

metric_type kcenon::monitoring::prometheus_metric_data::type

Definition at line 124 of file metric_exporters.h.

Referenced by to_prometheus_text().

◆ value

double kcenon::monitoring::prometheus_metric_data::value

Definition at line 125 of file metric_exporters.h.

Referenced by to_prometheus_text().


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