LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
PrometheusLoggerMetrics.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSLOGGERMETRICS_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSLOGGERMETRICS_HPP_INCLUDED
4
7
9
10#include <atomic>
11#include <cstdint>
12#include <limits>
13#include <string>
14#include <vector>
15
16namespace logit {
17
21 public:
23 void on_log(int64_t timestamp_ms) {
24 m_last_log_ts.store(timestamp_ms);
26 }
27
29 void add_dropped(uint64_t count = 1) {
30 m_dropped.fetch_add(count);
31 }
32
34 void add_failed_export(uint64_t count = 1) {
35 m_failed_exports.fetch_add(count);
36 }
37
39 uint64_t log_records_total() const {
40 return m_log_records_total.load();
41 }
42
44 uint64_t dropped_count() const {
45 return m_dropped.load();
46 }
47
49 uint64_t failed_export_count() const {
50 return m_failed_exports.load();
51 }
52
54 int64_t last_log_ts() const {
55 return m_last_log_ts.load();
56 }
57
60 int64_t time_since_last_log_ms(int64_t now_ms) const {
61 const int64_t last = last_log_ts();
62 if (last <= 0) {
63 return 0;
64 }
65 return now_ms > last ? now_ms - last : 0;
66 }
67
74 std::vector<PrometheusMetricFamily>& families,
75 const PrometheusTextFormatConfig& config,
76 const std::string& logger_name,
77 int64_t now_ms = 0) const {
78
79 const std::string& prefix = config.metric_prefix;
80
81 // logit_log_records_total (counter)
82 {
84 mf.name = prefix + "log_records_total";
85 mf.help = "Total number of log records processed";
88 s.name = prefix + "log_records_total";
89 s.value = static_cast<double>(m_log_records_total.load());
90 add_common_labels(s, config, logger_name);
91 mf.samples.push_back(s);
92 families.push_back(mf);
93 }
94
95 // logit_dropped_logs_total (counter)
96 {
98 mf.name = prefix + "dropped_logs_total";
99 mf.help = "Total number of dropped log records";
102 s.name = prefix + "dropped_logs_total";
103 s.value = static_cast<double>(m_dropped.load());
104 add_common_labels(s, config, logger_name);
105 mf.samples.push_back(s);
106 families.push_back(mf);
107 }
108
109 // logit_failed_exports_total (counter)
110 {
112 mf.name = prefix + "failed_exports_total";
113 mf.help = "Total number of failed export attempts";
116 s.name = prefix + "failed_exports_total";
117 s.value = static_cast<double>(m_failed_exports.load());
118 add_common_labels(s, config, logger_name);
119 mf.samples.push_back(s);
120 families.push_back(mf);
121 }
122
123 // logit_last_log_timestamp_ms (gauge)
124 {
126 mf.name = prefix + "last_log_timestamp_ms";
127 mf.help = "Timestamp of the last log record in milliseconds";
130 s.name = prefix + "last_log_timestamp_ms";
131 s.value = static_cast<double>(m_last_log_ts.load());
132 add_common_labels(s, config, logger_name);
133 mf.samples.push_back(s);
134 families.push_back(mf);
135 }
136
137 // logit_time_since_last_log_ms (gauge)
138 {
140 mf.name = prefix + "time_since_last_log_ms";
141 mf.help = "Milliseconds since the last log record";
144 s.name = prefix + "time_since_last_log_ms";
145 s.value = static_cast<double>(time_since_last_log_ms(now_ms));
146 add_common_labels(s, config, logger_name);
147 mf.samples.push_back(s);
148 families.push_back(mf);
149 }
150
151 // logit_build_info (gauge, value=1) with version/compiler labels
152 if (config.include_build_info) {
154 mf.name = prefix + "build_info";
155 mf.help = "Build information for logit-cpp";
158 s.name = prefix + "build_info";
159 s.value = 1.0;
160#ifdef LOGIT_VERSION
161 s.labels.push_back({"version", LOGIT_VERSION});
162#else
163 s.labels.push_back({"version", "1.0.2"});
164#endif
165#if defined(__GNUC__) && !defined(__clang__)
166 s.labels.push_back({"compiler", "gcc"});
167#elif defined(__clang__)
168 s.labels.push_back({"compiler", "clang"});
169#elif defined(_MSC_VER)
170 s.labels.push_back({"compiler", "msvc"});
171#else
172 s.labels.push_back({"compiler", "unknown"});
173#endif
174 add_common_labels(s, config, logger_name);
175 mf.samples.push_back(s);
176 families.push_back(mf);
177 }
178 }
179
181 static int64_t counter_to_int64(uint64_t value) {
182 const uint64_t max_value = static_cast<uint64_t>((std::numeric_limits<int64_t>::max)());
183 return value > max_value ? (std::numeric_limits<int64_t>::max)() : static_cast<int64_t>(value);
184 }
185
186 private:
187 std::atomic<uint64_t> m_log_records_total{0};
188 std::atomic<uint64_t> m_dropped{0};
189 std::atomic<uint64_t> m_failed_exports{0};
190 std::atomic<int64_t> m_last_log_ts{0};
191
192 static void add_common_labels(
193 PrometheusSample& sample,
194 const PrometheusTextFormatConfig& config,
195 const std::string& logger_name) {
196 if (config.include_logger_label) {
197 sample.labels.push_back(
198 {config.logger_label_name, logger_name});
199 }
200 if (config.include_instance_label) {
201 sample.labels.push_back(
203 }
204 }
205 };
206
207} // namespace logit
208
209#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSLOGGERMETRICS_HPP_INCLUDED
Defines Prometheus text exposition format types and configuration.
Tracks log-record counters and builds built-in Prometheus metric families.
void add_failed_export(uint64_t count=1)
Increment failed export/collect counter.
uint64_t dropped_count() const
Total dropped log records.
uint64_t log_records_total() const
Total log records processed.
int64_t last_log_ts() const
Last log timestamp (ms).
uint64_t failed_export_count() const
Total failed export/collect attempts.
void add_dropped(uint64_t count=1)
Increment dropped log counter.
static void add_common_labels(PrometheusSample &sample, const PrometheusTextFormatConfig &config, const std::string &logger_name)
static int64_t counter_to_int64(uint64_t value)
Safely convert uint64_t counter to int64_t (clamping on overflow).
int64_t time_since_last_log_ms(int64_t now_ms) const
Milliseconds since last log record (0 if none).
void build_builtin_metrics(std::vector< PrometheusMetricFamily > &families, const PrometheusTextFormatConfig &config, const std::string &logger_name, int64_t now_ms=0) const
Append the six built-in metric families to families.
void on_log(int64_t timestamp_ms)
Update metrics when a log record is processed.
The primary namespace for the LogIt++ library.
A group of related samples sharing a name, help, and type.
std::vector< PrometheusSample > samples
Samples in this family.
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.
Configuration for Prometheus text exposition format output.
std::string logger_label_name
Name of logger label.
bool include_build_info
Emit logit_build_info metric.
std::string instance_label_value
Value of instance label.
bool include_instance_label
Add instance label to built-in metrics.
std::string instance_label_name
Name of instance label.
bool include_logger_label
Add logger label to built-in metrics.
std::string metric_prefix
Prefix applied to built-in metric names.