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>;
58 template <impl::SomeHistogramImpl ImplType = impl::HistogramImpl<ValueType>>
59 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
63 Buckets
const& buckets,
64 ImplType&& impl = ImplType{}
67 , pimpl_(std::make_unique<Model<ImplType>>(std::forward<ImplType>(impl)))
69 ASSERT(!buckets.empty(),
"Histogram must have at least one bucket.");
71 std::is_sorted(buckets.begin(), buckets.end()),
"Buckets for histogra must be sorted."
73 pimpl_->setBuckets(buckets);
84 pimpl_->observe(value);
100 virtual ~Concept() =
default;
102 virtual void observe(NumberType) = 0;
105 setBuckets(Buckets
const& buckets) = 0;
109 std::string
const&
name,
115 template <impl::SomeHistogramImpl ImplType>
116 requires std::same_as<NumberType, typename std::remove_cvref_t<ImplType>::ValueType>
117 struct Model : Concept {
118 template <
typename SomeImplType>
119 requires std::same_as<SomeImplType, ImplType>
120 Model(SomeImplType&& impl) : impl_(std::forward<SomeImplType>(impl))
125 observe(NumberType value)
override
127 impl_.observe(value);
131 setBuckets(Buckets
const& buckets)
override
133 impl_.setBuckets(buckets);
138 std::string
const&
name,
150 std::unique_ptr<Concept> pimpl_;
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:60
void observe(ValueType const value)
Add a value to the histogram.
Definition Histogram.hpp:82
void serializeValue(OStream &stream) const override
Serialize the metric to a string in Prometheus format.
Definition Histogram.hpp:93
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:31
MetricBase(std::string name, std::string labelsString)
Construct a new MetricBase object.
Definition MetricBase.cpp:30
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