Skip to content

Commit 4a34b77

Browse files
authored
metrics: fix bug with wrong number of buckets for the histogram (tokio-rs#6957)
1 parent 8897885 commit 4a34b77

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

tokio/src/runtime/metrics/histogram.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ impl HistogramBuilder {
264264
}
265265
None => self.histogram_type,
266266
};
267-
let num_buckets = self.histogram_type.num_buckets();
267+
let num_buckets = histogram_type.num_buckets();
268268

269269
Histogram {
270270
buckets: (0..num_buckets)
271271
.map(|_| MetricAtomicU64::new(0))
272272
.collect::<Vec<_>>()
273273
.into_boxed_slice(),
274-
histogram_type: histogram_type,
274+
histogram_type,
275275
}
276276
}
277277
}
@@ -303,6 +303,13 @@ mod test {
303303
.build()
304304
}
305305

306+
#[test]
307+
fn test_legacy_builder() {
308+
let mut builder = HistogramBuilder::new();
309+
builder.legacy_mut(|b| b.num_buckets = 20);
310+
assert_eq!(builder.build().num_buckets(), 20);
311+
}
312+
306313
#[test]
307314
fn log_scale_resolution_1() {
308315
let h = HistogramBuilder {
@@ -355,6 +362,9 @@ mod test {
355362

356363
b.measure(4096, 1);
357364
assert_bucket_eq!(b, 9, 1);
365+
366+
b.measure(u64::MAX, 1);
367+
assert_bucket_eq!(b, 9, 2);
358368
}
359369

360370
#[test]

tokio/tests/rt_unstable_metrics.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::task::Poll;
1313
use std::thread;
1414
use tokio::macros::support::poll_fn;
1515

16-
use tokio::runtime::{HistogramConfiguration, LogHistogram, Runtime};
16+
use tokio::runtime::{HistogramConfiguration, HistogramScale, LogHistogram, Runtime};
1717
use tokio::task::consume_budget;
1818
use tokio::time::{self, Duration};
1919

@@ -424,6 +424,21 @@ fn log_histogram() {
424424
assert_eq!(N, n);
425425
}
426426

427+
#[test]
428+
#[allow(deprecated)]
429+
fn legacy_log_histogram() {
430+
let rt = tokio::runtime::Builder::new_multi_thread()
431+
.enable_all()
432+
.enable_metrics_poll_time_histogram()
433+
.metrics_poll_count_histogram_scale(HistogramScale::Log)
434+
.metrics_poll_count_histogram_resolution(Duration::from_micros(50))
435+
.metrics_poll_count_histogram_buckets(20)
436+
.build()
437+
.unwrap();
438+
let num_buckets = rt.metrics().poll_time_histogram_num_buckets();
439+
assert_eq!(num_buckets, 20);
440+
}
441+
427442
#[test]
428443
fn log_histogram_default_configuration() {
429444
let rt = tokio::runtime::Builder::new_current_thread()

0 commit comments

Comments
 (0)