Skip to content

Commit 17c64f2

Browse files
fix: validate scale range and raise exception if out of bounds (#1865)
* fix: add check to make sure scale is within the valid range * revision --------- Co-authored-by: Kayla Reopelle <[email protected]>
1 parent 6b3ed53 commit 17c64f2

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ class ExponentialBucketHistogram # rubocop:disable Metrics/ClassLength
1919
attr_reader :aggregation_temporality
2020

2121
# relate to min max scale: https://opentelemetry.io/docs/specs/otel/metrics/sdk/#support-a-minimum-and-maximum-scale
22+
DEFAULT_SIZE = 160
23+
DEFAULT_SCALE = 20
2224
MAX_SCALE = 20
2325
MIN_SCALE = -10
24-
MAX_SIZE = 160
26+
MIN_MAX_SIZE = 2
27+
MAX_MAX_SIZE = 16_384
2528

2629
# The default boundaries are calculated based on default max_size and max_scale values
2730
def initialize(
2831
aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta),
29-
max_size: MAX_SIZE,
30-
max_scale: MAX_SCALE,
32+
max_size: DEFAULT_SIZE,
33+
max_scale: DEFAULT_SCALE,
3134
record_min_max: true,
3235
zero_threshold: 0
3336
)
@@ -175,6 +178,7 @@ def grow_buckets(span, buckets)
175178
end
176179

177180
def new_mapping(scale)
181+
scale = validate_scale(scale)
178182
scale <= 0 ? ExponentialHistogram::ExponentMapping.new(scale) : ExponentialHistogram::LogarithmMapping.new(scale)
179183
end
180184

@@ -203,17 +207,17 @@ def downscale(change, positive, negative)
203207
end
204208

205209
def validate_scale(scale)
206-
return scale unless scale > MAX_SCALE || scale < MIN_SCALE
210+
raise ArgumentError, "Scale #{scale} is larger than maximum scale #{MAX_SCALE}" if scale > MAX_SCALE
211+
raise ArgumentError, "Scale #{scale} is smaller than minimum scale #{MIN_SCALE}" if scale < MIN_SCALE
207212

208-
OpenTelemetry.logger.warn "Scale #{scale} is invalid, using default max scale #{MAX_SCALE}"
209-
MAX_SCALE
213+
scale
210214
end
211215

212216
def validate_size(size)
213-
return size unless size > MAX_SIZE || size < 0
217+
raise ArgumentError, "Max size #{size} is smaller than minimum size #{MIN_MAX_SIZE}" if size < MIN_MAX_SIZE
218+
raise ArgumentError, "Max size #{size} is larger than maximum size #{MAX_MAX_SIZE}" if size > MAX_MAX_SIZE
214219

215-
OpenTelemetry.logger.warn "Size #{size} is invalid, using default max size #{MAX_SIZE}"
216-
MAX_SIZE
220+
size
217221
end
218222
end
219223
end

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,29 @@
255255
it 'test_merge' do
256256
# TODO
257257
end
258+
259+
it 'test_invalid_scale_validation' do
260+
error = assert_raises(ArgumentError) do
261+
OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(max_scale: 100)
262+
end
263+
assert_equal('Scale 100 is larger than maximum scale 20', error.message)
264+
265+
error = assert_raises(ArgumentError) do
266+
OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(max_scale: -20)
267+
end
268+
assert_equal('Scale -20 is smaller than minimum scale -10', error.message)
269+
end
270+
271+
it 'test_invalid_size_validation' do
272+
error = assert_raises(ArgumentError) do
273+
OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(max_size: 10_000_000)
274+
end
275+
assert_equal('Max size 10000000 is larger than maximum size 16384', error.message)
276+
277+
error = assert_raises(ArgumentError) do
278+
OpenTelemetry::SDK::Metrics::Aggregation::ExponentialBucketHistogram.new(max_size: 0)
279+
end
280+
assert_equal('Max size 0 is smaller than minimum size 2', error.message)
281+
end
258282
end
259283
end

0 commit comments

Comments
 (0)