Skip to content

metrics: Fix bug where num_buckets became inconsistent #6957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tokio/src/runtime/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 16 additions & 1 deletion tokio/tests/rt_unstable_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
Expand Down
Loading