metrics: fix bug with wrong number of buckets for the histogram (#6957)

This commit is contained in:
Russell Cohen 2024-11-07 02:45:36 -05:00 committed by GitHub
parent 8897885425
commit 4a34b77af5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View File

@ -264,14 +264,14 @@ impl HistogramBuilder {
}
None => self.histogram_type,
};
let num_buckets = self.histogram_type.num_buckets();
let num_buckets = histogram_type.num_buckets();
Histogram {
buckets: (0..num_buckets)
.map(|_| MetricAtomicU64::new(0))
.collect::<Vec<_>>()
.into_boxed_slice(),
histogram_type: histogram_type,
histogram_type,
}
}
}
@ -303,6 +303,13 @@ mod test {
.build()
}
#[test]
fn test_legacy_builder() {
let mut builder = HistogramBuilder::new();
builder.legacy_mut(|b| b.num_buckets = 20);
assert_eq!(builder.build().num_buckets(), 20);
}
#[test]
fn log_scale_resolution_1() {
let h = HistogramBuilder {
@ -355,6 +362,9 @@ mod test {
b.measure(4096, 1);
assert_bucket_eq!(b, 9, 1);
b.measure(u64::MAX, 1);
assert_bucket_eq!(b, 9, 2);
}
#[test]

View File

@ -13,7 +13,7 @@ use std::task::Poll;
use std::thread;
use tokio::macros::support::poll_fn;
use tokio::runtime::{HistogramConfiguration, LogHistogram, Runtime};
use tokio::runtime::{HistogramConfiguration, HistogramScale, LogHistogram, Runtime};
use tokio::task::consume_budget;
use tokio::time::{self, Duration};
@ -424,6 +424,21 @@ fn log_histogram() {
assert_eq!(N, n);
}
#[test]
#[allow(deprecated)]
fn legacy_log_histogram() {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.enable_metrics_poll_time_histogram()
.metrics_poll_count_histogram_scale(HistogramScale::Log)
.metrics_poll_count_histogram_resolution(Duration::from_micros(50))
.metrics_poll_count_histogram_buckets(20)
.build()
.unwrap();
let num_buckets = rt.metrics().poll_time_histogram_num_buckets();
assert_eq!(num_buckets, 20);
}
#[test]
fn log_histogram_default_configuration() {
let rt = tokio::runtime::Builder::new_current_thread()