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 <userver/formats/serialize/to.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils::statistics {
13
14/// `Rate` metrics (or "counter" metrics) are metrics that only monotonically
15/// increase or are reset to zero on restart. Some monitoring systems give them
16/// special treatment with regard to maintaining proper non-negative derivative.
17struct Rate {
18 using ValueType = std::uint64_t;
19
20 ValueType value{0};
21
22 inline Rate& operator+=(Rate other) noexcept {
23 value += other.value;
24 return *this;
25 }
26
27 explicit operator bool() const noexcept { return value != 0; }
28
29 bool operator==(const Rate& rhs) const noexcept { return value == rhs.value; }
30
31 bool operator!=(const Rate& rhs) const noexcept { return !(*this == rhs); }
32
33 bool operator<(const Rate& rhs) const noexcept { return value < rhs.value; }
34
35 bool operator>(const Rate& rhs) const noexcept { return rhs < *this; }
36
37 bool operator<=(const Rate& rhs) const noexcept { return !(rhs < *this); }
38
39 bool operator>=(const Rate& rhs) const noexcept { return !(*this < rhs); }
40};
41
42inline Rate operator+(Rate first, Rate second) noexcept {
43 return Rate{first.value + second.value};
44}
45
46template <typename ValueType>
47ValueType Serialize(const Rate& rate,
48 USERVER_NAMESPACE::formats::serialize::To<ValueType>) {
49 using ValueBuilder = typename ValueType::Builder;
50 return ValueBuilder{rate.value}.ExtractValue();
51}
52
53} // namespace utils::statistics
54
55USERVER_NAMESPACE_END