55#include < stddef.h>
66#include < stdint.h>
77#include < algorithm>
8+ #include < iostream>
89#include < limits>
910#include < memory>
10- #include < iostream>
1111#include < mutex>
1212#include < utility>
1313#include " opentelemetry/common/spin_lock_mutex.h"
@@ -189,7 +189,9 @@ void Base2ExponentialHistogramAggregation::Downscale(uint32_t by) noexcept
189189
190190// Merge A and B into a new circular buffer C.
191191// Caller must ensure that A and B are used as buckets at the same scale.
192- AdaptingCircularBufferCounter MergeBuckets (size_t max_buckets, const AdaptingCircularBufferCounter &A, const AdaptingCircularBufferCounter &B)
192+ AdaptingCircularBufferCounter MergeBuckets (size_t max_buckets,
193+ const AdaptingCircularBufferCounter &A,
194+ const AdaptingCircularBufferCounter &B)
193195{
194196 AdaptingCircularBufferCounter C = AdaptingCircularBufferCounter (max_buckets);
195197 C.Clear ();
@@ -217,7 +219,7 @@ AdaptingCircularBufferCounter MergeBuckets(size_t max_buckets, const AdaptingCir
217219 {
218220 C.Increment (i, count);
219221 }
220- }
222+ }
221223
222224 return C;
223225}
@@ -234,13 +236,12 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
234236 auto scale_reduction = high_res.scale_ - low_res.scale_ ;
235237
236238 Base2ExponentialHistogramPointData result_value;
237- result_value.count_ = low_res.count_ + high_res.count_ ;
238- result_value.sum_ = low_res.sum_ + high_res.sum_ ;
239- result_value.zero_count_ = low_res.zero_count_ + high_res.zero_count_ ;
240- result_value.scale_ = (std::min)(low_res.scale_ , high_res.scale_ );
241- result_value.max_buckets_ = low_res.max_buckets_ >= high_res.max_buckets_
242- ? low_res.max_buckets_
243- : high_res.max_buckets_ ;
239+ result_value.count_ = low_res.count_ + high_res.count_ ;
240+ result_value.sum_ = low_res.sum_ + high_res.sum_ ;
241+ result_value.zero_count_ = low_res.zero_count_ + high_res.zero_count_ ;
242+ result_value.scale_ = (std::min)(low_res.scale_ , high_res.scale_ );
243+ result_value.max_buckets_ =
244+ low_res.max_buckets_ >= high_res.max_buckets_ ? low_res.max_buckets_ : high_res.max_buckets_ ;
244245 result_value.record_min_max_ = low_res.record_min_max_ && high_res.record_min_max_ ;
245246 if (result_value.record_min_max_ )
246247 {
@@ -255,15 +256,21 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
255256 high_res.scale_ -= scale_reduction;
256257 }
257258
258- auto pos_min_index = (std::min)(low_res.positive_buckets_ .StartIndex (), high_res.positive_buckets_ .StartIndex ());
259- auto pos_max_index = (std::max)(low_res.positive_buckets_ .EndIndex (), high_res.positive_buckets_ .EndIndex ());
260- auto neg_min_index = (std::min)(low_res.negative_buckets_ .StartIndex (), high_res.negative_buckets_ .StartIndex ());
261- auto neg_max_index = (std::max)(low_res.negative_buckets_ .EndIndex (), high_res.negative_buckets_ .EndIndex ());
262-
263- if (pos_max_index > pos_min_index + result_value.max_buckets_ || neg_max_index > neg_min_index + result_value.max_buckets_ )
259+ auto pos_min_index =
260+ (std::min)(low_res.positive_buckets_ .StartIndex (), high_res.positive_buckets_ .StartIndex ());
261+ auto pos_max_index =
262+ (std::max)(low_res.positive_buckets_ .EndIndex (), high_res.positive_buckets_ .EndIndex ());
263+ auto neg_min_index =
264+ (std::min)(low_res.negative_buckets_ .StartIndex (), high_res.negative_buckets_ .StartIndex ());
265+ auto neg_max_index =
266+ (std::max)(low_res.negative_buckets_ .EndIndex (), high_res.negative_buckets_ .EndIndex ());
267+
268+ if (pos_max_index > pos_min_index + result_value.max_buckets_ ||
269+ neg_max_index > neg_min_index + result_value.max_buckets_ )
264270 {
265271 // We need to downscale the buckets to fit into the new max_buckets_.
266- const uint32_t scale_reduction = GetScaleReduction (pos_min_index, pos_max_index, result_value.max_buckets_ );
272+ const uint32_t scale_reduction =
273+ GetScaleReduction (pos_min_index, pos_max_index, result_value.max_buckets_ );
267274 DownscaleBuckets (&low_res.positive_buckets_ , scale_reduction);
268275 DownscaleBuckets (&high_res.positive_buckets_ , scale_reduction);
269276 DownscaleBuckets (&low_res.negative_buckets_ , scale_reduction);
@@ -273,10 +280,10 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
273280 result_value.scale_ -= scale_reduction;
274281 }
275282
276- result_value.positive_buckets_ = MergeBuckets (result_value. max_buckets_ , low_res. positive_buckets_ ,
277- high_res.positive_buckets_ );
278- result_value.negative_buckets_ = MergeBuckets (result_value. max_buckets_ , low_res. negative_buckets_ ,
279- high_res.negative_buckets_ );
283+ result_value.positive_buckets_ = MergeBuckets (
284+ result_value. max_buckets_ , low_res. positive_buckets_ , high_res.positive_buckets_ );
285+ result_value.negative_buckets_ = MergeBuckets (
286+ result_value. max_buckets_ , low_res. negative_buckets_ , high_res.negative_buckets_ );
280287
281288 return std::unique_ptr<Base2ExponentialHistogramAggregation>{
282289 new Base2ExponentialHistogramAggregation (std::move (result_value))};
@@ -300,15 +307,21 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
300307 high_res.scale_ -= scale_reduction;
301308 }
302309
303- auto pos_min_index = (std::min)(left.positive_buckets_ .StartIndex (), right.positive_buckets_ .StartIndex ());
304- auto pos_max_index = (std::max)(left.positive_buckets_ .EndIndex (), right.positive_buckets_ .EndIndex ());
305- auto neg_min_index = (std::min)(left.negative_buckets_ .StartIndex (), right.negative_buckets_ .StartIndex ());
306- auto neg_max_index = (std::max)(left.negative_buckets_ .EndIndex (), right.negative_buckets_ .EndIndex ());
307-
308- if (pos_max_index > pos_min_index + low_res.max_buckets_ || neg_max_index > neg_min_index + low_res.max_buckets_ )
310+ auto pos_min_index =
311+ (std::min)(left.positive_buckets_ .StartIndex (), right.positive_buckets_ .StartIndex ());
312+ auto pos_max_index =
313+ (std::max)(left.positive_buckets_ .EndIndex (), right.positive_buckets_ .EndIndex ());
314+ auto neg_min_index =
315+ (std::min)(left.negative_buckets_ .StartIndex (), right.negative_buckets_ .StartIndex ());
316+ auto neg_max_index =
317+ (std::max)(left.negative_buckets_ .EndIndex (), right.negative_buckets_ .EndIndex ());
318+
319+ if (pos_max_index > pos_min_index + low_res.max_buckets_ ||
320+ neg_max_index > neg_min_index + low_res.max_buckets_ )
309321 {
310322 // We need to downscale the buckets to fit into the new max_buckets_.
311- const uint32_t scale_reduction = GetScaleReduction (pos_min_index, pos_max_index, low_res.max_buckets_ );
323+ const uint32_t scale_reduction =
324+ GetScaleReduction (pos_min_index, pos_max_index, low_res.max_buckets_ );
312325 DownscaleBuckets (&left.positive_buckets_ , scale_reduction);
313326 DownscaleBuckets (&right.positive_buckets_ , scale_reduction);
314327 DownscaleBuckets (&left.negative_buckets_ , scale_reduction);
@@ -322,7 +335,8 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
322335 result_value.max_buckets_ = low_res.max_buckets_ ;
323336 result_value.record_min_max_ = false ;
324337 // caution for underflow
325- // expect right.{sum, count} >= left.{sum, count} since metric points should be monotonically increasing
338+ // expect right.{sum, count} >= left.{sum, count} since metric points should be monotonically
339+ // increasing
326340 result_value.count_ = (right.count_ >= left.count_ ) ? (right.count_ - left.count_ ) : 0.0 ;
327341 result_value.sum_ = (right.sum_ >= left.sum_ ) ? (right.sum_ - left.sum_ ) : 0.0 ;
328342 result_value.zero_count_ =
0 commit comments