11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- #include " opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h"
54#include < stddef.h>
65#include < stdint.h>
76#include < algorithm>
8- #include < iostream>
97#include < limits>
108#include < memory>
119#include < mutex>
1210#include < utility>
11+
1312#include " opentelemetry/common/spin_lock_mutex.h"
1413#include " opentelemetry/nostd/variant.h"
1514#include " opentelemetry/sdk/metrics/aggregation/aggregation.h"
1615#include " opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
16+ #include " opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h"
1717#include " opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h"
1818#include " opentelemetry/sdk/metrics/data/circular_buffer.h"
1919#include " opentelemetry/sdk/metrics/data/metric_data.h"
@@ -55,9 +55,9 @@ uint32_t GetScaleReduction(int32_t start_index, int32_t end_index, size_t max_bu
5555// return GetScaleReduction(start_index, end_index, max_buckets);
5656// }
5757
58- void DownscaleBuckets (AdaptingCircularBufferCounter * buckets, uint32_t by) noexcept
58+ void DownscaleBuckets (AdaptingCircularBufferCounter & buckets, uint32_t by) noexcept
5959{
60- if (buckets-> Empty ())
60+ if (buckets. Empty ())
6161 {
6262 return ;
6363 }
@@ -66,18 +66,18 @@ void DownscaleBuckets(AdaptingCircularBufferCounter *buckets, uint32_t by) noexc
6666 // Instead of creating a new counter, we copy the existing one (for bucket size
6767 // optimisations), and clear the values before writing the new ones.
6868 // TODO(euroelessar): Do downscaling in-place.
69- AdaptingCircularBufferCounter new_buckets = * buckets;
69+ auto new_buckets = buckets;
7070 new_buckets.Clear ();
7171
72- for (int i = buckets-> StartIndex (); i <= buckets-> EndIndex (); i++ )
72+ for (auto i = buckets. StartIndex (); i <= buckets. EndIndex (); ++i )
7373 {
74- const uint64_t count = buckets-> Get (i);
74+ const uint64_t count = buckets. Get (i);
7575 if (count > 0 )
7676 {
7777 new_buckets.Increment (i >> by, count);
7878 }
7979 }
80- * buckets = std::move (new_buckets);
80+ buckets = std::move (new_buckets);
8181}
8282
8383} // namespace
@@ -143,33 +143,33 @@ void Base2ExponentialHistogramAggregation::Aggregate(
143143 }
144144 else if (value > 0 )
145145 {
146- AggregateIntoBuckets (& point_data_.positive_buckets_ , value);
146+ AggregateIntoBuckets (point_data_.positive_buckets_ , value);
147147 }
148148 else
149149 {
150- AggregateIntoBuckets (& point_data_.negative_buckets_ , -value);
150+ AggregateIntoBuckets (point_data_.negative_buckets_ , -value);
151151 }
152152}
153153
154154void Base2ExponentialHistogramAggregation::AggregateIntoBuckets (
155- AdaptingCircularBufferCounter * buckets,
155+ AdaptingCircularBufferCounter & buckets,
156156 double value) noexcept
157157{
158- if (buckets-> MaxSize () == 0 )
158+ if (buckets. MaxSize () == 0 )
159159 {
160- * buckets = AdaptingCircularBufferCounter{point_data_.max_buckets_ };
160+ buckets = AdaptingCircularBufferCounter{point_data_.max_buckets_ };
161161 }
162162
163163 const int32_t index = indexer_.ComputeIndex (value);
164- if (!buckets-> Increment (index, 1 ))
164+ if (!buckets. Increment (index, 1 ))
165165 {
166- const int32_t start_index = (std::min)(buckets-> StartIndex (), index);
167- const int32_t end_index = (std::max)(buckets-> EndIndex (), index);
166+ const int32_t start_index = (std::min)(buckets. StartIndex (), index);
167+ const int32_t end_index = (std::max)(buckets. EndIndex (), index);
168168 const uint32_t scale_reduction =
169169 GetScaleReduction (start_index, end_index, point_data_.max_buckets_ );
170170 Downscale (scale_reduction);
171171
172- buckets-> Increment (index >> scale_reduction, 1 );
172+ buckets. Increment (index >> scale_reduction, 1 );
173173 }
174174}
175175
@@ -180,8 +180,8 @@ void Base2ExponentialHistogramAggregation::Downscale(uint32_t by) noexcept
180180 return ;
181181 }
182182
183- DownscaleBuckets (& point_data_.positive_buckets_ , by);
184- DownscaleBuckets (& point_data_.negative_buckets_ , by);
183+ DownscaleBuckets (point_data_.positive_buckets_ , by);
184+ DownscaleBuckets (point_data_.negative_buckets_ , by);
185185
186186 point_data_.scale_ -= by;
187187 indexer_ = Base2ExponentialHistogramIndexer (point_data_.scale_ );
@@ -231,9 +231,8 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
231231 auto right = nostd::get<Base2ExponentialHistogramPointData>(
232232 (static_cast <const Base2ExponentialHistogramAggregation &>(delta).ToPoint ()));
233233
234- auto low_res = left.scale_ < right.scale_ ? left : right;
235- auto high_res = left.scale_ < right.scale_ ? right : left;
236- auto scale_reduction = high_res.scale_ - low_res.scale_ ;
234+ auto &low_res = left.scale_ < right.scale_ ? left : right;
235+ auto &high_res = left.scale_ < right.scale_ ? right : left;
237236
238237 Base2ExponentialHistogramPointData result_value;
239238 result_value.count_ = low_res.count_ + high_res.count_ ;
@@ -243,17 +242,22 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
243242 result_value.max_buckets_ =
244243 low_res.max_buckets_ >= high_res.max_buckets_ ? low_res.max_buckets_ : high_res.max_buckets_ ;
245244 result_value.record_min_max_ = low_res.record_min_max_ && high_res.record_min_max_ ;
245+
246246 if (result_value.record_min_max_ )
247247 {
248248 result_value.min_ = (std::min)(low_res.min_ , high_res.min_ );
249249 result_value.max_ = (std::max)(low_res.max_ , high_res.max_ );
250250 }
251251
252- if (scale_reduction > 0 )
253252 {
254- DownscaleBuckets (&high_res.positive_buckets_ , scale_reduction);
255- DownscaleBuckets (&high_res.negative_buckets_ , scale_reduction);
256- high_res.scale_ -= scale_reduction;
253+ auto scale_reduction = high_res.scale_ - low_res.scale_ ;
254+
255+ if (scale_reduction > 0 )
256+ {
257+ DownscaleBuckets (high_res.positive_buckets_ , scale_reduction);
258+ DownscaleBuckets (high_res.negative_buckets_ , scale_reduction);
259+ high_res.scale_ -= scale_reduction;
260+ }
257261 }
258262
259263 auto pos_min_index =
@@ -273,10 +277,10 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
273277 // We need to downscale the buckets to fit into the new max_buckets_.
274278 const uint32_t scale_reduction =
275279 GetScaleReduction (pos_min_index, pos_max_index, result_value.max_buckets_ );
276- DownscaleBuckets (& low_res.positive_buckets_ , scale_reduction);
277- DownscaleBuckets (& high_res.positive_buckets_ , scale_reduction);
278- DownscaleBuckets (& low_res.negative_buckets_ , scale_reduction);
279- DownscaleBuckets (& high_res.negative_buckets_ , scale_reduction);
280+ DownscaleBuckets (low_res.positive_buckets_ , scale_reduction);
281+ DownscaleBuckets (high_res.positive_buckets_ , scale_reduction);
282+ DownscaleBuckets (low_res.negative_buckets_ , scale_reduction);
283+ DownscaleBuckets (high_res.negative_buckets_ , scale_reduction);
280284 low_res.scale_ -= scale_reduction;
281285 high_res.scale_ -= scale_reduction;
282286 result_value.scale_ -= scale_reduction;
@@ -298,15 +302,18 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
298302 auto right = nostd::get<Base2ExponentialHistogramPointData>(
299303 (static_cast <const Base2ExponentialHistogramAggregation &>(next).ToPoint ()));
300304
301- auto low_res = left.scale_ < right.scale_ ? left : right;
302- auto high_res = left.scale_ < right.scale_ ? right : left;
303- auto scale_reduction = high_res.scale_ - low_res.scale_ ;
305+ auto &low_res = left.scale_ < right.scale_ ? left : right;
306+ auto &high_res = left.scale_ < right.scale_ ? right : left;
304307
305- if (scale_reduction > 0 )
306308 {
307- DownscaleBuckets (&high_res.positive_buckets_ , scale_reduction);
308- DownscaleBuckets (&high_res.negative_buckets_ , scale_reduction);
309- high_res.scale_ -= scale_reduction;
309+ const auto scale_reduction = high_res.scale_ - low_res.scale_ ;
310+
311+ if (scale_reduction > 0 )
312+ {
313+ DownscaleBuckets (high_res.positive_buckets_ , scale_reduction);
314+ DownscaleBuckets (high_res.negative_buckets_ , scale_reduction);
315+ high_res.scale_ -= scale_reduction;
316+ }
310317 }
311318
312319 auto pos_min_index =
@@ -326,10 +333,10 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
326333 // We need to downscale the buckets to fit into the new max_buckets_.
327334 const uint32_t scale_reduction =
328335 GetScaleReduction (pos_min_index, pos_max_index, low_res.max_buckets_ );
329- DownscaleBuckets (& left.positive_buckets_ , scale_reduction);
330- DownscaleBuckets (& right.positive_buckets_ , scale_reduction);
331- DownscaleBuckets (& left.negative_buckets_ , scale_reduction);
332- DownscaleBuckets (& right.negative_buckets_ , scale_reduction);
336+ DownscaleBuckets (left.positive_buckets_ , scale_reduction);
337+ DownscaleBuckets (right.positive_buckets_ , scale_reduction);
338+ DownscaleBuckets (left.negative_buckets_ , scale_reduction);
339+ DownscaleBuckets (right.negative_buckets_ , scale_reduction);
333340 left.scale_ -= scale_reduction;
334341 right.scale_ -= scale_reduction;
335342 }
@@ -341,7 +348,7 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
341348 // caution for underflow
342349 // expect right.{sum, count} >= left.{sum, count} since metric points should be monotonically
343350 // increasing
344- result_value.count_ = (right.count_ >= left.count_ ) ? (right.count_ - left.count_ ) : 0.0 ;
351+ result_value.count_ = (right.count_ >= left.count_ ) ? (right.count_ - left.count_ ) : 0 ;
345352 result_value.sum_ = (right.sum_ >= left.sum_ ) ? (right.sum_ - left.sum_ ) : 0.0 ;
346353 result_value.zero_count_ =
347354 (right.zero_count_ >= left.zero_count_ ) ? (right.zero_count_ - left.zero_count_ ) : 0 ;
0 commit comments