From dc3cac6bd747faf908e70bd91c93c9c9f8e947da Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 9 Apr 2024 15:27:23 +0200 Subject: [PATCH] metrics/instance: Add async connection pools --- src/metrics/instance.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/metrics/instance.rs b/src/metrics/instance.rs index 0254f4aaa37..8582b1a6ecd 100644 --- a/src/metrics/instance.rs +++ b/src/metrics/instance.rs @@ -19,6 +19,7 @@ use crate::metrics::macros::metrics; use crate::{app::App, db::DieselPool}; +use deadpool_diesel::postgres::Pool; use prometheus::{ proto::MetricFamily, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, }; @@ -57,6 +58,11 @@ impl InstanceMetrics { self.refresh_pool_stats("follower", follower)?; } + self.refresh_async_pool_stats("async_primary", &app.deadpool_primary)?; + if let Some(follower) = &app.deadpool_replica { + self.refresh_async_pool_stats("async_follower", follower)?; + } + Ok(self.registry.gather()) } @@ -72,4 +78,17 @@ impl InstanceMetrics { Ok(()) } + + fn refresh_async_pool_stats(&self, name: &str, pool: &Pool) -> prometheus::Result<()> { + let status = pool.status(); + + self.database_idle_conns + .get_metric_with_label_values(&[name])? + .set(status.available as i64); + self.database_used_conns + .get_metric_with_label_values(&[name])? + .set((status.size - status.available) as i64); + + Ok(()) + } }