Skip to content

Commit 8377879

Browse files
committed
address comments - allow empty and disallow nan
1 parent 2baf92e commit 8377879

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333

3434

3535
- **Bug Fix:** Validates the `with_boundaries` bucket boundaries used in
36-
Histograms. The boundaries provided by the user must be a non-empty vector,
37-
sorted in strictly increasing order, and contain no duplicates. Instruments
38-
will not record measurements if the boundaries are invalid.
36+
Histograms. The boundaries provided by the user must not contain `f64::NAN`,
37+
`f64::INFINITY`, `f64::NEG_INFINITY` and must be sorted in strictly
38+
increasing order, and contain no duplicates. Instruments will not record
39+
measurements if the boundaries are invalid.
3940
[#2351](https://github.com/open-telemetry/opentelemetry-rust/pull/2351)
4041

4142
## 0.27.0

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl SdkMeter {
395395
}
396396

397397
if let Some(ref boundaries) = builder.boundaries {
398-
let validation_result = validate_buckets(boundaries);
398+
let validation_result = validate_bucket_boundaries(boundaries);
399399
if let Err(err) = validation_result {
400400
// TODO: Include the buckets too in the error message.
401401
// TODO: This validation is not done when Views are used to
@@ -550,16 +550,19 @@ fn validate_instrument_config(name: &str, unit: &Option<Cow<'static, str>>) -> M
550550
validate_instrument_name(name).and_then(|_| validate_instrument_unit(unit))
551551
}
552552

553-
fn validate_buckets(buckets: &[f64]) -> MetricResult<()> {
554-
if buckets.is_empty() {
555-
return Err(MetricError::InvalidInstrumentConfiguration(
556-
"Buckets must not be empty",
557-
));
553+
fn validate_bucket_boundaries(boundaries: &[f64]) -> MetricResult<()> {
554+
// Validate boundaries do not contain f64::NAN, f64::INFINITY, or f64::NEG_INFINITY
555+
for boundary in boundaries {
556+
if boundary.is_nan() || boundary.is_infinite() {
557+
return Err(MetricError::InvalidInstrumentConfiguration(
558+
"Buckets must not contain NaN, +Inf, or -Inf",
559+
));
560+
}
558561
}
559562

560563
// validate that buckets are sorted and non-duplicate
561-
for window in buckets.windows(2) {
562-
if window[0] >= window[1] {
564+
for i in 1..boundaries.len() {
565+
if boundaries[i] <= boundaries[i - 1] {
563566
return Err(MetricError::InvalidInstrumentConfiguration(
564567
"Buckets must be sorted and non-duplicate",
565568
));

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,26 @@ mod tests {
179179
test_context.check_no_metrics();
180180
}
181181

182-
let invalid_buckets = vec![
183-
vec![], // empty buckets
184-
vec![1.0, 1.0], // duplicate buckets
185-
vec![1.0, 2.0, 3.0, 2.0], // duplicate non consequent buckets
186-
vec![1.0, 2.0, 3.0, 4.0, 2.5], // unsorted buckets
182+
let invalid_bucket_boundaries = vec![
183+
vec![1.0, 1.0], // duplicate boundaries
184+
vec![1.0, 2.0, 3.0, 2.0], // duplicate non consequent boundaries
185+
vec![1.0, 2.0, 3.0, 4.0, 2.5], // unsorted boundaries
186+
vec![1.0, 2.0, 3.0, f64::INFINITY, 4.0], // boundaries with NaNs
187+
vec![1.0, 2.0, 3.0, f64::NAN], // boundaries with NaNs
188+
vec![f64::NEG_INFINITY, 2.0, 3.0], // boundaries with NaNs
187189
];
188-
for buckets in invalid_buckets {
190+
for bucket_boundaries in invalid_bucket_boundaries {
189191
let test_context = TestContext::new(Temporality::Cumulative);
190192
let histogram = test_context
191193
.meter()
192194
.f64_histogram("test")
193-
.with_boundaries(buckets)
195+
.with_boundaries(bucket_boundaries)
194196
.build();
195197
histogram.record(1.9, &[]);
196198
test_context.flush_metrics();
197199

198-
// As buckets via Advisory params are invalid, no metrics should be
199-
// exported
200+
// As bucket boundaires via Advisory params are invalid, no metrics
201+
// should be exported
200202
test_context.check_no_metrics();
201203
}
202204
}

0 commit comments

Comments
 (0)