Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Prometheus.hpp
1#pragma once
2
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"
11
12#include <concepts>
13#include <cstdint>
14#include <memory>
15#include <optional>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20namespace util::prometheus {
21
26public:
33 PrometheusInterface(bool isEnabled, bool compressReply)
34 : isEnabled_(isEnabled), compressReplyEnabled_(compressReply)
35 {
36 }
37
38 virtual ~PrometheusInterface() = default;
39
49 virtual Bool
51 std::string name,
52 Labels labels,
53 std::optional<std::string> description = std::nullopt
54 ) = 0;
55
64 virtual CounterInt&
66 std::string name,
67 Labels labels,
68 std::optional<std::string> description = std::nullopt
69 ) = 0;
70
79 virtual CounterDouble&
81 std::string name,
82 Labels labels,
83 std::optional<std::string> description = std::nullopt
84 ) = 0;
85
94 virtual GaugeInt&
96 std::string name,
97 Labels labels,
98 std::optional<std::string> description = std::nullopt
99 ) = 0;
100
109 virtual GaugeDouble&
111 std::string name,
112 Labels labels,
113 std::optional<std::string> description = std::nullopt
114 ) = 0;
115
125 virtual HistogramInt&
127 std::string name,
128 Labels labels,
129 std::vector<std::int64_t> const& buckets,
130 std::optional<std::string> description = std::nullopt
131 ) = 0;
132
142 virtual HistogramDouble&
144 std::string name,
145 Labels labels,
146 std::vector<double> const& buckets,
147 std::optional<std::string> description = std::nullopt
148 ) = 0;
149
155 virtual std::string
157
163 [[nodiscard]] bool
164 isEnabled() const
165 {
166 return isEnabled_;
167 }
168
174 [[nodiscard]] bool
176 {
177 return compressReplyEnabled_;
178 }
179
180private:
181 bool isEnabled_;
182 bool compressReplyEnabled_;
183};
184
191public:
193
202 Bool
204 std::string name,
205 Labels labels,
206 std::optional<std::string> description = std::nullopt
207 ) override;
208
218 counterInt(std::string name, Labels labels, std::optional<std::string> description) override;
219
229 counterDouble(std::string name, Labels labels, std::optional<std::string> description) override;
230
239 GaugeInt&
240 gaugeInt(std::string name, Labels labels, std::optional<std::string> description) override;
241
251 gaugeDouble(std::string name, Labels labels, std::optional<std::string> description) override;
252
264 std::string name,
265 Labels labels,
266 std::vector<std::int64_t> const& buckets,
267 std::optional<std::string> description = std::nullopt
268 ) override;
269
281 std::string name,
282 Labels labels,
283 std::vector<double> const& buckets,
284 std::optional<std::string> description = std::nullopt
285 ) override;
286
293 std::string
294 collectMetrics() override;
295
296private:
298 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
299
301 getMetric(
302 std::string name,
303 Labels labels,
304 std::optional<std::string> description,
305 MetricType type
306 );
307
308 template <typename ValueType>
309 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
311 getMetric(
312 std::string name,
313 Labels labels,
314 std::optional<std::string> description,
315 MetricType type,
316 std::vector<ValueType> const& buckets
317 );
318
319 std::unordered_map<std::string, MetricsFamily> metrics_;
320};
321
322} // namespace util::prometheus
323
328public:
334 static void
336
342 static bool
344
356 std::string name,
358 std::optional<std::string> description = std::nullopt
359 );
360
371 std::string name,
373 std::optional<std::string> description = std::nullopt
374 );
375
386 std::string name,
388 std::optional<std::string> description = std::nullopt
389 );
390
400 gaugeInt(
401 std::string name,
403 std::optional<std::string> description = std::nullopt
404 );
405
416 std::string name,
418 std::optional<std::string> description = std::nullopt
419 );
420
432 std::string name,
434 std::vector<std::int64_t> const& buckets,
435 std::optional<std::string> description = std::nullopt
436 );
437
449 std::string name,
451 std::vector<double> const& buckets,
452 std::optional<std::string> description = std::nullopt
453 );
454
460 static std::string
462
468 static bool
469 isEnabled();
470
476 static bool
478
486 static void
487 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
488
495 instance();
496
497private:
498 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
499};
Singleton class to access the PrometheusInterface.
Definition Prometheus.hpp:327
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.
Prometheus metrics: counters, gauges, histograms and their registry and serialization.
Definition Bool.hpp:12
AnyCounter< std::uint64_t > CounterInt
A prometheus counter metric implementation with a value of type std::int64_t.
Definition Counter.hpp:114
AnyCounter< double > CounterDouble
A prometheus counter metric implementation with a value of type double.
Definition Counter.hpp:119
AnyBool< GaugeInt > Bool
Alias for Prometheus bool metric with GaugeInt implementation.
Definition Bool.hpp:74
AnyGauge< std::int64_t > GaugeInt
Alias for a gauge with a 64-bit integer value.
Definition Gauge.hpp:127
MetricType
The kind and value type of a Prometheus metric.
Definition MetricBase.hpp:72
AnyHistogram< double > HistogramDouble
Histogram of floating point values.
Definition Histogram.hpp:135
AnyGauge< double > GaugeDouble
Alias for a gauge with a double value.
Definition Gauge.hpp:132
AnyHistogram< std::int64_t > HistogramInt
Histogram of integer values.
Definition Histogram.hpp:134