2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSTEXTSERIALIZER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSTEXTSERIALIZER_HPP_INCLUDED
26 out.reserve(value.size());
27 for (
size_t i = 0; i < value.size(); ++i) {
28 const char c = value[i];
31 }
else if (c ==
'\n') {
45 out.reserve(value.size());
46 for (
size_t i = 0; i < value.size(); ++i) {
47 const char c = value[i];
50 }
else if (c ==
'"') {
52 }
else if (c ==
'\n') {
66 out.reserve(name.size());
67 for (
size_t i = 0; i < name.size(); ++i) {
68 const unsigned char c =
static_cast<unsigned char>(name[i]);
70 if (std::isalpha(c) || c ==
'_' || c ==
':') {
71 out +=
static_cast<char>(c);
76 if (std::isalnum(c) || c ==
'_' || c ==
':') {
77 out +=
static_cast<char>(c);
94 out.reserve(name.size());
95 for (
size_t i = 0; i < name.size(); ++i) {
96 const unsigned char c =
static_cast<unsigned char>(name[i]);
98 if (std::isalpha(c) || c ==
'_') {
99 out +=
static_cast<char>(c);
104 if (std::isalnum(c) || c ==
'_') {
105 out +=
static_cast<char>(c);
121 if (std::isnan(value)) {
123 }
else if (std::isinf(value)) {
130 os << std::setprecision(std::numeric_limits<double>::max_digits10) << value;
139 std::ostringstream& os,
149 const char* type_str =
"untyped";
150 switch (family.
type) {
155 os <<
"# TYPE " << safe_name <<
" " << type_str <<
"\n";
158 for (
size_t si = 0; si < family.
samples.size(); ++si) {
165 std::vector<PrometheusLabel> merged;
169 if (!sample.
labels.empty()) {
170 merged.insert(merged.end(), sample.
labels.begin(), sample.
labels.end());
173 if (!merged.empty()) {
175 for (
size_t li = 0; li < merged.size(); ++li) {
201 const std::vector<PrometheusMetricFamily>& families,
203 std::ostringstream os;
204 for (
size_t i = 0; i < families.size(); ++i) {
Defines Prometheus text exposition format types and configuration.
The primary namespace for the LogIt++ library.
std::string prometheus_escape_label_value(const std::string &value)
Escapes label value for Prometheus text format.
std::string prometheus_sanitize_metric_name(const std::string &name)
Sanitizes a metric name to match Prometheus naming rules.
std::string prometheus_sanitize_label_name(const std::string &name)
Sanitizes a label name to match Prometheus naming rules.
std::string prometheus_escape_help(const std::string &value)
Escapes help string for Prometheus text format.
void prometheus_format_value(std::ostringstream &os, double value)
Formats a Prometheus sample value according to text format rules.
std::string build_prometheus_text_payload(const std::vector< PrometheusMetricFamily > &families, const PrometheusTextFormatConfig &config)
Builds a complete Prometheus text exposition format payload.
void prometheus_write_metric_family(std::ostringstream &os, const PrometheusMetricFamily &family, const PrometheusTextFormatConfig &config)
Writes one PrometheusMetricFamily to an output stream.
A group of related samples sharing a name, help, and type.
std::vector< PrometheusSample > samples
Samples in this family.
std::string help
HELP text.
std::string name
Metric family name.
PrometheusMetricType type
Metric type.
A single metric sample with optional labels and timestamp.
std::string name
Full metric name (including suffixes).
std::vector< PrometheusLabel > labels
Labels for this sample.
double value
Sample value.
int64_t timestamp_ms
Timestamp in ms; 0 = omit.
Configuration for Prometheus text exposition format output.
bool include_timestamp
Emit sample timestamps.
bool include_help
Emit HELP lines.
bool include_type
Emit TYPE lines.
std::vector< PrometheusLabel > const_labels
Labels added to every sample.