3#include "util/Assert.hpp"
4#include "util/prometheus/MetricBase.hpp"
5#include "util/prometheus/OStream.hpp"
6#include "util/prometheus/impl/HistogramImpl.hpp"
15namespace util::prometheus {
22template <SomeNumberType NumberType>
25 using ValueType = NumberType;
26 using Buckets = std::vector<NumberType>;
39 template <impl::SomeHistogramImpl ImplType = impl::HistogramImpl<ValueType>>
40 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
44 Buckets
const& buckets,
45 ImplType&& impl = ImplType{}
48 , pimpl_(std::make_unique<Model<ImplType>>(std::forward<ImplType>(impl)))
50 ASSERT(!buckets.empty(),
"Histogram must have at least one bucket.");
52 std::is_sorted(buckets.begin(), buckets.end()),
"Buckets for histogra must be sorted."
54 pimpl_->setBuckets(buckets);
65 pimpl_->observe(value);
81 virtual ~Concept() =
default;
83 virtual void observe(NumberType) = 0;
86 setBuckets(Buckets
const& buckets) = 0;
90 std::string
const&
name,
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))
106 observe(NumberType value)
override
108 impl_.observe(value);
112 setBuckets(Buckets
const& buckets)
override
114 impl_.setBuckets(buckets);
119 std::string
const&
name,
131 std::unique_ptr<Concept> pimpl_;
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