From 498c5079c2773fbb3eb33788ec30b5e63cd365bd Mon Sep 17 00:00:00 2001 From: dog Date: Sun, 23 Nov 2025 12:39:10 +0200 Subject: [PATCH] Refactor prometheus metrics collection to always export state metrics Split server metrics collection into two separate loops: - state_metrics (is_banned, is_paused) are now exported for all servers - activity_metrics (bytes_received, bytes_sent, etc.) are only exported when server_info is available This ensures that ban and pause states are always visible in metrics, even for servers without activity information. This improves observability for connection pool management and server health monitoring. --- src/prometheus.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/prometheus.rs b/src/prometheus.rs index 6c9a1be8..550d1c46 100644 --- a/src/prometheus.rs +++ b/src/prometheus.rs @@ -455,8 +455,25 @@ fn push_server_stats(lines: &mut Vec) { for shard in 0..pool.shards() { for server in 0..pool.servers(shard) { let address = pool.address(shard, server); + + let state_metrics = [ + ("is_banned", if pool.is_banned(address) { 1 } else { 0 }), + ("is_paused", if pool.paused() { 1 } else { 0 }), + ]; + + for (key, value) in state_metrics { + if let Some(prometheus_metric) = + PrometheusMetric::::from_server_info(address, key, value) + { + grouped_metrics + .entry(key.to_string()) + .or_default() + .push(prometheus_metric); + } + } + if let Some(server_info) = prom_stats.get(&address.name()) { - let metrics = [ + let activity_metrics = [ ("bytes_received", server_info.bytes_received), ("bytes_sent", server_info.bytes_sent), ("transaction_count", server_info.transaction_count), @@ -466,10 +483,9 @@ fn push_server_stats(lines: &mut Vec) { ("active_count", server_info.active_count), ("login_count", server_info.login_count), ("tested_count", server_info.tested_count), - ("is_banned", if pool.is_banned(address) { 1 } else { 0 }), - ("is_paused", if pool.paused() { 1 } else { 0 }), ]; - for (key, value) in metrics { + + for (key, value) in activity_metrics { if let Some(prometheus_metric) = PrometheusMetric::::from_server_info(address, key, value) {