upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-12-10 22:29:39 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-12-10 22:29:39 +0000
commit99bd932d7b11a5fe50e55a3e3c87113c17a82969 (patch)
tree921f8059a03f18cc92a912b45c63a3738cfb6b22 /src
parent82cc74ade1524edc096608795b4e13c3cb19c5eb (diff)
feat: add metrics field to SyncManager (Phase 2)
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/sync/mod.rs21
2 files changed, 23 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 31c7b63..c6cf8c5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -53,6 +53,7 @@ async fn main() -> Result<()> {
53 53
54 // Start SyncManager for proactive sync (Phase 2: multi-relay support, Phase 3: health tracking) 54 // Start SyncManager for proactive sync (Phase 2: multi-relay support, Phase 3: health tracking)
55 // Even without bootstrap relay, SyncManager discovers relays from stored announcements 55 // Even without bootstrap relay, SyncManager discovers relays from stored announcements
56 // TODO(Phase 3): Pass registry reference for sync metrics
56 let sync_manager = SyncManager::new( 57 let sync_manager = SyncManager::new(
57 config.sync_bootstrap_relay_url.clone(), 58 config.sync_bootstrap_relay_url.clone(),
58 config.domain.clone(), 59 config.domain.clone(),
@@ -60,6 +61,7 @@ async fn main() -> Result<()> {
60 relay_with_db.write_policy.clone(), 61 relay_with_db.write_policy.clone(),
61 relay_with_db.relay.clone(), 62 relay_with_db.relay.clone(),
62 &config, 63 &config,
64 None, // Registry will be passed in Phase 3
63 ); 65 );
64 66
65 if config.sync_bootstrap_relay_url.is_some() { 67 if config.sync_bootstrap_relay_url.is_some() {
diff --git a/src/sync/mod.rs b/src/sync/mod.rs
index dd0479c..a58406b 100644
--- a/src/sync/mod.rs
+++ b/src/sync/mod.rs
@@ -39,6 +39,7 @@ use std::sync::Arc;
39use std::time::Duration; 39use std::time::Duration;
40 40
41use nostr_sdk::prelude::*; 41use nostr_sdk::prelude::*;
42use prometheus::Registry;
42use tokio::sync::{broadcast, Mutex, RwLock}; 43use tokio::sync::{broadcast, Mutex, RwLock};
43 44
44use crate::config::Config; 45use crate::config::Config;
@@ -333,6 +334,8 @@ pub struct SyncManager {
333 connect_tx: Option<tokio::sync::mpsc::Sender<ConnectNotification>>, 334 connect_tx: Option<tokio::sync::mpsc::Sender<ConnectNotification>>,
334 /// Channel for broadcasting shutdown signal to all background tasks 335 /// Channel for broadcasting shutdown signal to all background tasks
335 shutdown_tx: Option<broadcast::Sender<()>>, 336 shutdown_tx: Option<broadcast::Sender<()>>,
337 /// Prometheus metrics for sync operations (None if metrics disabled)
338 metrics: Option<SyncMetrics>,
336} 339}
337 340
338impl SyncManager { 341impl SyncManager {
@@ -345,6 +348,7 @@ impl SyncManager {
345 /// * `write_policy` - Policy for validating events before storage 348 /// * `write_policy` - Policy for validating events before storage
346 /// * `local_relay` - Local relay for submitting synced events (enables WebSocket broadcast) 349 /// * `local_relay` - Local relay for submitting synced events (enables WebSocket broadcast)
347 /// * `config` - Configuration for sync settings 350 /// * `config` - Configuration for sync settings
351 /// * `registry` - Optional Prometheus registry for metrics (metrics only created if config.metrics_enabled is true)
348 pub fn new( 352 pub fn new(
349 bootstrap_relay_url: Option<String>, 353 bootstrap_relay_url: Option<String>,
350 service_domain: String, 354 service_domain: String,
@@ -352,7 +356,23 @@ impl SyncManager {
352 write_policy: Nip34WritePolicy, 356 write_policy: Nip34WritePolicy,
353 local_relay: LocalRelay, 357 local_relay: LocalRelay,
354 config: &Config, 358 config: &Config,
359 registry: Option<&Registry>,
355 ) -> Self { 360 ) -> Self {
361 // Create metrics only if metrics are enabled AND a registry is provided
362 let metrics = if config.metrics_enabled {
363 registry.and_then(|r| {
364 match SyncMetrics::register(r) {
365 Ok(m) => Some(m),
366 Err(e) => {
367 tracing::warn!("Failed to register sync metrics: {}", e);
368 None
369 }
370 }
371 })
372 } else {
373 None
374 };
375
356 Self { 376 Self {
357 bootstrap_relay_url, 377 bootstrap_relay_url,
358 service_domain, 378 service_domain,
@@ -370,6 +390,7 @@ impl SyncManager {
370 eose_tx: None, 390 eose_tx: None,
371 connect_tx: None, 391 connect_tx: None,
372 shutdown_tx: None, 392 shutdown_tx: None,
393 metrics,
373 } 394 }
374 } 395 }
375 396