From b68909946f8518df2890dd82e2f3ad374943c832 Mon Sep 17 00:00:00 2001 From: lcian <17258265+lcian@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:36:25 +0100 Subject: [PATCH] feat(metrics): Make the exporter flush interval configurable Add a flush_interval setting (OS__METRICS__FLUSH_INTERVAL) to MetricsConfig so gauge snapshots can be exported more often than the DogStatsD default of 10 seconds. The default stays at 10s, and a zero interval is rejected at init. --- Cargo.lock | 1 + objectstore-metrics/Cargo.toml | 1 + objectstore-metrics/src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e88dc5cb..b20b3a8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2962,6 +2962,7 @@ dependencies = [ name = "objectstore-metrics" version = "0.1.0" dependencies = [ + "humantime-serde", "metrics", "metrics-exporter-dogstatsd", "objectstore-log", diff --git a/objectstore-metrics/Cargo.toml b/objectstore-metrics/Cargo.toml index 0859bbc9..f7355b8e 100644 --- a/objectstore-metrics/Cargo.toml +++ b/objectstore-metrics/Cargo.toml @@ -10,6 +10,7 @@ edition = "2024" publish = false [dependencies] +humantime-serde = { workspace = true } metrics = { workspace = true } metrics-exporter-dogstatsd = { workspace = true } objectstore-log = { workspace = true } diff --git a/objectstore-metrics/src/lib.rs b/objectstore-metrics/src/lib.rs index f1e022a7..eecf4d1f 100644 --- a/objectstore-metrics/src/lib.rs +++ b/objectstore-metrics/src/lib.rs @@ -200,6 +200,7 @@ pub enum Error { /// - `OS__METRICS__ADDR` — StatsD address (e.g. `127.0.0.1:8125` or `unixgram:///tmp/statsd.sock`) /// - `OS__METRICS__PREFIX` — global metric name prefix /// - `OS__METRICS__BUFFER_SIZE` — maximum payload length in bytes +/// - `OS__METRICS__FLUSH_INTERVAL` — exporter flush interval (e.g. `1s`) /// - `OS__METRICS__TAGS__KEY=value` — per-key global tags #[derive(Clone, Debug, Deserialize, Serialize)] pub struct MetricsConfig { @@ -250,6 +251,13 @@ pub struct MetricsConfig { /// `OS__METRICS__BUFFER_SIZE` pub buffer_size: Option, + /// Interval between exporter flushes. Defaults to ten seconds. + /// + /// Shorter intervals retain more frequent gauge snapshots but increase export overhead. + /// Must be greater than zero. Set with `OS__METRICS__FLUSH_INTERVAL`. + #[serde(default = "default_flush_interval", with = "humantime_serde")] + pub flush_interval: std::time::Duration, + /// Global tags applied to all metrics. /// /// Key-value pairs attached to every emitted metric. Useful for identifying @@ -281,12 +289,17 @@ fn default_prefix() -> String { "objectstore".to_owned() } +fn default_flush_interval() -> std::time::Duration { + std::time::Duration::from_secs(10) +} + impl Default for MetricsConfig { fn default() -> Self { Self { addr: None, prefix: "objectstore".to_owned(), buffer_size: None, + flush_interval: default_flush_interval(), tags: BTreeMap::new(), } } @@ -300,6 +313,14 @@ pub fn init(config: &MetricsConfig) -> Result<(), Error> { let Some(ref addr) = config.addr else { return Ok(()); }; + if config.flush_interval.is_zero() { + return Err( + metrics_exporter_dogstatsd::BuildError::InvalidConfiguration { + reason: "metrics flush interval must be greater than zero".to_owned(), + } + .into(), + ); + } objectstore_log::info!("reporting metrics to statsd at {addr}"); @@ -313,6 +334,7 @@ pub fn init(config: &MetricsConfig) -> Result<(), Error> { .with_remote_address(addr)? .with_telemetry(true) .with_aggregation_mode(AggregationMode::Aggressive) + .with_flush_interval(config.flush_interval) .send_histograms_as_distributions(true) .with_histogram_sampling(true) .set_global_prefix(&config.prefix)