Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Counter.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/Assert.hpp"
23#include "util/prometheus/MetricBase.hpp"
24#include "util/prometheus/OStream.hpp"
25#include "util/prometheus/impl/AnyCounterBase.hpp"
26
27#include <cstdint>
28#include <string>
29#include <type_traits>
30#include <utility>
31
32namespace util::prometheus {
33
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 AnyCounter(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
79 operator+=(ValueType const value)
80 {
81 ASSERT(value >= 0, "Cannot decrease a counter");
82 this->pimpl_->add(value);
83 return *this;
84 }
85
91 void
92 set(ValueType value)
93 {
94 ASSERT(value >= this->value(), "Cannot decrease a counter {}", this->name());
95 this->pimpl_->set(value);
96 }
97
101 void
103 {
104 this->pimpl_->set(ValueType{0});
105 }
106
112 ValueType
113 value() const
114 {
115 return this->pimpl_->value();
116 }
117
123 void
124 serializeValue(OStream& stream) const override
125 {
126 stream << name() << labelsString() << ' ' << value();
127 }
128};
129
133using CounterInt = AnyCounter<std::uint64_t>;
134
138using CounterDouble = AnyCounter<double>;
139
140} // 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 counter metric implementation. It can only be increased or be reset to zero.
Definition Counter.hpp:40
void reset()
Reset the counter to zero.
Definition Counter.hpp:102
ValueType value() const
Get the value of the counter.
Definition Counter.hpp:113
void set(ValueType value)
Set the value of the counter.
Definition Counter.hpp:92
AnyCounter & operator++()
Increase the counter by one.
Definition Counter.hpp:65
void serializeValue(OStream &stream) const override
Serialize the counter to a string in prometheus format (i.e. name{labels} value)
Definition Counter.hpp:124
AnyCounter(std::string name, std::string labelsString, ImplType &&impl=ImplType{})
Construct a new AnyCounter object.
Definition Counter.hpp:53
AnyCounter & operator+=(ValueType const value)
Increase the counter by the given value.
Definition Counter.hpp:79