3#include "util/log/Logger.hpp"
4#include "util/prometheus/Bool.hpp"
5#include "util/prometheus/Counter.hpp"
6#include "util/prometheus/Gauge.hpp"
7#include "util/prometheus/Histogram.hpp"
8#include "util/prometheus/Label.hpp"
9#include "util/prometheus/MetricBase.hpp"
10#include "util/prometheus/MetricsFamily.hpp"
17#include <unordered_map>
20namespace util::prometheus {
34 : isEnabled_(
isEnabled), compressReplyEnabled_(compressReply)
53 std::optional<std::string> description = std::nullopt
68 std::optional<std::string> description = std::nullopt
79 virtual CounterDouble&
83 std::optional<std::string> description = std::nullopt
98 std::optional<std::string> description = std::nullopt
113 std::optional<std::string> description = std::nullopt
125 virtual HistogramInt&
129 std::vector<std::int64_t>
const& buckets,
130 std::optional<std::string> description = std::nullopt
142 virtual HistogramDouble&
146 std::vector<double>
const& buckets,
147 std::optional<std::string> description = std::nullopt
177 return compressReplyEnabled_;
182 bool compressReplyEnabled_;
198 std::optional<std::string> description = std::nullopt
202 counterInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
205 counterDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
208 gaugeInt(std::string name,
Labels labels, std::optional<std::string> description)
override;
211 gaugeDouble(std::string name,
Labels labels, std::optional<std::string> description)
override;
217 std::vector<std::int64_t>
const& buckets,
218 std::optional<std::string> description = std::nullopt
225 std::vector<double>
const& buckets,
226 std::optional<std::string> description = std::nullopt
234 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
240 std::optional<std::string> description,
244 template <
typename ValueType>
245 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
250 std::optional<std::string> description,
252 std::vector<ValueType>
const& buckets
255 std::unordered_map<std::string, MetricsFamily> metrics_;
290 static util::prometheus::Bool
294 std::optional<std::string> description = std::nullopt
305 static util::prometheus::CounterInt&
309 std::optional<std::string> description = std::nullopt
320 static util::prometheus::CounterDouble&
324 std::optional<std::string> description = std::nullopt
335 static util::prometheus::GaugeInt&
339 std::optional<std::string> description = std::nullopt
350 static util::prometheus::GaugeDouble&
354 std::optional<std::string> description = std::nullopt
366 static util::prometheus::HistogramInt&
370 std::vector<std::int64_t>
const& buckets,
371 std::optional<std::string> description = std::nullopt
383 static util::prometheus::HistogramDouble&
387 std::vector<double>
const& buckets,
388 std::optional<std::string> description = std::nullopt
423 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
434 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
Singleton class to access the PrometheusInterface.
Definition Prometheus.hpp:263
static bool compressReplyEnabled()
Whether to compress the reply.
Definition Prometheus.cpp:289
static void replaceInstance(std::unique_ptr< util::prometheus::PrometheusInterface > inst)
Replace the prometheus object stored in the singleton.
Definition Prometheus.cpp:295
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:211
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:264
static util::prometheus::PrometheusInterface & instance()
Get the prometheus object stored in the singleton.
Definition Prometheus.cpp:301
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:221
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:201
static bool isInitialised()
Whether the singleton has been already initialised.
Definition Prometheus.cpp:195
static void init(util::config::ClioConfigDefinition const &config)
Initialize the singleton with the given configuration.
Definition Prometheus.cpp:186
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:241
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:251
static std::string collectMetrics()
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:277
static bool isEnabled()
Whether prometheus is enabled.
Definition Prometheus.cpp:283
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:231
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
Class representing a collection of Prometheus labels.
Definition Label.hpp:41
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:12
Class representing a collection of Prometheus metric with the same name and type.
Definition MetricsFamily.hpp:20
Implementation of PrometheusInterface.
Definition Prometheus.hpp:190
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:46
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:85
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:68
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:76
std::string collectMetrics() override
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:121
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:39
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:103
PrometheusInterface(bool isEnabled, bool compressReply)
Construct a new Prometheus Interface object.
Definition Prometheus.hpp:33
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:55
Interface for prometheus (https://prometheus.io/).
Definition Prometheus.hpp:25
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:164
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:175
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:33
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.