Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Counter.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/AnyCounterBase.hpp"
7
8#include <cstdint>
9#include <string>
10#include <type_traits>
11#include <utility>
12
13namespace util::prometheus {
14
20template <SomeNumberType NumberType>
22 using ValueType = NumberType;
23
32 template <impl::SomeCounterImpl ImplType = impl::CounterImpl<ValueType>>
33 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
34 AnyCounter(std::string name, std::string labelsString, ImplType&& impl = ImplType{})
35 : MetricBase(std::move(name), std::move(labelsString))
36 , impl::AnyCounterBase<ValueType>(std::forward<ImplType>(impl))
37 {
38 }
39
47 {
48 this->pimpl_->add(ValueType{1});
49 return *this;
50 }
51
60 operator+=(ValueType const value)
61 {
62 ASSERT(value >= 0, "Cannot decrease a counter");
63 this->pimpl_->add(value);
64 return *this;
65 }
66
72 void
73 set(ValueType value)
74 {
75 ASSERT(value >= this->value(), "Cannot decrease a counter {}", this->name());
76 this->pimpl_->set(value);
77 }
78
82 void
84 {
85 this->pimpl_->set(ValueType{0});
86 }
87
93 ValueType
94 value() const
95 {
96 return this->pimpl_->value();
97 }
98
104 void
105 serializeValue(OStream& stream) const override
106 {
107 stream << name() << labelsString() << ' ' << value();
108 }
109};
110
114using CounterInt = AnyCounter<std::uint64_t>;
115
119using CounterDouble = AnyCounter<double>;
120
121} // namespace util::prometheus
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
Definition AnyCounterBase.hpp:13
A prometheus counter metric implementation. It can only be increased or be reset to zero.
Definition Counter.hpp:21
void reset()
Reset the counter to zero.
Definition Counter.hpp:83
ValueType value() const
Definition Counter.hpp:94
void set(ValueType value)
Set the value of the counter.
Definition Counter.hpp:73
AnyCounter & operator++()
Increase the counter by one.
Definition Counter.hpp:46
void serializeValue(OStream &stream) const override
Serialize the counter to a string in prometheus format (i.e. name{labels} value).
Definition Counter.hpp:105
AnyCounter(std::string name, std::string labelsString, ImplType &&impl=ImplType{})
Construct a new AnyCounter object.
Definition Counter.hpp:34
AnyCounter & operator+=(ValueType const value)
Increase the counter by the given value.
Definition Counter.hpp:60