Skip to content

Commit 4363c8d

Browse files
committed
feat statistics: add HistogramAggregator
It can: - add together multiple histograms faster than Histogram would do it - drop some buckets, e.g. to write approximate by-handler metrics and precise aggregate metrics
1 parent 099bcef commit 4363c8d

File tree

14 files changed

+153
-35
lines changed

14 files changed

+153
-35
lines changed

.mapping.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@
562562
"core/include/userver/utils/statistics/fwd.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/fwd.hpp",
563563
"core/include/userver/utils/statistics/graphite.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/graphite.hpp",
564564
"core/include/userver/utils/statistics/histogram.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/histogram.hpp",
565+
"core/include/userver/utils/statistics/histogram_aggregator.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/histogram_aggregator.hpp",
565566
"core/include/userver/utils/statistics/histogram_view.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/histogram_view.hpp",
566567
"core/include/userver/utils/statistics/impl/histogram_bucket.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/impl/histogram_bucket.hpp",
567568
"core/include/userver/utils/statistics/json.hpp":"taxi/uservices/userver/core/include/userver/utils/statistics/json.hpp",
@@ -1298,6 +1299,7 @@
12981299
"core/src/utils/statistics/entry_impl.hpp":"taxi/uservices/userver/core/src/utils/statistics/entry_impl.hpp",
12991300
"core/src/utils/statistics/graphite.cpp":"taxi/uservices/userver/core/src/utils/statistics/graphite.cpp",
13001301
"core/src/utils/statistics/histogram.cpp":"taxi/uservices/userver/core/src/utils/statistics/histogram.cpp",
1302+
"core/src/utils/statistics/histogram_aggregator.cpp":"taxi/uservices/userver/core/src/utils/statistics/histogram_aggregator.cpp",
13011303
"core/src/utils/statistics/histogram_benchmark.cpp":"taxi/uservices/userver/core/src/utils/statistics/histogram_benchmark.cpp",
13021304
"core/src/utils/statistics/histogram_test.cpp":"taxi/uservices/userver/core/src/utils/statistics/histogram_test.cpp",
13031305
"core/src/utils/statistics/histogram_view.cpp":"taxi/uservices/userver/core/src/utils/statistics/histogram_view.cpp",

clickhouse/src/storages/clickhouse/cluster.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <userver/formats/common/merge.hpp>
66
#include <userver/formats/json/value_builder.hpp>
77
#include <userver/utils/async.hpp>
8-
#include <userver/utils/statistics/metadata.hpp>
98

109
#include <storages/clickhouse/impl/settings.hpp>
1110

core/include/userver/utils/statistics/histogram.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ struct BoundsBlock;
5555
/// This can be useful for writing custom metric serialization formats or
5656
/// for testing.
5757
///
58+
/// Histogram metrics can be summed using
59+
/// utils::statistics::HistogramAggregator.
60+
///
5861
/// Histogram can be used in utils::statistics::MetricTag:
5962
/// @snippet utils/statistics/histogram_test.cpp metric tag
6063
class Histogram final {
@@ -75,10 +78,6 @@ class Histogram final {
7578
/// Atomically increment the bucket corresponding to the given value.
7679
void Account(double value, std::uint64_t count = 1) noexcept;
7780

78-
/// Add the other histogram to the current one. Bucket borders in `this` and
79-
/// `other` must be identical. Writes to `*this` are non-atomic.
80-
void Add(HistogramView other);
81-
8281
/// Atomically reset all counters to zero.
8382
friend void ResetMetric(Histogram& histogram) noexcept;
8483

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
12+
USERVER_NAMESPACE_BEGIN
13+
14+
namespace 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
20+
class 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+
/// Allows reading the histogram.
37+
HistogramView GetView() const& noexcept;
38+
39+
/// @cond
40+
// Store Histogram in a variable before taking a view on it.
41+
HistogramView GetView() && noexcept = delete;
42+
/// @endcond
43+
44+
private:
45+
std::unique_ptr<impl::histogram::Bucket[]> buckets_;
46+
};
47+
48+
/// Metric serialization support for HistogramAggregator.
49+
void DumpMetric(Writer& writer, const HistogramAggregator& histogram);
50+
51+
} // namespace utils::statistics
52+
53+
USERVER_NAMESPACE_END

core/include/userver/utils/statistics/recentperiod.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,14 @@ class RecentPeriod {
176176
mutable std::vector<EpochBucket> items_;
177177
};
178178

179-
/// @brief @a Writer support for @a RecentPeriod. Forwards to `DumpMetric`
180-
/// overload for `Result`.
181-
///
182-
/// @param args if any, are forwarded to `DumpMetric` for `Result`
179+
/// @a Writer support for @a RecentPeriod
183180
template <typename Counter, typename Result, typename Timer>
184181
void DumpMetric(Writer& writer,
185182
const RecentPeriod<Counter, Result, Timer>& recent_period) {
186183
writer = recent_period.GetStatsForPeriod();
187184
}
188185

186+
/// Reset support for @a RecentPeriod
189187
template <typename Counter, typename Result, typename Timer>
190188
void ResetMetric(RecentPeriod<Counter, Result, Timer>& recent_period) {
191189
recent_period.Reset();

core/src/cache/cache_update_trait_impl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <userver/utils/async.hpp>
1515
#include <userver/utils/atomic.hpp>
1616
#include <userver/utils/datetime.hpp>
17-
#include <userver/utils/statistics/metadata.hpp>
1817

1918
#include <cache/cache_dependencies.hpp>
2019
#include <dump/dump_locator.hpp>

core/src/cache/lru_cache_component_base.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <userver/dump/dumper.hpp>
88
#include <userver/dynamic_config/storage/component.hpp>
99
#include <userver/testsuite/testsuite_support.hpp>
10-
#include <userver/utils/statistics/metadata.hpp>
1110
#include <userver/yaml_config/merge_schemas.hpp>
1211
#include <userver/yaml_config/schema.hpp>
1312

core/src/clients/dns/component.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <userver/components/component.hpp>
55
#include <userver/components/statistics_storage.hpp>
66
#include <userver/formats/json/inline.hpp>
7-
#include <userver/formats/json/value_builder.hpp>
8-
#include <userver/formats/parse/common_containers.hpp>
9-
#include <userver/utils/statistics/metadata.hpp>
107
#include <userver/yaml_config/merge_schemas.hpp>
118

129
USERVER_NAMESPACE_BEGIN

core/src/clients/http/component.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <userver/components/statistics_storage.hpp>
99
#include <userver/dynamic_config/storage/component.hpp>
1010
#include <userver/testsuite/testsuite_support.hpp>
11-
#include <userver/utils/statistics/metadata.hpp>
1211

1312
#include <clients/http/destination_statistics.hpp>
1413
#include <clients/http/statistics.hpp>

core/src/components/manager_controller_component.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <userver/dynamic_config/storage/component.hpp>
99
#include <userver/dynamic_config/value.hpp>
1010
#include <userver/logging/component.hpp>
11-
#include <userver/utils/statistics/metadata.hpp>
1211

1312
#include <components/manager.hpp>
1413

0 commit comments

Comments
 (0)