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
6#include <memory>
7
8#include <userver/utils/span.hpp>
9#include <userver/utils/statistics/fwd.hpp>
10#include <userver/utils/statistics/histogram_view.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::statistics {
15
16/// @brief Used to aggregate multiple utils::statistics::Histogram metrics.
17///
18/// Usage example:
19/// @snippet utils/statistics/histogram_test.cpp HistogramAggregator
20class HistogramAggregator final {
21public:
22 /// @brief Sets upper bounds for each non-"infinite" bucket. The lowest bound is
23 /// always 0.
24 ///
25 /// @param upper_bounds is copied inside and is not required to be kept alive after the constructor completes.
26 explicit HistogramAggregator(utils::span<const double> upper_bounds);
27
28 HistogramAggregator(HistogramAggregator&&) noexcept;
29 HistogramAggregator& operator=(HistogramAggregator&&) noexcept;
30 ~HistogramAggregator();
31
32 /// @brief Add the other histogram to the current one.
33 ///
34 /// Bucket borders in `this` and `other` must be either identical, or bucket
35 /// borders in `this` must be a strict subset of bucket borders in `other`.
36 ///
37 /// Writes to `*this` are non-atomic.
38 void Add(HistogramView other);
39
40 /// Non-atomically increment the bucket corresponding to the given index.
41 void AccountAt(std::size_t bucket_index, std::uint64_t count = 1) noexcept;
42
43 /// Non-atomically increment the "infinity" bucket.
44 void AccountInf(std::uint64_t count = 1) noexcept;
45
46 /// Reset all buckets to zero.
47 void Reset() noexcept;
48
49 /// Allows reading the histogram.
50 HistogramView GetView() const& noexcept;
51
52 /// @cond
53 // Store Histogram in a variable before taking a view on it.
54 HistogramView GetView() && noexcept = delete;
55 /// @endcond
56
57private:
58 std::unique_ptr<impl::histogram::Bucket[]> buckets_;
59};
60
61/// Metric serialization support for HistogramAggregator.
62void DumpMetric(Writer& writer, const HistogramAggregator& histogram);
63
64} // namespace utils::statistics
65
66USERVER_NAMESPACE_END