22#include "util/Assert.hpp"
23#include "util/prometheus/MetricBase.hpp"
24#include "util/prometheus/OStream.hpp"
25#include "util/prometheus/impl/HistogramImpl.hpp"
34namespace util::prometheus {
41template <SomeNumberType NumberType>
44 using ValueType = NumberType;
45 using Buckets = std::vector<NumberType>;
56 template <impl::SomeHistogramImpl ImplType = impl::HistogramImpl<ValueType>>
57 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
60 , pimpl_(std::make_unique<Model<ImplType>>(std::forward<ImplType>(impl)))
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);
75 pimpl_->observe(value);
91 virtual ~Concept() =
default;
93 virtual void observe(NumberType) = 0;
96 setBuckets(Buckets
const& buckets) = 0;
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))
112 observe(NumberType value)
override
114 impl_.observe(value);
118 setBuckets(Buckets
const& buckets)
override
120 impl_.setBuckets(buckets);
124 serializeValue(std::string
const&
name, std::string
const&
labelsString, OStream& stream)
const override
133 std::unique_ptr<Concept> pimpl_;
136using HistogramInt = AnyHistogram<std::int64_t>;
137using HistogramDouble = AnyHistogram<double>;
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