2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSREGISTRY_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_PROMETHEUS_PROMETHEUSREGISTRY_HPP_INCLUDED
39 const std::string& name,
40 const std::string& help,
41 std::function<
double()> value_fn,
42 std::vector<PrometheusLabel> labels = {}) {
53 const std::string& name,
54 const std::string& help,
55 std::function<
double()> value_fn,
56 std::vector<PrometheusLabel> labels = {}) {
66 void collect(std::vector<PrometheusMetricFamily>& out)
const {
67 std::vector<PrometheusMetricFamily> collected;
68 std::exception_ptr first_exception;
70 for (std::size_t i = 0; i <
m_entries.size(); ++i) {
78 if (!first_exception) {
79 first_exception = std::current_exception();
86 if (family ==
nullptr) {
91 collected.push_back(std::move(mf));
92 family = &collected.back();
96 sample.
name = full_name;
99 family->
samples.push_back(std::move(sample));
102 out.insert(out.end(), collected.begin(), collected.end());
103 if (first_exception) {
104 std::rethrow_exception(first_exception);
122 const std::string& name,
123 const std::string& help,
124 std::function<
double()> value_fn,
125 std::vector<PrometheusLabel> labels) {
126 for (std::size_t i = 0; i <
m_entries.size(); ++i) {
131 entry.
value_fn = std::move(value_fn);
132 entry.
labels = std::move(labels);
141 entry.
value_fn = std::move(value_fn);
142 entry.
labels = std::move(labels);
147 const std::vector<PrometheusLabel>& lhs,
148 const std::vector<PrometheusLabel>& rhs) {
149 if (lhs.size() != rhs.size()) {
152 for (std::size_t i = 0; i < lhs.size(); ++i) {
153 if (lhs[i].name != rhs[i].name || lhs[i].value != rhs[i].value) {
161 std::vector<PrometheusMetricFamily>& families,
162 const std::string& name,
164 for (std::size_t i = 0; i < families.size(); ++i) {
165 if (families[i].name == name && families[i].type == type) {
Defines Prometheus text exposition format types and configuration.
Stores custom gauge and counter callbacks for Prometheus collection.
void collect(std::vector< PrometheusMetricFamily > &out) const
Appends current registry metrics to the output vector.
void set_metric_prefix(const std::string &metric_prefix)
Sets prefix applied to metrics collected by this registry.
static PrometheusMetricFamily * find_family(std::vector< PrometheusMetricFamily > &families, const std::string &name, PrometheusMetricType type)
PrometheusRegistry(std::string metric_prefix=std::string())
Constructs registry with an optional metric prefix.
std::string m_metric_prefix
static bool labels_equal(const std::vector< PrometheusLabel > &lhs, const std::vector< PrometheusLabel > &rhs)
std::vector< Entry > m_entries
void set_counter(const std::string &name, const std::string &help, std::function< double()> value_fn, std::vector< PrometheusLabel > labels={})
Registers or replaces a counter metric callback.
const std::string & metric_prefix() const
Returns prefix applied to metrics collected by this registry.
void set_metric(PrometheusMetricType type, const std::string &name, const std::string &help, std::function< double()> value_fn, std::vector< PrometheusLabel > labels)
void set_gauge(const std::string &name, const std::string &help, std::function< double()> value_fn, std::vector< PrometheusLabel > labels={})
Registers or replaces a gauge metric callback.
The primary namespace for the LogIt++ library.
PrometheusMetricType
Prometheus metric type identifiers.
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.
std::function< double()> value_fn
PrometheusMetricType type
std::vector< PrometheusLabel > labels
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.