userver: userver/utils/statistics/histogram_aggregator.hpp Source File
Loading...
Searching...
No Matches
histogram_aggregator.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/histogram_aggregator.hpp
4/// @brief @copybrief utils::statistics::HistogramAggregator
5/// @ingroup userver_universal
6
7#include <memory>
8
9#include <userver/utils/impl/internal_tag.hpp>
10#include <userver/utils/span.hpp>
11#include <userver/utils/statistics/fwd.hpp>
12#include <userver/utils/statistics/histogram_view.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace utils::statistics {
17
18/// @brief Used to aggregate multiple @ref utils::statistics::Histogram metrics.
19///
20/// Usage example:
21/// @snippet universal/src/utils/statistics/histogram_test.cpp HistogramAggregator
22class HistogramAggregator final {
23public:
24 /// @brief Sets upper bounds for each non-"infinite" bucket. The lowest bound is
25 /// always 0.
26 ///
27 /// @param upper_bounds is copied inside and is not required to be kept alive after the constructor completes.
28 explicit HistogramAggregator(utils::span<const double> upper_bounds);
29
30 /// @brief Empty tag. Selects the constructor that copies bucket borders from a @ref
31 /// utils::statistics::HistogramView.
32 struct CopyBounds {};
33
34 /// @brief Copies bucket borders from @a view. Bucket counts stay zero.
35 ///
36 /// Use @ref utils::statistics::HistogramAggregator::Add to copy or merge values afterwards.
37 HistogramAggregator(CopyBounds, HistogramView view);
38
39 /// @brief Transfers ownership of the bucket buffer.
40 ///
41 /// @ref utils::statistics::HistogramView instances already taken from @a other remain valid and refer to that
42 /// buffer, now owned by this object.
43 HistogramAggregator(HistogramAggregator&&) noexcept;
44
45 /// Views already taken from the left-hand side become invalid: its previous buffer is destroyed.
46 /// @see utils::statistics::HistogramAggregator::HistogramAggregator(HistogramAggregator&&)
47 HistogramAggregator& operator=(HistogramAggregator&&) noexcept;
48 ~HistogramAggregator();
49
50 /// @brief Add the other histogram to the current one.
51 ///
52 /// Bucket borders in `this` and `other` must be either identical, or bucket
53 /// borders in `this` must be a strict subset of bucket borders in `other`.
54 ///
55 /// Writes to `*this` are non-atomic.
56 void Add(HistogramView other);
57
58 /// Non-atomically increment the bucket corresponding to the given index.
59 void AccountAt(std::size_t bucket_index, std::uint64_t count = 1) noexcept;
60
61 /// Non-atomically increment the "infinity" bucket.
62 void AccountInf(std::uint64_t count = 1) noexcept;
63
64 /// @cond
65 /// Add @a sum to the "infinity" bucket sum. The "infinity" count stays unchanged.
66 void AccountInfSum(utils::impl::InternalTag, double sum) noexcept;
67 /// @endcond
68
69 /// Reset all buckets to zero.
70 void Reset() noexcept;
71
72 /// Allows reading the histogram.
73 HistogramView GetView() const& noexcept;
74
75 /// @cond
76 // Store HistogramAggregator in a variable before taking a view on it.
77 HistogramView GetView() && noexcept = delete;
78 /// @endcond
79
80private:
81 std::unique_ptr<impl::histogram::Bucket[]> buckets_;
82};
83
84/// Metric serialization support for @ref utils::statistics::HistogramAggregator.
85void DumpMetric(Writer& writer, const HistogramAggregator& histogram);
86
87} // namespace utils::statistics
88
89USERVER_NAMESPACE_END