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