diff --git a/aquatic_ws/src/config.rs b/aquatic_ws/src/config.rs index e222d89..6c46e33 100644 --- a/aquatic_ws/src/config.rs +++ b/aquatic_ws/src/config.rs @@ -154,6 +154,8 @@ pub struct MetricsConfig { pub run_prometheus_endpoint: bool, /// Address to run prometheus endpoint on pub prometheus_endpoint_address: SocketAddr, + /// Update metrics for torrent count this often, in seconds + pub torrent_count_update_interval: u64, } #[cfg(feature = "metrics")] @@ -162,6 +164,7 @@ impl Default for MetricsConfig { Self { run_prometheus_endpoint: false, prometheus_endpoint_address: SocketAddr::from(([0, 0, 0, 0], 9000)), + torrent_count_update_interval: 10, } } } diff --git a/aquatic_ws/src/workers/swarm.rs b/aquatic_ws/src/workers/swarm.rs index 861413e..362acc8 100644 --- a/aquatic_ws/src/workers/swarm.rs +++ b/aquatic_ws/src/workers/swarm.rs @@ -180,6 +180,27 @@ pub async fn run_swarm_worker( })() })); + // Periodically update torrent count metrics + #[cfg(feature = "metrics")] + TimerActionRepeat::repeat(enclose!((config, torrents) move || { + enclose!((config, torrents) move || async move { + let torrents = torrents.borrow_mut(); + + ::metrics::gauge!( + "aquatic_torrents", + torrents.ipv4.len() as f64, + "ip_version" => "4" + ); + ::metrics::gauge!( + "aquatic_torrents", + torrents.ipv6.len() as f64, + "ip_version" => "6" + ); + + Some(Duration::from_secs(config.metrics.torrent_count_update_interval)) + })() + })); + let mut handles = Vec::new(); for (_, receiver) in control_message_receivers.streams() {