diff --git a/limitador-server/src/main.rs b/limitador-server/src/main.rs index 3f4ad865..e72f1a4e 100644 --- a/limitador-server/src/main.rs +++ b/limitador-server/src/main.rs @@ -14,7 +14,6 @@ use crate::config::{ use crate::envoy_rls::server::{run_envoy_rls_server, RateLimitHeaders}; use crate::http_api::server::run_http_server; use crate::metrics::MetricsLayer; -use ::metrics::histogram; use clap::{value_parser, Arg, ArgAction, Command}; use const_format::formatcp; use limitador::counter::Counter; @@ -271,11 +270,17 @@ async fn main() -> Result<(), Box> { tracing_subscriber::fmt::layer() }; - let metrics_layer = MetricsLayer::new().gather( - "should_rate_limit", - |timings| histogram!("counter_latency").record(Duration::from(timings).as_secs_f64()), - vec!["datastore"], - ); + let metrics_layer = MetricsLayer::new() + .gather( + "should_rate_limit", + PrometheusMetrics::record_datastore_latency, + vec!["datastore"], + ) + .gather( + "flush_batcher_and_update_counters", + PrometheusMetrics::record_datastore_latency, + vec!["datastore"], + ); if !config.tracing_endpoint.is_empty() { global::set_text_map_propagator(TraceContextPropagator::new()); diff --git a/limitador-server/src/prometheus_metrics.rs b/limitador-server/src/prometheus_metrics.rs index 9279a922..99086a37 100644 --- a/limitador-server/src/prometheus_metrics.rs +++ b/limitador-server/src/prometheus_metrics.rs @@ -1,7 +1,9 @@ -use metrics::{counter, describe_counter, describe_gauge, describe_histogram, gauge}; +use metrics::{counter, describe_counter, describe_gauge, describe_histogram, gauge, histogram}; use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; use std::sync::Arc; +use std::time::Duration; +use crate::metrics::Timings; use limitador::limit::Namespace; const NAMESPACE_LABEL: &str = "limitador_namespace"; @@ -91,6 +93,10 @@ impl PrometheusMetrics { pub fn gather_metrics(&self) -> String { self.prometheus_handle.render() } + + pub fn record_datastore_latency(timings: Timings) { + histogram!("datastore_latency").record(Duration::from(timings).as_secs_f64()) + } } #[cfg(test)] diff --git a/limitador/src/storage/redis/redis_cached.rs b/limitador/src/storage/redis/redis_cached.rs index 03f2b815..9faf96b4 100644 --- a/limitador/src/storage/redis/redis_cached.rs +++ b/limitador/src/storage/redis/redis_cached.rs @@ -335,7 +335,7 @@ async fn update_counters( Ok(res) } - +#[tracing::instrument(skip_all)] async fn flush_batcher_and_update_counters( mut redis_conn: C, storage_is_alive: bool,