Skip to content

Commit

Permalink
Adds prometheus builder support for specifying bucket count and durat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
cparratto committed Feb 28, 2024
1 parent 2085cb1 commit e869445
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
12 changes: 12 additions & 0 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::convert::TryFrom;
use std::future::Future;
#[cfg(feature = "http-listener")]
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::num::NonZeroU32;
#[cfg(any(feature = "http-listener", feature = "push-gateway"))]
use std::pin::Pin;
use std::sync::RwLock;
Expand Down Expand Up @@ -97,6 +98,7 @@ pub struct PrometheusBuilder {
allowed_addresses: Option<Vec<IpNet>>,
quantiles: Vec<Quantile>,
bucket_duration: Option<Duration>,
bucket_count: Option<NonZeroU32>,
buckets: Option<Vec<f64>>,
bucket_overrides: Option<HashMap<Matcher, Vec<f64>>>,
idle_timeout: Option<Duration>,
Expand All @@ -122,6 +124,7 @@ impl PrometheusBuilder {
allowed_addresses: None,
quantiles,
bucket_duration: None,
bucket_count: None,
buckets: None,
bucket_overrides: None,
idle_timeout: None,
Expand Down Expand Up @@ -257,6 +260,14 @@ impl PrometheusBuilder {
Ok(self)
}

/// Sets the default bucket count for rolling summaries
///
/// Count number buckets are created to store summary information
pub fn set_bucket_count(mut self, count: NonZeroU32) -> Self {
self.bucket_count = Some(count);
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 @@ -556,6 +567,7 @@ impl PrometheusBuilder {
self.quantiles,
self.bucket_duration,
self.buckets,
self.bucket_count,
self.bucket_overrides,
),
descriptions: RwLock::new(HashMap::new()),
Expand Down
28 changes: 20 additions & 8 deletions metrics-exporter-prometheus/src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::common::Matcher;

use metrics_util::{Histogram, Quantile, Summary};

const DEFAULT_SUMMARY_BUCKET_COUNT: u32 = 3;
const DEFAULT_SUMMARY_BUCKET_DURATION: u64 = 20;

/// Distribution type.
#[derive(Clone)]
pub enum Distribution {
Expand Down Expand Up @@ -36,13 +39,10 @@ impl Distribution {
/// Creates a summary distribution.
pub fn new_summary(
quantiles: Arc<Vec<Quantile>>,
bucket_duration: Option<Duration>,
bucket_duration: Duration,
bucket_count: NonZeroU32,
) -> 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)
Distribution::Summary(RollingSummary::new(bucket_count, bucket_duration), quantiles, 0.0)
}

/// Records the given `samples` in the current distribution.
Expand All @@ -67,6 +67,7 @@ pub struct DistributionBuilder {
quantiles: Arc<Vec<Quantile>>,
buckets: Option<Vec<f64>>,
bucket_duration: Option<Duration>,
bucket_count: Option<NonZeroU32>,
bucket_overrides: Option<Vec<(Matcher, Vec<f64>)>>,
}

Expand All @@ -76,12 +77,14 @@ impl DistributionBuilder {
quantiles: Vec<Quantile>,
bucket_duration: Option<Duration>,
buckets: Option<Vec<f64>>,
bucket_count: Option<NonZeroU32>,
bucket_overrides: Option<HashMap<Matcher, Vec<f64>>>,
) -> DistributionBuilder {
DistributionBuilder {
quantiles: Arc::new(quantiles),
bucket_duration,
buckets,
bucket_count,
bucket_overrides: bucket_overrides.map(|entries| {
let mut matchers = entries.into_iter().collect::<Vec<_>>();
matchers.sort_by(|a, b| a.0.cmp(&b.0));
Expand All @@ -104,7 +107,13 @@ impl DistributionBuilder {
return Distribution::new_histogram(buckets);
}

Distribution::new_summary(self.quantiles.clone(), self.bucket_duration)
let b_duration = self
.bucket_duration
.map_or(Duration::from_secs(DEFAULT_SUMMARY_BUCKET_DURATION), |d| d);
let b_count =
self.bucket_count.map_or(NonZeroU32::new(DEFAULT_SUMMARY_BUCKET_COUNT).unwrap(), |c| c);

Distribution::new_summary(self.quantiles.clone(), b_duration, b_count)
}

/// Returns the distribution type for the given metric key.
Expand Down Expand Up @@ -151,7 +160,10 @@ pub struct RollingSummary {

impl Default for RollingSummary {
fn default() -> Self {
RollingSummary::new(NonZeroU32::new(3).unwrap(), Duration::from_secs(20))
RollingSummary::new(
NonZeroU32::new(DEFAULT_SUMMARY_BUCKET_COUNT).unwrap(),
Duration::from_secs(20),
)
}
}

Expand Down

0 comments on commit e869445

Please sign in to comment.