From 9d1545b7b13bc6cd3b0a075865918ceaad9e07ff Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 11 Dec 2025 13:19:08 +0000 Subject: fix: sync metrics aggregate relay counts --- src/sync/mod.rs | 12 +++++++++++- tests/sync/metrics.rs | 7 ------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/sync/mod.rs b/src/sync/mod.rs index b6cd00a..c78c0e1 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -1208,13 +1208,23 @@ impl SyncManager { } // Mark as connected in relay sync index - { + // Track whether this is a new relay for metrics + let is_new_relay = { let mut index = relay_sync_index.write().await; + let is_new = !index.contains_key(&relay_url); let state = index.entry(relay_url.clone()).or_default(); state.connection_status = ConnectionStatus::Connected; state.is_bootstrap = is_bootstrap; state.last_connected = Some(Timestamp::now()); state.disconnected_at = None; + is_new + }; + + // Increment tracked count for new relays + if is_new_relay { + if let Some(ref metrics) = self.metrics { + metrics.inc_tracked_count(); + } } // Store connection in HashMap BEFORE sending notification diff --git a/tests/sync/metrics.rs b/tests/sync/metrics.rs index 98ce4f3..26d379d 100644 --- a/tests/sync/metrics.rs +++ b/tests/sync/metrics.rs @@ -374,8 +374,6 @@ async fn test_startup_sync_event_count() { /// This test validates that when sync cannot connect to a source relay, /// the connection_attempts_total counter with result="failure" increases. /// -/// NOTE: This test may fail until sync metrics recording is fully wired up. -/// The test documents the expected behavior. #[tokio::test] async fn test_connection_failure_increments_counter() { let mut harness = MetricsTestHarness::with_sources(0).await; // No sources @@ -489,8 +487,6 @@ async fn test_relay_connected_status() { /// This test validates that `ngit_sync_relay_status` gauge transitions from /// healthy (1) to degraded (2) or dead (3) when a relay cannot be connected to. /// -/// NOTE: This test may fail until sync metrics recording is fully wired up. -/// The test documents the expected behavior. #[tokio::test] async fn test_health_state_degrades_on_failure() { use crate::common::sync_helpers::MetricsTestHarness; @@ -533,10 +529,7 @@ async fn test_health_state_degrades_on_failure() { /// Note: Current implementation may only support one sync source, so this tests /// with one source, verifying tracked=1 and connected=1, then connected=0 after stopping. /// -/// NOTE: This test may fail until sync metrics recording is fully wired up. -/// The test documents the expected behavior. #[tokio::test] -#[ignore] // Enable when relay tracking metrics are wired up async fn test_multi_source_aggregate_counts() { use crate::common::sync_helpers::MetricsTestHarness; -- cgit v1.2.3