Skip to content

Commit

Permalink
Added datastore latency histogram to prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Dec 21, 2023
1 parent 33392ff commit 38d9f05
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions limitador/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cfg-if = "1"
prometheus = "0.13"
lazy_static = "1"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

# Optional dependencies
rocksdb = { version = "0.21.0", optional = true, features = ["multi-threaded-cf"] }
Expand All @@ -53,6 +54,7 @@ tokio = { version = "1", optional = true, features = [
infinispan = { version = "0.3", optional = true }
reqwest = { version = "0.11", optional = true }
base64 = { version = "0.21.0", optional = true }
log = "0.4.20"

[dev-dependencies]
serial_test = "2.0.0"
Expand Down
8 changes: 8 additions & 0 deletions limitador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ impl RateLimiter {
}
}

pub fn metrics(&self) -> &PrometheusMetrics {
&self.prometheus_metrics
}

pub fn get_namespaces(&self) -> HashSet<Namespace> {
self.storage.get_namespaces()
}
Expand Down Expand Up @@ -508,6 +512,10 @@ impl AsyncRateLimiter {
}
}

pub fn metrics(&self) -> &PrometheusMetrics {
&self.prometheus_metrics
}

pub fn get_namespaces(&self) -> HashSet<Namespace> {
self.storage.get_namespaces()
}
Expand Down
34 changes: 33 additions & 1 deletion limitador/src/prometheus_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::limit::Namespace;
use prometheus::{Encoder, IntCounterVec, IntGauge, Opts, Registry, TextEncoder};
use prometheus::{
Encoder, Histogram, HistogramOpts, IntCounterVec, IntGauge, Opts, Registry, TextEncoder,
};
use std::time::Duration;

const NAMESPACE_LABEL: &str = "limitador_namespace";
const LIMIT_NAME_LABEL: &str = "limit_name";
Expand All @@ -22,15 +25,26 @@ lazy_static! {
name: "limitador_up".into(),
description: "Limitador is running".into(),
};
static ref DATASTORE_LATENCY: Metric = Metric {
name: "counter_latency".into(),
description: "Latency to the underlying counter datastore".into(),
};
}

pub struct PrometheusMetrics {
registry: Registry,
authorized_calls: IntCounterVec,
limited_calls: IntCounterVec,
counter_latency: Histogram,
use_limit_name_label: bool,
}

impl Default for PrometheusMetrics {
fn default() -> Self {
Self::new()
}
}

impl PrometheusMetrics {
pub fn new() -> Self {
Self::new_with_options(false)
Expand Down Expand Up @@ -65,6 +79,10 @@ impl PrometheusMetrics {
self.limited_calls.with_label_values(&labels).inc();
}

pub fn counter_access(&self, duration: Duration) {
self.counter_latency.observe(duration.as_secs_f64());
}

pub fn gather_metrics(&self) -> String {
let mut buffer = Vec::new();

Expand All @@ -79,6 +97,7 @@ impl PrometheusMetrics {
let authorized_calls_counter = Self::authorized_calls_counter();
let limited_calls_counter = Self::limited_calls_counter(use_limit_name_label);
let limitador_up_gauge = Self::limitador_up_gauge();
let counter_latency = Self::counter_latency();

let registry = Registry::new();

Expand All @@ -94,12 +113,17 @@ impl PrometheusMetrics {
.register(Box::new(limitador_up_gauge.clone()))
.unwrap();

registry
.register(Box::new(counter_latency.clone()))
.unwrap();

limitador_up_gauge.set(1);

Self {
registry,
authorized_calls: authorized_calls_counter,
limited_calls: limited_calls_counter,
counter_latency,
use_limit_name_label,
}
}
Expand Down Expand Up @@ -129,6 +153,14 @@ impl PrometheusMetrics {
fn limitador_up_gauge() -> IntGauge {
IntGauge::new(&LIMITADOR_UP.name, &LIMITADOR_UP.description).unwrap()
}

fn counter_latency() -> Histogram {
Histogram::with_opts(HistogramOpts::new(
&DATASTORE_LATENCY.name,
&DATASTORE_LATENCY.description,
))
.unwrap()
}
}

#[cfg(test)]
Expand Down

0 comments on commit 38d9f05

Please sign in to comment.