Skip to content

Commit

Permalink
Record datastore latency aggregated by batcher flush
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed May 10, 2024
1 parent 19b5b2c commit 8e576e3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 11 additions & 6 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -271,11 +270,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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());
Expand Down
10 changes: 8 additions & 2 deletions limitador-server/src/prometheus_metrics.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,7 +42,7 @@ impl PrometheusMetrics {
prometheus_handle: Arc<PrometheusHandle>,
) -> Self {
describe_histogram!(
"counter_latency",
"datastore_latency",
"Latency to the underlying counter datastore"
);
describe_counter!("authorized_calls", "Authorized calls");
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ async fn update_counters<C: ConnectionLike>(

Ok(res)
}

#[tracing::instrument(skip_all)]
async fn flush_batcher_and_update_counters<C: ConnectionLike>(
mut redis_conn: C,
storage_is_alive: bool,
Expand Down

0 comments on commit 8e576e3

Please sign in to comment.