Skip to content
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

feat: add range based exponential buckets in histogram #233

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ pub fn exponential_buckets(start: f64, factor: f64, length: u16) -> impl Iterato
.take(length.into())
}

/// Exponential bucket distribution within a range
/// /// Creates `length` buckets, where the lowest bucket is `min` and the highest bucket is `max`.
/// /// The final +Inf bucket is not counted and not included in the returned iterator.
/// /// The function panics if `length` is 0 or negative, or if `min` is 0 or negative.
kohlisid marked this conversation as resolved.
Show resolved Hide resolved
fn exponential_buckets_range(min: f64, max: f64, length: u16) -> impl Iterator<Item = f64> {
if length < 1 {
panic!("ExponentialBucketsRange length needs a positive length");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a NonZeroU16 instead of a u16. That said, that would be inconsistent with exponential_buckets. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess better to keep as u16 to keep consistent with exponential_buckets.
We can keep the additional check in the code for correctness.
What do you think?

if min <= 0.0 {
panic!("ExponentialBucketsRange min needs to be greater than 0");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't panic anywhere else in the crate. Reason being, that given that instrumentation is auxiliary only, folks likely don't want a panic. Would an empty Iterator work too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I have removed panics, but instead of an empty iterator I have added defaults with a warning.
Empty iterator might have have side-effects in histogram functionality.
Wdyt?


// We know max/min and highest bucket. Solve for growth_factor.
let growth_factor = (max / min).powf(1.0 / (length as f64 - 1.0));

iter::repeat(())
.enumerate()
.map(move |(i, _)| min * growth_factor.powf(i as f64))
.take(length.into())
}

/// Linear bucket distribution.
pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator<Item = f64> {
iter::repeat(())
Expand Down Expand Up @@ -166,4 +187,12 @@ mod tests {
linear_buckets(0.0, 1.0, 10).collect::<Vec<_>>()
);
}

#[test]
fn exponential_range() {
assert_eq!(
vec![1.0, 2.0, 4.0, 8.0, 16.0, 32.0],
exponential_buckets_range(1.0, 32.0, 6).collect::<Vec<_>>()
);
}
}
2 changes: 1 addition & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Registry {
/// let subsystem_b_counter_1: Counter = Counter::default();
/// let subsystem_b_counter_2: Counter = Counter::default();
///
/// let subsystem_a_registry = registry.sub_registry_with_prefix("subsystem_b");
/// let subsystem_b_registry = registry.sub_registry_with_prefix("subsystem_b");
kohlisid marked this conversation as resolved.
Show resolved Hide resolved
/// registry.register("counter_1", "", subsystem_b_counter_1.clone());
/// registry.register("counter_2", "", subsystem_b_counter_2.clone());
/// ```
Expand Down