From 00393c6ca09c9ebe67dc224c56ff5dc72bb61c09 Mon Sep 17 00:00:00 2001 From: flaneur Date: Tue, 29 Oct 2024 20:50:34 +0800 Subject: [PATCH] fix(metrics): use f64::MAX as histogram bound to display +Inf properly (#16701) * use f64::MAX as histogram bound * add test --- src/common/base/src/runtime/metrics/registry.rs | 4 ++-- src/common/base/tests/it/metrics/registry.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/common/base/src/runtime/metrics/registry.rs b/src/common/base/src/runtime/metrics/registry.rs index 9bd25c41a712..97897ad043e3 100644 --- a/src/common/base/src/runtime/metrics/registry.rs +++ b/src/common/base/src/runtime/metrics/registry.rs @@ -43,8 +43,8 @@ use crate::runtime::metrics::process_collector::ProcessCollector; use crate::runtime::metrics::sample::MetricSample; use crate::runtime::ThreadTracker; -pub const MIN_HISTOGRAM_BOUND: f64 = i64::MIN as f64; -pub const MAX_HISTOGRAM_BOUND: f64 = i64::MAX as f64; +pub const MIN_HISTOGRAM_BOUND: f64 = f64::MIN; +pub const MAX_HISTOGRAM_BOUND: f64 = f64::MAX; pub static GLOBAL_METRICS_REGISTRY: LazyLock = LazyLock::new(GlobalRegistry::create); diff --git a/src/common/base/tests/it/metrics/registry.rs b/src/common/base/tests/it/metrics/registry.rs index 10378be60d7d..38b3417bad3e 100644 --- a/src/common/base/tests/it/metrics/registry.rs +++ b/src/common/base/tests/it/metrics/registry.rs @@ -261,6 +261,19 @@ fn test_tracking_scoped_histogram_in_milliseconds_metrics() -> Result<()> { Ok(()) } +#[test] +fn test_tracking_histogram_inf() -> Result<()> { + let uniq_metric_name = GlobalUniqName::unique(); + let histogram = register_histogram_in_seconds(&uniq_metric_name); + // observe a value that exceed the max bucket of the histogram + histogram.observe(3600.0 * 355.0); + + let output = GLOBAL_METRICS_REGISTRY.render_metrics()?; + let expected = format!("{}_bucket{{le=\"+Inf\"}} 1", uniq_metric_name); + assert!(output.contains(&expected)); + Ok(()) +} + #[test] fn test_tracking_scoped_family_counter_metrics() -> Result<()> { let uniq_metric_name = GlobalUniqName::unique();