userver: userver/utils/statistics/histogram_view.hpp Source File
Loading...
Searching...
No Matches
histogram_view.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/histogram_view.hpp
4/// @brief @copybrief utils::statistics::HistogramView
5
6#include <cstddef>
7#include <cstdint>
8#include <type_traits>
9
10#include <fmt/format.h>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::statistics {
15
16class Writer;
17
18namespace impl::histogram {
19struct Bucket;
20struct Access;
21} // namespace impl::histogram
22
23/// @ingroup userver_universal
24///
25/// @brief The non-owning reader API for "histogram" metrics.
26///
27/// @see utils::statistics::Histogram for details on semantics
28///
29/// HistogramView is cheap to copy, expected to be passed around by value.
30class HistogramView final {
31public:
32 // trivially copyable
33 constexpr HistogramView(const HistogramView&) noexcept = default;
34 constexpr HistogramView& operator=(const HistogramView&) noexcept = default;
35
36 /// Returns the number of "normal" (non-"infinity") buckets.
37 std::size_t GetBucketCount() const noexcept;
38
39 /// Returns the upper bucket boundary for the given bucket.
40 double GetUpperBoundAt(std::size_t index) const;
41
42 /// Returns the occurrence count for the given bucket.
43 std::uint64_t GetValueAt(std::size_t index) const;
44
45 /// Returns the occurrence count for the "infinity" bucket
46 /// (greater than the largest bucket boundary).
47 std::uint64_t GetValueAtInf() const noexcept;
48
49 /// Returns the sum of counts from all buckets.
50 std::uint64_t GetTotalCount() const noexcept;
51
52 /// Returns sum of values from the given bucket.
53 double GetSumAt(std::size_t index) const;
54
55 // Returns sum of values from the "infinity" bucket
56 /// (greater than the largest bucket boundary).
57 double GetSumAtInf() const noexcept;
58
59 /// Returns sum of values from all buckets.
60 double GetTotalSum() const noexcept;
61
62private:
63 friend struct impl::histogram::Access;
64
65 constexpr explicit HistogramView(const impl::histogram::Bucket& buckets) noexcept : buckets_(&buckets) {}
66
67 const impl::histogram::Bucket* buckets_;
68};
69
70/// Compares equal if bounds are close and values are equal.
71bool operator==(HistogramView lhs, HistogramView rhs) noexcept;
72
73/// @overload
74bool operator!=(HistogramView lhs, HistogramView rhs) noexcept;
75
76} // namespace utils::statistics
77
78USERVER_NAMESPACE_END
79
80template <>
81struct fmt::formatter<USERVER_NAMESPACE::utils::statistics::HistogramView> {
82 constexpr static auto parse(format_parse_context& ctx) { return ctx.begin(); }
83
84 template <typename FormatCtx>
85 auto format(USERVER_NAMESPACE::utils::statistics::HistogramView histogram, FormatCtx& ctx) const {
86 const auto bucket_count = histogram.GetBucketCount();
87 for (std::size_t i = 0; i < bucket_count; ++i) {
88 ctx.advance_to(fmt::format_to(ctx.out(), "[{}]={}", histogram.GetUpperBoundAt(i), histogram.GetValueAt(i)));
89 ctx.advance_to(fmt::format_to(ctx.out(), ","));
90 }
91 ctx.advance_to(fmt::format_to(ctx.out(), "[inf]={}", histogram.GetValueAtInf()));
92 return ctx.out();
93 }
94};