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)
72 std::optional<std::string> description = std::nullopt
87 std::optional<std::string> description = std::nullopt
98 virtual CounterDouble&
102 std::optional<std::string> description = std::nullopt
117 std::optional<std::string> description = std::nullopt
132 std::optional<std::string> description = std::nullopt
144 virtual HistogramInt&
148 std::vector<std::int64_t>
const& buckets,
149 std::optional<std::string> description = std::nullopt
161 virtual HistogramDouble&
165 std::vector<double>
const& buckets,
166 std::optional<std::string> description = std::nullopt
196 return compressReplyEnabled_;
201 bool compressReplyEnabled_;
217 std::optional<std::string> description = std::nullopt
221 counterInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
224 counterDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
227 gaugeInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
230 gaugeDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
236 std::vector<std::int64_t>
const& buckets,
237 std::optional<std::string> description = std::nullopt
244 std::vector<double>
const& buckets,
245 std::optional<std::string> description = std::nullopt
253 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
259 std::optional<std::string> description,
263 template <
typename ValueType>
264 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
269 std::optional<std::string> description,
271 std::vector<ValueType>
const& buckets
274 std::unordered_map<std::string, MetricsFamily> metrics_;
309 static util::prometheus::Bool
313 std::optional<std::string> description = std::nullopt
324 static util::prometheus::CounterInt&
328 std::optional<std::string> description = std::nullopt
339 static util::prometheus::CounterDouble&
343 std::optional<std::string> description = std::nullopt
354 static util::prometheus::GaugeInt&
358 std::optional<std::string> description = std::nullopt
369 static util::prometheus::GaugeDouble&
373 std::optional<std::string> description = std::nullopt
385 static util::prometheus::HistogramInt&
389 std::vector<std::int64_t>
const& buckets,
390 std::optional<std::string> description = std::nullopt
402 static util::prometheus::HistogramDouble&
406 std::vector<double>
const& buckets,
407 std::optional<std::string> description = std::nullopt
442 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
453 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
Singleton class to access the PrometheusInterface.
Definition Prometheus.hpp:282
static bool compressReplyEnabled()
Whether to compress the reply.
Definition Prometheus.cpp:308
static void replaceInstance(std::unique_ptr< util::prometheus::PrometheusInterface > inst)
Replace the prometheus object stored in the singleton.
Definition Prometheus.cpp:314
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:230
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:283
static util::prometheus::PrometheusInterface & instance()
Get the prometheus object stored in the singleton.
Definition Prometheus.cpp:320
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:240
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:220
static bool isInitialised()
Whether the singleton has been already initialised.
Definition Prometheus.cpp:214
static void init(util::config::ClioConfigDefinition const &config)
Initialize the singleton with the given configuration.
Definition Prometheus.cpp:205
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:260
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:270
static std::string collectMetrics()
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:296
static bool isEnabled()
Whether prometheus is enabled.
Definition Prometheus.cpp:302
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:250
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
Class representing a collection of Prometheus labels.
Definition Label.hpp:60
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
Implementation of PrometheusInterface.
Definition Prometheus.hpp:209
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:104
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:87
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:95
std::string collectMetrics() override
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:140
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:122
PrometheusInterface(bool isEnabled, bool compressReply)
Construct a new Prometheus Interface object.
Definition Prometheus.hpp:52
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:74
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:183
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:194
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.