Skip to content

Commit

Permalink
Adds bucket duration as configurable element for setting rolling summ…
Browse files Browse the repository at this point in the history
…aries.

cleanup
  • Loading branch information
cparratto committed Feb 7, 2024
1 parent f50f914 commit 739d6ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
19 changes: 19 additions & 0 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct PrometheusBuilder {
#[cfg(feature = "http-listener")]
allowed_addresses: Option<Vec<IpNet>>,
quantiles: Vec<Quantile>,
bucket_duration: Option<Duration>,
buckets: Option<Vec<f64>>,
bucket_overrides: Option<HashMap<Matcher, Vec<f64>>>,
idle_timeout: Option<Duration>,
Expand All @@ -120,6 +121,7 @@ impl PrometheusBuilder {
#[cfg(feature = "http-listener")]
allowed_addresses: None,
quantiles,
bucket_duration: None,
buckets: None,
bucket_overrides: None,
idle_timeout: None,
Expand Down Expand Up @@ -239,6 +241,22 @@ impl PrometheusBuilder {
Ok(self)
}

/// Sets the default bucket duration for rolling summaries
///
/// Buckets will be cleared after this interval expires
///
/// ## Errors
///
/// If `value` less than 1 error will be thrown
pub fn set_bucket_duration(mut self, value: Duration) -> Result<Self, BuildError> {
if value.is_zero() {
return Err(BuildError::ZeroBucketDuration);
}

self.bucket_duration = Some(value);
Ok(self)
}

/// Sets the buckets to use when rendering histograms.
///
/// Buckets values represent the higher bound of each buckets. If buckets are set, then all
Expand Down Expand Up @@ -536,6 +554,7 @@ impl PrometheusBuilder {
distributions: RwLock::new(HashMap::new()),
distribution_builder: DistributionBuilder::new(
self.quantiles,
self.bucket_duration,
self.buckets,
self.bucket_overrides,
),
Expand Down
4 changes: 4 additions & 0 deletions metrics-exporter-prometheus/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub enum BuildError {
/// Bucket bounds or quantiles were empty.
#[error("bucket bounds/quantiles cannot be empty")]
EmptyBucketsOrQuantiles,

/// Bucket duration cannot be zero
#[error("bucket durations cannot be set to zero")]
ZeroBucketDuration,
}

pub struct Snapshot {
Expand Down
15 changes: 12 additions & 3 deletions metrics-exporter-prometheus/src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ impl Distribution {
}

/// Creates a summary distribution.
pub fn new_summary(quantiles: Arc<Vec<Quantile>>) -> Distribution {
let summary = RollingSummary::default();
pub fn new_summary(
quantiles: Arc<Vec<Quantile>>,
bucket_duration: Option<Duration>,
) -> Distribution {
let summary = bucket_duration.map_or(
RollingSummary::new(NonZeroU32::new(3).unwrap(), Duration::from_secs(20)),
|duration| RollingSummary::new(NonZeroU32::new(3).unwrap(), duration),
);
Distribution::Summary(summary, quantiles, 0.0)
}

Expand All @@ -60,18 +66,21 @@ impl Distribution {
pub struct DistributionBuilder {
quantiles: Arc<Vec<Quantile>>,
buckets: Option<Vec<f64>>,
bucket_duration: Option<Duration>,
bucket_overrides: Option<Vec<(Matcher, Vec<f64>)>>,
}

impl DistributionBuilder {
/// Creates a new instance of `DistributionBuilder`.
pub fn new(
quantiles: Vec<Quantile>,
bucket_duration: Option<Duration>,
buckets: Option<Vec<f64>>,
bucket_overrides: Option<HashMap<Matcher, Vec<f64>>>,
) -> DistributionBuilder {
DistributionBuilder {
quantiles: Arc::new(quantiles),
bucket_duration,
buckets,
bucket_overrides: bucket_overrides.map(|entries| {
let mut matchers = entries.into_iter().collect::<Vec<_>>();
Expand All @@ -95,7 +104,7 @@ impl DistributionBuilder {
return Distribution::new_histogram(buckets);
}

Distribution::new_summary(self.quantiles.clone())
Distribution::new_summary(self.quantiles.clone(), self.bucket_duration)
}

/// Returns the distribution type for the given metric key.
Expand Down

0 comments on commit 739d6ba

Please sign in to comment.