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#include <memory>
4
5#include <userver/utils/span.hpp>
6#include <userver/utils/statistics/fwd.hpp>
7#include <userver/utils/statistics/histogram_view.hpp>
8
9/// @file userver/utils/statistics/histogram_aggregator.hpp
10/// @brief @copybrief utils::statistics::HistogramAggregator
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 {
21 public:
22 explicit HistogramAggregator(utils::span<const double> upper_bounds);
23
24 HistogramAggregator(HistogramAggregator&&) noexcept;
25 HistogramAggregator& operator=(HistogramAggregator&&) noexcept;
26 ~HistogramAggregator();
27
28 /// @brief Add the other histogram to the current one.
29 ///
30 /// Bucket borders in `this` and `other` must be either identical, or bucket
31 /// borders in `this` must be a strict subset of bucket borders in `other`.
32 ///
33 /// Writes to `*this` are non-atomic.
34 void Add(HistogramView other);
35
36 /// Non-atomically increment the bucket corresponding to the given index.
37 void AccountAt(std::size_t bucket_index, std::uint64_t count = 1) noexcept;
38
39 /// Non-atomically increment the "infinity" bucket.
40 void AccountInf(std::uint64_t count = 1) noexcept;
41
42 /// Reset all buckets to zero.
43 void Reset() noexcept;
44
45 /// Allows reading the histogram.
46 HistogramView GetView() const& noexcept;
47
48 /// @cond
49 // Store Histogram in a variable before taking a view on it.
50 HistogramView GetView() && noexcept = delete;
51 /// @endcond
52
53 private:
54 std::unique_ptr<impl::histogram::Bucket[]> buckets_;
55};
56
57/// Metric serialization support for HistogramAggregator.
58void DumpMetric(Writer& writer, const HistogramAggregator& histogram);
59
60} // namespace utils::statistics
61
62USERVER_NAMESPACE_END