Skip to content

Commit 7f5db11

Browse files
fix: use mapping scale outside rescale logic (#1891)
* fix: use mapping scale * add test case --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent 463ef11 commit 7f5db11

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ def update(amount, attributes, data_points)
140140
scale_change = get_scale_change(low, high)
141141
downscale(scale_change, hdp.positive, hdp.negative)
142142
new_scale = @mapping.scale - scale_change
143-
hdp.scale = new_scale
144143
@mapping = new_mapping(new_scale)
145144
bucket_index = @mapping.map_to_index(amount)
146145

147146
OpenTelemetry.logger.debug "Rescaled with new scale #{new_scale} from #{low} and #{high}; bucket_index is updated to #{bucket_index}"
148147
end
149148

149+
hdp.scale = @mapping.scale
150+
150151
# adjust buckets based on the bucket_index
151152
if bucket_index < buckets.index_start
152153
span = buckets.index_end - bucket_index

metrics_sdk/test/opentelemetry/sdk/metrics/aggregation/exponential_bucket_histogram_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@
136136
_(exphdps[0].zero_threshold).must_equal(0)
137137
end
138138

139+
it 'adjusts_scale_after_initial_zero_value' do
140+
expbh = OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(
141+
aggregation_temporality: aggregation_temporality,
142+
record_min_max: record_min_max,
143+
zero_threshold: 0
144+
)
145+
146+
expbh.update(0, {}, data_points)
147+
expbh.update(10_000, {}, data_points)
148+
149+
exphdps = expbh.collect(start_time, end_time, data_points)
150+
151+
_(exphdps.size).must_equal(1)
152+
_(exphdps[0].count).must_equal(2)
153+
_(exphdps[0].sum).must_equal(10_000)
154+
_(exphdps[0].min).must_equal(0)
155+
_(exphdps[0].max).must_equal(10_000)
156+
_(exphdps[0].scale).must_equal(20)
157+
_(exphdps[0].zero_count).must_equal(1)
158+
_(exphdps[0].zero_threshold).must_equal(0)
159+
end
160+
139161
it 'test_permutations' do
140162
test_cases = [
141163
[

0 commit comments

Comments
 (0)