Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Gauge.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/prometheus/MetricBase.hpp"
23#include "util/prometheus/OStream.hpp"
24#include "util/prometheus/impl/AnyCounterBase.hpp"
25
26#include <cstdint>
27#include <string>
28#include <type_traits>
29#include <utility>
30
31namespace util::prometheus {
32
38template <SomeNumberType NumberType>
40 using ValueType = NumberType;
41
50 template <impl::SomeCounterImpl ImplType = impl::CounterImpl<ValueType>>
51 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
52 AnyGauge(std::string name, std::string labelsString, ImplType&& impl = ImplType{})
53 : MetricBase(std::move(name), std::move(labelsString))
54 , impl::AnyCounterBase<ValueType>(std::forward<ImplType>(impl))
55 {
56 }
57
65 {
66 this->pimpl_->add(ValueType{1});
67 return *this;
68 }
69
77 {
78 this->pimpl_->add(ValueType{-1});
79 return *this;
80 }
81
89 operator+=(ValueType const value)
90 {
91 this->pimpl_->add(value);
92 return *this;
93 }
94
101 AnyGauge&
102 operator-=(ValueType const value)
103 {
104 this->pimpl_->add(-value);
105 return *this;
106 }
107
113 void
114 set(ValueType const value)
115 {
116 this->pimpl_->set(value);
117 }
118
124 ValueType
125 value() const
126 {
127 return this->pimpl_->value();
128 }
129
135 void
136 serializeValue(OStream& stream) const override
137 {
138 stream << name() << labelsString() << ' ' << value();
139 }
140};
141
145using GaugeInt = AnyGauge<std::int64_t>;
146
150using GaugeDouble = AnyGauge<double>;
151
152} // namespace util::prometheus
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
Definition AnyCounterBase.hpp:32
A prometheus gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:39
void set(ValueType const value)
Set the gauge to the given value.
Definition Gauge.hpp:114
AnyGauge(std::string name, std::string labelsString, ImplType &&impl=ImplType{})
Construct a new AnyGauge object.
Definition Gauge.hpp:52
AnyGauge & operator--()
Decrease the gauge by one.
Definition Gauge.hpp:76
AnyGauge & operator++()
Increase the gauge by one.
Definition Gauge.hpp:64
AnyGauge & operator-=(ValueType const value)
Decrease the gauge by the given value.
Definition Gauge.hpp:102
AnyGauge & operator+=(ValueType const value)
Increase the gauge by the given value.
Definition Gauge.hpp:89
void serializeValue(OStream &stream) const override
Serialize the counter to a string in prometheus format (i.e. name{labels} value)
Definition Gauge.hpp:136
ValueType value() const
Get the value of the counter.
Definition Gauge.hpp:125