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
69 boolMetric(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) = 0;
70
79 virtual CounterInt&
80 counterInt(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) = 0;
81
90 virtual CounterDouble&
91 counterDouble(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) = 0;
92
101 virtual GaugeInt&
102 gaugeInt(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) = 0;
103
112 virtual GaugeDouble&
113 gaugeDouble(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) = 0;
114
124 virtual HistogramInt&
126 std::string name,
127 Labels labels,
128 std::vector<std::int64_t> const& buckets,
129 std::optional<std::string> description = std::nullopt
130 ) = 0;
131
141 virtual HistogramDouble&
143 std::string name,
144 Labels labels,
145 std::vector<double> const& buckets,
146 std::optional<std::string> description = std::nullopt
147 ) = 0;
148
154 virtual std::string
156
162 bool
163 isEnabled() const
164 {
165 return isEnabled_;
166 }
167
173 bool
175 {
176 return compressReplyEnabled_;
177 }
178
179private:
180 bool isEnabled_;
181 bool compressReplyEnabled_;
182};
183
190public:
192
193 Bool
194 boolMetric(std::string name, Labels labels, std::optional<std::string> description = std::nullopt) override;
195
197 counterInt(std::string name, Labels labels, std::optional<std::string> description) override;
198
200 counterDouble(std::string name, Labels labels, std::optional<std::string> description) override;
201
202 GaugeInt&
203 gaugeInt(std::string name, Labels labels, std::optional<std::string> description) override;
204
206 gaugeDouble(std::string name, Labels labels, std::optional<std::string> description) override;
207
210 std::string name,
211 Labels labels,
212 std::vector<std::int64_t> const& buckets,
213 std::optional<std::string> description = std::nullopt
214 ) override;
215
218 std::string name,
219 Labels labels,
220 std::vector<double> const& buckets,
221 std::optional<std::string> description = std::nullopt
222 ) override;
223
224 std::string
225 collectMetrics() override;
226
227private:
229 getMetricsFamily(std::string name, std::optional<std::string> description, MetricType type);
230
232 getMetric(std::string name, Labels labels, std::optional<std::string> description, MetricType type);
233
234 template <typename ValueType>
235 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
237 getMetric(
238 std::string name,
239 Labels labels,
240 std::optional<std::string> description,
241 MetricType type,
242 std::vector<ValueType> const& buckets
243 );
244
245 std::unordered_map<std::string, MetricsFamily> metrics_;
246};
247
248} // namespace util::prometheus
249
254public:
260 void static init(util::config::ClioConfigDefinition const& config);
261
273 std::string name,
275 std::optional<std::string> description = std::nullopt
276 );
277
288 std::string name,
290 std::optional<std::string> description = std::nullopt
291 );
292
303 std::string name,
305 std::optional<std::string> description = std::nullopt
306 );
307
317 gaugeInt(std::string name, util::prometheus::Labels labels, std::optional<std::string> description = std::nullopt);
318
329 std::string name,
331 std::optional<std::string> description = std::nullopt
332 );
333
345 std::string name,
347 std::vector<std::int64_t> const& buckets,
348 std::optional<std::string> description = std::nullopt
349 );
350
362 std::string name,
364 std::vector<double> const& buckets,
365 std::optional<std::string> description = std::nullopt
366 );
367
373 static std::string
375
381 static bool
382 isEnabled();
383
389 static bool
391
399 static void
400 replaceInstance(std::unique_ptr<util::prometheus::PrometheusInterface> inst);
401
408 instance();
409
410private:
411 static std::unique_ptr<util::prometheus::PrometheusInterface> impl;
412};
Singleton class to access the PrometheusInterface.
Definition Prometheus.hpp:253
static bool compressReplyEnabled()
Whether to compress the reply.
Definition Prometheus.cpp:260
static void replaceInstance(std::unique_ptr< util::prometheus::PrometheusInterface > inst)
Replace the prometheus object stored in the singleton.
Definition Prometheus.cpp:266
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:194
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:237
static util::prometheus::PrometheusInterface & instance()
Get the prometheus object stored in the singleton.
Definition Prometheus.cpp:272
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:200
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:188
static void init(util::config::ClioConfigDefinition const &config)
Initialize the singleton with the given configuration.
Definition Prometheus.cpp:179
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:216
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:226
static std::string collectMetrics()
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:248
static bool isEnabled()
Whether prometheus is enabled.
Definition Prometheus.cpp:254
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:210
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
A wrapped to provide bool interface for a Prometheus metric.
Definition Bool.hpp:41
A Prometheus histogram metric with a generic value type.
Definition Histogram.hpp:42
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
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
Implemetation of PrometheusInterface.
Definition Prometheus.hpp:189
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:97
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:81
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:89
std::string collectMetrics() override
Collect all metrics and return them as a string in Prometheus format.
Definition Prometheus.cpp:123
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:110
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:73
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:163
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:174
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.
A prometheus counter metric implementation. It can only be increased or be reset to zero.
Definition Counter.hpp:40
A prometheus gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:39