userver: userver/utils/statistics/rate.hpp Source File
Loading...
Searching...
No Matches
rate.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/rate.hpp
4/// @brief @copybrief utils::statistics::Rate
5
6#include <cstdint>
7
8#include <fmt/format.h>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils::statistics {
13
14/// @ingroup userver_universal
15///
16/// @brief `Rate` metrics (or "counter" metrics) are metrics that only monotonically
17/// increase or are reset to zero on restart. Some monitoring systems give them
18/// special treatment with regard to maintaining proper non-negative derivative.
19struct Rate {
20 using ValueType = std::uint64_t;
21
22 ValueType value{0};
23
24 inline Rate& operator+=(Rate other) noexcept {
25 value += other.value;
26 return *this;
27 }
28
29 explicit operator bool() const noexcept { return value != 0; }
30
31 Rate operator-(Rate rhs) const noexcept { return {value - rhs.value}; }
32
33 bool operator==(Rate rhs) const noexcept { return value == rhs.value; }
34
35 bool operator!=(Rate rhs) const noexcept { return !(*this == rhs); }
36
37 bool operator<(Rate rhs) const noexcept { return value < rhs.value; }
38
39 bool operator>(Rate rhs) const noexcept { return rhs < *this; }
40
41 bool operator<=(Rate rhs) const noexcept { return !(rhs < *this); }
42
43 bool operator>=(Rate rhs) const noexcept { return !(*this < rhs); }
44
45 bool operator==(std::uint64_t rhs) const noexcept { return value == rhs; }
46
47 bool operator!=(std::uint64_t rhs) const noexcept { return value != rhs; }
48
49 bool operator<(std::uint64_t rhs) const noexcept { return value < rhs; }
50
51 bool operator>(std::uint64_t rhs) const noexcept { return value > rhs; }
52
53 bool operator<=(std::uint64_t rhs) const noexcept { return value <= rhs; }
54
55 bool operator>=(std::uint64_t rhs) const noexcept { return value >= rhs; }
56};
57
58inline Rate operator+(Rate first, Rate second) noexcept { return Rate{first.value + second.value}; }
59
60} // namespace utils::statistics
61
62USERVER_NAMESPACE_END
63
64template <>
65class fmt::formatter<USERVER_NAMESPACE::utils::statistics::Rate> {
66public:
67 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
68
69 template <typename FormatCtx>
70 auto format(const USERVER_NAMESPACE::utils::statistics::Rate& rate, FormatCtx& ctx) const {
71 return fmt::format_to(ctx.out(), "{}", rate.value);
72 }
73};