22#include "util/log/Logger.hpp"
23#include "util/prometheus/Bool.hpp"
24#include "util/prometheus/Counter.hpp"
25#include "util/prometheus/Gauge.hpp"
26#include "util/prometheus/Histogram.hpp"
27#include "util/prometheus/Label.hpp"
28#include "util/prometheus/MetricBase.hpp"
29#include "util/prometheus/MetricsFamily.hpp"
36#include <unordered_map>
39namespace util::prometheus {
53 : isEnabled_(
isEnabled), compressReplyEnabled_(compressReply)
69 boolMetric(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt) = 0;
80 counterInt(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt) = 0;
91 counterDouble(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt) = 0;
102 gaugeInt(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt) = 0;
113 gaugeDouble(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt) = 0;
128 std::vector<std::int64_t>
const& buckets,
129 std::optional<std::string> description = std::nullopt
145 std::vector<double>
const& buckets,
146 std::optional<std::string> description = std::nullopt
176 return compressReplyEnabled_;
181 bool compressReplyEnabled_;
194 boolMetric(std::string name,
Labels labels, std::optional<std::string> description = std::nullopt)
override;
197 counterInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
200 counterDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
203 gaugeInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
206 gaugeDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
212 std::vector<std::int64_t>
const& buckets,
213 std::optional<std::string> description = std::nullopt
220 std::vector<double>
const& buckets,
221 std::optional<std::string> description = std::nullopt
229 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
232 getMetric(std::string name,
Labels labels, std::optional<std::string> description, MetricType type);
234 template <
typename ValueType>
235 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
240 std::optional<std::string> description,
242 std::vector<ValueType>
const& buckets
245 std::unordered_map<std::string, MetricsFamily> metrics_;
275 std::optional<std::string> description = std::nullopt
290 std::optional<std::string> description = std::nullopt
305 std::optional<std::string> description = std::nullopt
331 std::optional<std::string> description = std::nullopt
347 std::vector<std::int64_t>
const& buckets,
348 std::optional<std::string> description = std::nullopt
364 std::vector<double>
const& buckets,
365 std::optional<std::string> description = std::nullopt
400 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
411 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
Singleton class to access the PrometheusInterface.
Definition Prometheus.hpp:253
static bool compressReplyEnabled()
Whether to compress the reply.
Definition Prometheus.cpp:260
static void replaceInstance(std::unique_ptr< util::prometheus::PrometheusInterface > inst)
Replace the prometheus object stored in the singleton.
Definition Prometheus.cpp:266
static util::prometheus::CounterInt & counterInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:194
static util::prometheus::HistogramDouble & histogramDouble(std::string name, util::prometheus::Labels labels, std::vector< double > const &buckets, std::optional< std::string > description=std::nullopt)
Get a double based histogram metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:237
static util::prometheus::PrometheusInterface & instance()
Get the prometheus object stored in the singleton.
Definition Prometheus.cpp:272
static util::prometheus::CounterDouble & counterDouble(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a double based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:200
static util::prometheus::Bool boolMetric(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a bool based metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:188
static void init(util::config::ClioConfigDefinition const &config)
Initialize the singleton with the given configuration.
Definition Prometheus.cpp:179
static util::prometheus::GaugeDouble & gaugeDouble(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a double based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:216
static util::prometheus::HistogramInt & histogramInt(std::string name, util::prometheus::Labels labels, std::vector< std::int64_t > const &buckets, std::optional< std::string > description=std::nullopt)
Get an integer based histogram metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:226
static std::string collectMetrics()
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:248
static bool isEnabled()
Whether prometheus is enabled.
Definition Prometheus.cpp:254
static util::prometheus::GaugeInt & gaugeInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:210
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
A wrapped to provide bool interface for a Prometheus metric.
Definition Bool.hpp:41
A Prometheus histogram metric with a generic value type.
Definition Histogram.hpp:42
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:31
Class representing a collection of Prometheus metric with the same name and type.
Definition MetricsFamily.hpp:39
Implemetation of PrometheusInterface.
Definition Prometheus.hpp:189
CounterInt & counterInt(std::string name, Labels labels, std::optional< std::string > description) override
Get an integer based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:65
HistogramInt & histogramInt(std::string name, Labels labels, std::vector< std::int64_t > const &buckets, std::optional< std::string > description=std::nullopt) override
Get an integer based histogram metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:97
GaugeInt & gaugeInt(std::string name, Labels labels, std::optional< std::string > description) override
Get an integer based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:81
GaugeDouble & gaugeDouble(std::string name, Labels labels, std::optional< std::string > description) override
Get a double based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:89
std::string collectMetrics() override
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:123
Bool boolMetric(std::string name, Labels labels, std::optional< std::string > description=std::nullopt) override
Get a bool based metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:58
HistogramDouble & histogramDouble(std::string name, Labels labels, std::vector< double > const &buckets, std::optional< std::string > description=std::nullopt) override
Get a double based histogram metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:110
CounterDouble & counterDouble(std::string name, Labels labels, std::optional< std::string > description) override
Get a double based counter metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:73
Interface for prometheus (https://prometheus.io/)
Definition Prometheus.hpp:44
virtual std::string collectMetrics()=0
Collect all metrics and return them as a string in Prometheus format.
bool isEnabled() const
Whether prometheus is enabled.
Definition Prometheus.hpp:163
virtual Bool boolMetric(std::string name, Labels labels, std::optional< std::string > description=std::nullopt)=0
Get a bool based metric. It will be created if it doesn't exist.
virtual CounterInt & counterInt(std::string name, Labels labels, std::optional< std::string > description=std::nullopt)=0
Get an integer based counter metric. It will be created if it doesn't exist.
bool compressReplyEnabled() const
Whether to compress the reply.
Definition Prometheus.hpp:174
virtual CounterDouble & counterDouble(std::string name, Labels labels, std::optional< std::string > description=std::nullopt)=0
Get a double based counter metric. It will be created if it doesn't exist.
virtual GaugeDouble & gaugeDouble(std::string name, Labels labels, std::optional< std::string > description=std::nullopt)=0
Get a double based gauge metric. It will be created if it doesn't exist.
virtual GaugeInt & gaugeInt(std::string name, Labels labels, std::optional< std::string > description=std::nullopt)=0
Get an integer based gauge metric. It will be created if it doesn't exist.
virtual HistogramInt & histogramInt(std::string name, Labels labels, std::vector< std::int64_t > const &buckets, std::optional< std::string > description=std::nullopt)=0
Get an integer based histogram metric. It will be created if it doesn't exist.
PrometheusInterface(bool isEnabled, bool compressReply)
Construct a new Prometheus Interface object.
Definition Prometheus.hpp:52
virtual HistogramDouble & histogramDouble(std::string name, Labels labels, std::vector< double > const &buckets, std::optional< std::string > description=std::nullopt)=0
Get a double based histogram metric. It will be created if it doesn't exist.
A prometheus counter metric implementation. It can only be increased or be reset to zero.
Definition Counter.hpp:40
A prometheus gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:39