Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Prometheus.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
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"
30
31#include <concepts>
32#include <cstdint>
33#include <memory>
34#include <optional>
35#include <string>
36#include <unordered_map>
37#include <vector>
38
39namespace util::prometheus {
40
45public:
52 PrometheusInterface(bool isEnabled, bool compressReply)
53 : isEnabled_(isEnabled), compressReplyEnabled_(compressReply)
54 {
55 }
56
57 virtual ~PrometheusInterface() = default;
58
68 virtual Bool
70 std::string name,
71 Labels labels,
72 std::optional<std::string> description = std::nullopt
73 ) = 0;
74
83 virtual CounterInt&
85 std::string name,
86 Labels labels,
87 std::optional<std::string> description = std::nullopt
88 ) = 0;
89
98 virtual CounterDouble&
100 std::string name,
101 Labels labels,
102 std::optional<std::string> description = std::nullopt
103 ) = 0;
104
113 virtual GaugeInt&
115 std::string name,
116 Labels labels,
117 std::optional<std::string> description = std::nullopt
118 ) = 0;
119
128 virtual GaugeDouble&
130 std::string name,
131 Labels labels,
132 std::optional<std::string> description = std::nullopt
133 ) = 0;
134
144 virtual HistogramInt&
146 std::string name,
147 Labels labels,
148 std::vector<std::int64_t> const& buckets,
149 std::optional<std::string> description = std::nullopt
150 ) = 0;
151
161 virtual HistogramDouble&
163 std::string name,
164 Labels labels,
165 std::vector<double> const& buckets,
166 std::optional<std::string> description = std::nullopt
167 ) = 0;
168
174 virtual std::string
176
182 bool
183 isEnabled() const
184 {
185 return isEnabled_;
186 }
187
193 bool
195 {
196 return compressReplyEnabled_;
197 }
198
199private:
200 bool isEnabled_;
201 bool compressReplyEnabled_;
202};
203
210public:
212
213 Bool
215 std::string name,
216 Labels labels,
217 std::optional<std::string> description = std::nullopt
218 ) override;
219
220 CounterInt&
221 counterInt(std::string name, Labels labels, std::optional<std::string> description) override;
222
223 CounterDouble&
224 counterDouble(std::string name, Labels labels, std::optional<std::string> description) override;
225
226 GaugeInt&
227 gaugeInt(std::string name, Labels labels, std::optional<std::string> description) override;
228
229 GaugeDouble&
230 gaugeDouble(std::string name, Labels labels, std::optional<std::string> description) override;
231
232 HistogramInt&
234 std::string name,
235 Labels labels,
236 std::vector<std::int64_t> const& buckets,
237 std::optional<std::string> description = std::nullopt
238 ) override;
239
240 HistogramDouble&
242 std::string name,
243 Labels labels,
244 std::vector<double> const& buckets,
245 std::optional<std::string> description = std::nullopt
246 ) override;
247
248 std::string
249 collectMetrics() override;
250
251private:
253 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
254
256 getMetric(
257 std::string name,
258 Labels labels,
259 std::optional<std::string> description,
260 MetricType type
261 );
262
263 template <typename ValueType>
264 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
266 getMetric(
267 std::string name,
268 Labels labels,
269 std::optional<std::string> description,
270 MetricType type,
271 std::vector<ValueType> const& buckets
272 );
273
274 std::unordered_map<std::string, MetricsFamily> metrics_;
275};
276
277} // namespace util::prometheus
278
283public:
289 static void
291
297 static bool
299
309 static util::prometheus::Bool
311 std::string name,
313 std::optional<std::string> description = std::nullopt
314 );
315
324 static util::prometheus::CounterInt&
326 std::string name,
328 std::optional<std::string> description = std::nullopt
329 );
330
339 static util::prometheus::CounterDouble&
341 std::string name,
343 std::optional<std::string> description = std::nullopt
344 );
345
354 static util::prometheus::GaugeInt&
355 gaugeInt(
356 std::string name,
358 std::optional<std::string> description = std::nullopt
359 );
360
369 static util::prometheus::GaugeDouble&
371 std::string name,
373 std::optional<std::string> description = std::nullopt
374 );
375
385 static util::prometheus::HistogramInt&
387 std::string name,
389 std::vector<std::int64_t> const& buckets,
390 std::optional<std::string> description = std::nullopt
391 );
392
402 static util::prometheus::HistogramDouble&
404 std::string name,
406 std::vector<double> const& buckets,
407 std::optional<std::string> description = std::nullopt
408 );
409
415 static std::string
417
423 static bool
424 isEnabled();
425
431 static bool
433
441 static void
442 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
443
450 instance();
451
452private:
453 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
454};
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.