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 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parking_lot = "0.12"
prometheus-client-derive-encode = { version = "0.4.1", path = "derive-encode" }
prost = { version = "0.12.0", optional = true }
prost-types = { version = "0.12.0", optional = true }
log = "0.4.22"

[dev-dependencies]
async-std = { version = "1", features = ["attributes"] }
Expand Down
36 changes: 36 additions & 0 deletions src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::encoding::{EncodeMetric, MetricEncoder, NoLabelSet};

use super::{MetricType, TypedMetric};
use log::warn;
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use std::iter::{self, once};
use std::sync::Arc;
Expand Down Expand Up @@ -122,6 +123,33 @@ 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 function defaults to `length` = 1 if `length` is 0 or negative,
/// and defaults to `min` = 1.0 if `min` is 0 or negative.
pub fn exponential_buckets_range(min: f64, max: f64, length: u16) -> impl Iterator<Item = f64> {
let mut len_observed = length;
let mut min_bucket = min;
if length < 1 {
warn!("exponential_buckets_range length needs a positive length, defaulting to 1");
len_observed = 1;
}
if min <= 0.0 {
warn!("exponential_buckets_range min needs to be greater than 0, defaulting to 1.0");
min_bucket = 1.0;
}

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

iter::repeat(())
.enumerate()
.map(move |(i, _)| min_bucket * growth_factor.powf(i as f64))
.take(len_observed.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 +194,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<_>>()
);
}
}