22// SPDX-License-Identifier: Apache-2.0
33
44#include " opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
5+ #include " opentelemetry/version.h"
6+
57#include < algorithm>
68#include < iomanip>
79#include < limits>
810#include < memory>
9- #include " opentelemetry/version.h"
10-
1111#include < mutex>
12+
1213OPENTELEMETRY_BEGIN_NAMESPACE
1314namespace sdk
1415{
@@ -59,16 +60,8 @@ void LongHistogramAggregation::Aggregate(int64_t value,
5960 point_data_.min_ = std::min (nostd::get<int64_t >(point_data_.min_ ), value);
6061 point_data_.max_ = std::max (nostd::get<int64_t >(point_data_.max_ ), value);
6162 }
62- size_t index = 0 ;
63- for (auto it = point_data_.boundaries_ .begin (); it != point_data_.boundaries_ .end (); ++it)
64- {
65- if (value < *it)
66- {
67- point_data_.counts_ [index] += 1 ;
68- return ;
69- }
70- index++;
71- }
63+ size_t index = BucketBinarySearch (value, point_data_.boundaries_ );
64+ point_data_.counts_ [index] += 1 ;
7265}
7366
7467std::unique_ptr<Aggregation> LongHistogramAggregation::Merge (
@@ -77,7 +70,10 @@ std::unique_ptr<Aggregation> LongHistogramAggregation::Merge(
7770 auto curr_value = nostd::get<HistogramPointData>(ToPoint ());
7871 auto delta_value = nostd::get<HistogramPointData>(
7972 (static_cast <const LongHistogramAggregation &>(delta).ToPoint ()));
80- LongHistogramAggregation *aggr = new LongHistogramAggregation ();
73+ HistogramAggregationConfig agg_config;
74+ agg_config.boundaries_ = curr_value.boundaries_ ;
75+ agg_config.record_min_max_ = record_min_max_;
76+ LongHistogramAggregation *aggr = new LongHistogramAggregation (&agg_config);
8177 HistogramMerge<int64_t >(curr_value, delta_value, aggr->point_data_ );
8278 return std::unique_ptr<Aggregation>(aggr);
8379}
@@ -87,7 +83,10 @@ std::unique_ptr<Aggregation> LongHistogramAggregation::Diff(const Aggregation &n
8783 auto curr_value = nostd::get<HistogramPointData>(ToPoint ());
8884 auto next_value = nostd::get<HistogramPointData>(
8985 (static_cast <const LongHistogramAggregation &>(next).ToPoint ()));
90- LongHistogramAggregation *aggr = new LongHistogramAggregation ();
86+ HistogramAggregationConfig agg_config;
87+ agg_config.boundaries_ = curr_value.boundaries_ ;
88+ agg_config.record_min_max_ = record_min_max_;
89+ LongHistogramAggregation *aggr = new LongHistogramAggregation (&agg_config);
9190 HistogramDiff<int64_t >(curr_value, next_value, aggr->point_data_ );
9291 return std::unique_ptr<Aggregation>(aggr);
9392}
@@ -107,8 +106,8 @@ DoubleHistogramAggregation::DoubleHistogramAggregation(const AggregationConfig *
107106 }
108107 else
109108 {
110- point_data_.boundaries_ =
111- std::list< double >{ 0.0 , 5.0 , 10.0 , 25 .0 , 50 .0 , 75 .0 , 100 .0 , 250 .0 , 500 .0 , 1000 .0 };
109+ point_data_.boundaries_ = { 0.0 , 5.0 , 10.0 , 25.0 , 50.0 , 75.0 , 100.0 , 250.0 ,
110+ 500 .0 , 750 .0 , 1000 .0 , 2500 .0 , 5000 .0 , 7500 .0 , 10000 .0 };
112111 }
113112 if (ac)
114113 {
@@ -141,16 +140,8 @@ void DoubleHistogramAggregation::Aggregate(double value,
141140 point_data_.min_ = std::min (nostd::get<double >(point_data_.min_ ), value);
142141 point_data_.max_ = std::max (nostd::get<double >(point_data_.max_ ), value);
143142 }
144- size_t index = 0 ;
145- for (auto it = point_data_.boundaries_ .begin (); it != point_data_.boundaries_ .end (); ++it)
146- {
147- if (value < *it)
148- {
149- point_data_.counts_ [index] += 1 ;
150- return ;
151- }
152- index++;
153- }
143+ size_t index = BucketBinarySearch (value, point_data_.boundaries_ );
144+ point_data_.counts_ [index] += 1 ;
154145}
155146
156147std::unique_ptr<Aggregation> DoubleHistogramAggregation::Merge (
@@ -159,12 +150,10 @@ std::unique_ptr<Aggregation> DoubleHistogramAggregation::Merge(
159150 auto curr_value = nostd::get<HistogramPointData>(ToPoint ());
160151 auto delta_value = nostd::get<HistogramPointData>(
161152 (static_cast <const DoubleHistogramAggregation &>(delta).ToPoint ()));
162- std::shared_ptr<AggregationConfig> aggregation_config (new HistogramAggregationConfig);
163- static_cast <opentelemetry::sdk::metrics::HistogramAggregationConfig *>(aggregation_config.get ())
164- ->boundaries_ = curr_value.boundaries_ ;
165- static_cast <opentelemetry::sdk::metrics::HistogramAggregationConfig *>(aggregation_config.get ())
166- ->record_min_max_ = record_min_max_;
167- DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation (aggregation_config.get ());
153+ HistogramAggregationConfig agg_config;
154+ agg_config.boundaries_ = curr_value.boundaries_ ;
155+ agg_config.record_min_max_ = record_min_max_;
156+ DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation (&agg_config);
168157 HistogramMerge<double >(curr_value, delta_value, aggr->point_data_ );
169158 return std::unique_ptr<Aggregation>(aggr);
170159}
@@ -175,7 +164,10 @@ std::unique_ptr<Aggregation> DoubleHistogramAggregation::Diff(
175164 auto curr_value = nostd::get<HistogramPointData>(ToPoint ());
176165 auto next_value = nostd::get<HistogramPointData>(
177166 (static_cast <const DoubleHistogramAggregation &>(next).ToPoint ()));
178- DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation ();
167+ HistogramAggregationConfig agg_config;
168+ agg_config.boundaries_ = curr_value.boundaries_ ;
169+ agg_config.record_min_max_ = record_min_max_;
170+ DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation (&agg_config);
179171 HistogramDiff<double >(curr_value, next_value, aggr->point_data_ );
180172 return std::unique_ptr<Aggregation>(aggr);
181173}
0 commit comments