Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Gauge.hpp
1#pragma once
2
3#include "util/prometheus/MetricBase.hpp"
4#include "util/prometheus/OStream.hpp"
5#include "util/prometheus/impl/AnyCounterBase.hpp"
6
7#include <cstdint>
8#include <string>
9#include <type_traits>
10#include <utility>
11
12namespace util::prometheus {
13
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 AnyGauge(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
59 {
60 this->pimpl_->add(ValueType{-1});
61 return *this;
62 }
63
71 operator+=(ValueType const value)
72 {
73 this->pimpl_->add(value);
74 return *this;
75 }
76
84 operator-=(ValueType const value)
85 {
86 this->pimpl_->add(-value);
87 return *this;
88 }
89
95 void
96 set(ValueType const value)
97 {
98 this->pimpl_->set(value);
99 }
100
106 [[nodiscard]] ValueType
107 value() const
108 {
109 return this->pimpl_->value();
110 }
111
117 void
118 serializeValue(OStream& stream) const override
119 {
120 stream << name() << labelsString() << ' ' << value();
121 }
122};
123
127using GaugeInt = AnyGauge<std::int64_t>;
128
132using GaugeDouble = AnyGauge<double>;
133
134} // 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 gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:21
void set(ValueType const value)
Set the gauge to the given value.
Definition Gauge.hpp:96
AnyGauge & operator--()
Decrease the gauge by one.
Definition Gauge.hpp:58
AnyGauge & operator++()
Increase the gauge by one.
Definition Gauge.hpp:46
AnyGauge & operator-=(ValueType const value)
Decrease the gauge by the given value.
Definition Gauge.hpp:84
AnyGauge & operator+=(ValueType const value)
Increase the gauge by the given value.
Definition Gauge.hpp:71
void serializeValue(OStream &stream) const override
Serialize the counter to a string in prometheus format (i.e. name{labels} value).
Definition Gauge.hpp:118
ValueType value() const
Definition Gauge.hpp:107
AnyGauge(std::string name, std::string labelsString, ImplType &&impl=ImplType{})
Construct a new AnyGauge object.
Definition Gauge.hpp:34