Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Histogram.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4#include "util/prometheus/MetricBase.hpp"
5#include "util/prometheus/OStream.hpp"
6#include "util/prometheus/impl/HistogramImpl.hpp"
7
8#include <cstdint>
9#include <memory>
10#include <string>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15namespace util::prometheus {
16
22template <SomeNumberType NumberType>
23class AnyHistogram : public MetricBase {
24public:
25 using ValueType = NumberType;
26 using Buckets = std::vector<NumberType>;
27
39 template <impl::SomeHistogramImpl ImplType = impl::HistogramImpl<ValueType>>
40 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
42 std::string name,
43 std::string labelsString,
44 Buckets const& buckets,
45 ImplType&& impl = ImplType{}
46 )
47 : MetricBase(std::move(name), std::move(labelsString))
48 , pimpl_(std::make_unique<Model<ImplType>>(std::forward<ImplType>(impl)))
49 {
50 ASSERT(!buckets.empty(), "Histogram must have at least one bucket.");
51 ASSERT(
52 std::is_sorted(buckets.begin(), buckets.end()), "Buckets for histogra must be sorted."
53 );
54 pimpl_->setBuckets(buckets);
55 }
56
62 void
63 observe(ValueType const value)
64 {
65 pimpl_->observe(value);
66 }
67
73 void
74 serializeValue(OStream& stream) const override
75 {
76 pimpl_->serializeValue(name(), labelsString(), stream);
77 }
78
79private:
80 struct Concept {
81 virtual ~Concept() = default;
82
83 virtual void observe(NumberType) = 0;
84
85 virtual void
86 setBuckets(Buckets const& buckets) = 0;
87
88 virtual void
89 serializeValue(
90 std::string const& name,
91 std::string const& labelsString,
92 OStream&
93 ) const = 0;
94 };
95
96 template <impl::SomeHistogramImpl ImplType>
97 requires std::same_as<NumberType, typename std::remove_cvref_t<ImplType>::ValueType>
98 struct Model : Concept {
99 template <typename SomeImplType>
100 requires std::same_as<SomeImplType, ImplType>
101 Model(SomeImplType&& impl) : impl_(std::forward<SomeImplType>(impl))
102 {
103 }
104
105 void
106 observe(NumberType value) override
107 {
108 impl_.observe(value);
109 }
110
111 void
112 setBuckets(Buckets const& buckets) override
113 {
114 impl_.setBuckets(buckets);
115 }
116
117 void
118 serializeValue(
119 std::string const& name,
120 std::string const& labelsString,
121 OStream& stream
122 ) const override
123 {
124 impl_.serializeValue(name, labelsString, stream);
125 }
126
127 private:
128 ImplType impl_;
129 };
130
131 std::unique_ptr<Concept> pimpl_;
132};
133
134using HistogramInt = AnyHistogram<std::int64_t>;
135using HistogramDouble = AnyHistogram<double>;
136
137} // namespace util::prometheus
A Prometheus histogram metric with a generic value type.
Definition Histogram.hpp:23
AnyHistogram(std::string name, std::string labelsString, Buckets const &buckets, ImplType &&impl=ImplType{})
Construct a new Histogram object.
Definition Histogram.hpp:41
void observe(ValueType const value)
Add a value to the histogram.
Definition Histogram.hpp:63
void serializeValue(OStream &stream) const override
Serialize the metric to a string in Prometheus format.
Definition Histogram.hpp:74
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:12
MetricBase(std::string name, std::string labelsString)
Construct a new MetricBase object.
Definition MetricBase.cpp:11
std::string const & name() const
Get the name of the metric.
Definition MetricBase.cpp:48
std::string const & labelsString() const
Get the labels of the metric in serialized format, e.g. {name="value",name2="value2"}...
Definition MetricBase.cpp:54
A stream that can optionally compress its data.
Definition OStream.hpp:12