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 bool
164 isEnabled() const
165 {
166 return isEnabled_;
167 }
168
174 bool
176 {
177 return compressReplyEnabled_;
178 }
179
180private:
181 bool isEnabled_;
182 bool compressReplyEnabled_;
183};
184
191public:
193
194 Bool
196 std::string name,
197 Labels labels,
198 std::optional<std::string> description = std::nullopt
199 ) override;
200
201 CounterInt&
202 counterInt(std::string name, Labels labels, std::optional<std::string> description) override;
203
204 CounterDouble&
205 counterDouble(std::string name, Labels labels, std::optional<std::string> description) override;
206
207 GaugeInt&
208 gaugeInt(std::string name, Labels labels, std::optional<std::string> description) override;
209
210 GaugeDouble&
211 gaugeDouble(std::string name, Labels labels, std::optional<std::string> description) override;
212
213 HistogramInt&
215 std::string name,
216 Labels labels,
217 std::vector<std::int64_t> const& buckets,
218 std::optional<std::string> description = std::nullopt
219 ) override;
220
221 HistogramDouble&
223 std::string name,
224 Labels labels,
225 std::vector<double> const& buckets,
226 std::optional<std::string> description = std::nullopt
227 ) override;
228
229 std::string
230 collectMetrics() override;
231
232private:
234 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
235
237 getMetric(
238 std::string name,
239 Labels labels,
240 std::optional<std::string> description,
241 MetricType type
242 );
243
244 template <typename ValueType>
245 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
247 getMetric(
248 std::string name,
249 Labels labels,
250 std::optional<std::string> description,
251 MetricType type,
252 std::vector<ValueType> const& buckets
253 );
254
255 std::unordered_map<std::string, MetricsFamily> metrics_;
256};
257
258} // namespace util::prometheus
259
264public:
270 static void
272
278 static bool
280
290 static util::prometheus::Bool
292 std::string name,
294 std::optional<std::string> description = std::nullopt
295 );
296
305 static util::prometheus::CounterInt&
307 std::string name,
309 std::optional<std::string> description = std::nullopt
310 );
311
320 static util::prometheus::CounterDouble&
322 std::string name,
324 std::optional<std::string> description = std::nullopt
325 );
326
335 static util::prometheus::GaugeInt&
336 gaugeInt(
337 std::string name,
339 std::optional<std::string> description = std::nullopt
340 );
341
350 static util::prometheus::GaugeDouble&
352 std::string name,
354 std::optional<std::string> description = std::nullopt
355 );
356
366 static util::prometheus::HistogramInt&
368 std::string name,
370 std::vector<std::int64_t> const& buckets,
371 std::optional<std::string> description = std::nullopt
372 );
373
383 static util::prometheus::HistogramDouble&
385 std::string name,
387 std::vector<double> const& buckets,
388 std::optional<std::string> description = std::nullopt
389 );
390
396 static std::string
398
404 static bool
405 isEnabled();
406
412 static bool
414
422 static void
423 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
424
431 instance();
432
433private:
434 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
435};
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.