Skip to content

Commit 293e2dd

Browse files
committed
Fix comments and errors in the pipeline
1 parent 53e025e commit 293e2dd

File tree

5 files changed

+56
-49
lines changed

5 files changed

+56
-49
lines changed

examples/common/metrics_foo_library/foo_library.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void foo_library::histogram_exp_example(const std::string &name)
116116
{
117117
std::string histogram_name = name + "_exponential_histogram";
118118
auto provider = metrics_api::Provider::GetMeterProvider();
119-
auto meter = provider->GetMeter(name, "1.2.0");
119+
auto meter = provider->GetMeter(name, "1.2.0");
120120
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "histogram-unit");
121121
auto context = opentelemetry::context::Context{};
122122
for (uint32_t i = 0; i < 20; ++i)

sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Base2ExponentialHistogramAggregation : public Aggregation
4545
PointType ToPoint() const noexcept override;
4646

4747
private:
48-
void AggregateIntoBuckets(AdaptingCircularBufferCounter *buckets, double value) noexcept;
48+
void AggregateIntoBuckets(AdaptingCircularBufferCounter &buckets, double value) noexcept;
4949
void Downscale(uint32_t by) noexcept;
5050

5151
mutable opentelemetry::common::SpinLockMutex lock_;

sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
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

154154
void 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;

sdk/test/metrics/aggregation_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ TEST(Aggregation, DoubleHistogramAggregation)
225225
EXPECT_EQ(histogram_data.counts_[7], 1); // aggr2(105.0) - aggr1(0)
226226
}
227227

228-
TEST(aggregation, Base2ExponentialHistogramAggregation)
228+
TEST(Aggregation, Base2ExponentialHistogramAggregation)
229229
{
230230
// Low res histo
231231
auto SCALE0 = 0;

sdk/test/metrics/sync_metric_storage_histogram_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ TEST_P(WritableMetricStorageHistogramTestFixture, Base2ExponentialDoubleHistogra
485485
EXPECT_EQ(data.count_, 3);
486486
EXPECT_EQ(data.min_, 10);
487487
EXPECT_EQ(data.max_, 50);
488-
auto count = 0;
489-
for (int i = start_index; i <= end_index; ++i)
488+
uint64_t count = 0;
489+
for (auto i = start_index; i <= end_index; ++i)
490490
{
491491
count += data.positive_buckets_.Get(i);
492492
}
@@ -513,8 +513,8 @@ TEST_P(WritableMetricStorageHistogramTestFixture, Base2ExponentialDoubleHistogra
513513
EXPECT_EQ(data.count_, 3);
514514
EXPECT_EQ(data.min_, 30);
515515
EXPECT_EQ(data.max_, 40);
516-
auto count = 0;
517-
for (int i = start_index; i <= end_index; ++i)
516+
uint64_t count = 0;
517+
for (auto i = start_index; i <= end_index; ++i)
518518
{
519519
count += data.positive_buckets_.Get(i);
520520
}

0 commit comments

Comments
 (0)