userver: userver/utils/statistics/metric_value.hpp Source File
Loading...
Searching...
No Matches
metric_value.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/metric_value.hpp
4/// @brief @copybrief utils::statistics::MetricValue
5
6#include <cstdint>
7#include <type_traits>
8#include <variant>
9
10#include <userver/utils/statistics/histogram_view.hpp>
11#include <userver/utils/statistics/rate.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils::statistics {
16
17/// @brief The value of a metric.
18///
19/// Cheap to copy, expected to be passed around by value.
20class MetricValue final {
21public:
22 using RawType = std::variant<std::int64_t, double, Rate, HistogramView>;
23
24 /// Creates an unspecified metric value.
25 constexpr MetricValue() noexcept : value_(std::int64_t{0}) {}
26
27 /// Constructs MetricValue for tests.
28 /// @{
29 constexpr /*implicit*/ MetricValue(std::int64_t value) noexcept : value_(value) {}
30
31 constexpr /*implicit*/ MetricValue(double value) noexcept : value_(value) {}
32
33 constexpr /*implicit*/ MetricValue(Rate value) noexcept : value_(value) {}
34
35 constexpr /*implicit*/ MetricValue(HistogramView value) noexcept : value_(value) {}
36 /// @}
37
38 // trivially copyable
39 MetricValue(const MetricValue&) = default;
40 MetricValue& operator=(const MetricValue&) = default;
41
42 friend bool operator==(const MetricValue& lhs, const MetricValue& rhs) noexcept { return lhs.value_ == rhs.value_; }
43
44 friend bool operator!=(const MetricValue& lhs, const MetricValue& rhs) noexcept { return lhs.value_ != rhs.value_; }
45
46 /// @brief Retrieve the value of an integer metric.
47 /// @throws std::exception on type mismatch.
48 std::int64_t AsInt() const { return std::get<std::int64_t>(value_); }
49
50 /// @brief Retrieve the value of a floating-point metric.
51 /// @throws std::exception on type mismatch.
52 double AsFloat() const { return std::get<double>(value_); }
53
54 /// @brief Retrieve the value of a Rate metric.
55 /// @throws std::exception on type mismatch.
56 Rate AsRate() const { return std::get<Rate>(value_); }
57
58 /// @brief Returns whether metric is Rate metric.
59 bool IsRate() const noexcept { return std::holds_alternative<Rate>(value_); }
60
61 /// @brief Retrieve the value of a HistogramView metric.
62 /// @throws std::exception on type mismatch.
63 HistogramView AsHistogram() const { return std::get<HistogramView>(value_); }
64
65 /// @brief Returns whether metric is HistogramView metric.
66 bool IsHistogram() const noexcept { return std::holds_alternative<HistogramView>(value_); }
67
68 /// @brief Calls @p visitor with either a `std::int64_t` or a `double` value.
69 /// @returns Whatever @p visitor returns.
70 template <typename VisitorFunc>
71 decltype(auto) Visit(VisitorFunc visitor) const {
72 return std::visit(visitor, value_);
73 }
74
75 /// @cond
76 explicit MetricValue(RawType value) noexcept : value_(value) {}
77 /// @endcond
78
79private:
80 RawType value_;
81};
82
83} // namespace utils::statistics
84
85USERVER_NAMESPACE_END