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
39template <SomeNumberType NumberType>
41 using ValueType = NumberType;
42
51 template <impl::SomeCounterImpl ImplType = impl::CounterImpl<ValueType>>
52 requires std::same_as<ValueType, typename std::remove_cvref_t<ImplType>::ValueType>
53 AnyGauge(std::string name, std::string labelsString, ImplType&& impl = ImplType{})
54 : MetricBase(std::move(name), std::move(labelsString))
55 , impl::AnyCounterBase<ValueType>(std::forward<ImplType>(impl))
56 {
57 }
58
66 {
67 this->pimpl_->add(ValueType{1});
68 return *this;
69 }
70
78 {
79 this->pimpl_->add(ValueType{-1});
80 return *this;
81 }
82
90 operator+=(ValueType const value)
91 {
92 this->pimpl_->add(value);
93 return *this;
94 }
95
102 AnyGauge&
103 operator-=(ValueType const value)
104 {
105 this->pimpl_->add(-value);
106 return *this;
107 }
108
114 void
115 set(ValueType const value)
116 {
117 this->pimpl_->set(value);
118 }
119
125 ValueType
126 value() const
127 {
128 return this->pimpl_->value();
129 }
130
136 void
137 serializeValue(OStream& stream) const override
138 {
139 stream << name() << labelsString() << ' ' << value();
140 }
141};
142
146using GaugeInt = AnyGauge<std::int64_t>;
147
151using GaugeDouble = AnyGauge<double>;
152
153} // namespace util::prometheus
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
Definition AnyCounterBase.hpp:32
A prometheus gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:40
void set(ValueType const value)
Set the gauge to the given value.
Definition Gauge.hpp:115
AnyGauge & operator--()
Decrease the gauge by one.
Definition Gauge.hpp:77
AnyGauge & operator++()
Increase the gauge by one.
Definition Gauge.hpp:65
AnyGauge & operator-=(ValueType const value)
Decrease the gauge by the given value.
Definition Gauge.hpp:103
AnyGauge & operator+=(ValueType const value)
Increase the gauge by the given value.
Definition Gauge.hpp:90
void serializeValue(OStream &stream) const override
Serialize the counter to a string in prometheus format (i.e. name{labels} value).
Definition Gauge.hpp:137
ValueType value() const
Definition Gauge.hpp:126
AnyGauge(std::string name, std::string labelsString, ImplType &&impl=ImplType{})
Construct a new AnyGauge object.
Definition Gauge.hpp:53