diff --git a/aquatic_udp/src/config.rs b/aquatic_udp/src/config.rs index ccb2e69..cf310d9 100644 --- a/aquatic_udp/src/config.rs +++ b/aquatic_udp/src/config.rs @@ -1,6 +1,7 @@ use std::{net::SocketAddr, path::PathBuf}; use aquatic_common::{access_list::AccessListConfig, privileges::PrivilegeConfig}; +use cfg_if::cfg_if; use serde::Deserialize; use aquatic_common::cli::LogLevel; @@ -154,15 +155,25 @@ pub struct StatisticsConfig { /// Path to save HTML file to pub html_file_path: PathBuf, /// Run a prometheus endpoint + #[cfg(feature = "prometheus")] pub run_prometheus_endpoint: bool, /// Address to run prometheus endpoint on + #[cfg(feature = "prometheus")] pub prometheus_endpoint_address: SocketAddr, } impl StatisticsConfig { - pub fn active(&self) -> bool { - (self.interval != 0) - & (self.print_to_stdout | self.write_html_to_file | self.run_prometheus_endpoint) + cfg_if! { + if #[cfg(feature = "prometheus")] { + pub fn active(&self) -> bool { + (self.interval != 0) & + (self.print_to_stdout | self.write_html_to_file | self.run_prometheus_endpoint) + } + } else { + pub fn active(&self) -> bool { + (self.interval != 0) & (self.print_to_stdout | self.write_html_to_file) + } + } } } @@ -174,7 +185,9 @@ impl Default for StatisticsConfig { print_to_stdout: false, write_html_to_file: false, html_file_path: "tmp/statistics.html".into(), + #[cfg(feature = "prometheus")] run_prometheus_endpoint: false, + #[cfg(feature = "prometheus")] prometheus_endpoint_address: SocketAddr::from(([0, 0, 0, 0], 9000)), } } diff --git a/aquatic_udp/src/workers/statistics/collector.rs b/aquatic_udp/src/workers/statistics/collector.rs index 68c7300..3e3ed97 100644 --- a/aquatic_udp/src/workers/statistics/collector.rs +++ b/aquatic_udp/src/workers/statistics/collector.rs @@ -14,16 +14,18 @@ pub struct StatisticsCollector { last_update: Instant, pending_histograms: Vec>, last_complete_histogram: PeerHistogramStatistics, + #[cfg(feature = "prometheus")] ip_version: String, } impl StatisticsCollector { - pub fn new(shared: Arc, ip_version: String) -> Self { + pub fn new(shared: Arc, #[cfg(feature = "prometheus")] ip_version: String) -> Self { Self { shared, last_update: Instant::now(), pending_histograms: Vec::new(), last_complete_histogram: Default::default(), + #[cfg(feature = "prometheus")] ip_version, } } @@ -37,7 +39,10 @@ impl StatisticsCollector { } } - pub fn collect_from_shared(&mut self, config: &Config) -> CollectedStatistics { + pub fn collect_from_shared( + &mut self, + #[cfg(feature = "prometheus")] config: &Config, + ) -> CollectedStatistics { let requests_received = Self::fetch_and_reset(&self.shared.requests_received); let responses_sent_connect = Self::fetch_and_reset(&self.shared.responses_sent_connect); let responses_sent_announce = Self::fetch_and_reset(&self.shared.responses_sent_announce); diff --git a/aquatic_udp/src/workers/statistics/mod.rs b/aquatic_udp/src/workers/statistics/mod.rs index f54052e..f54f08b 100644 --- a/aquatic_udp/src/workers/statistics/mod.rs +++ b/aquatic_udp/src/workers/statistics/mod.rs @@ -57,8 +57,16 @@ pub fn run_statistics_worker( None }; - let mut ipv4_collector = StatisticsCollector::new(shared_state.statistics_ipv4, "4".into()); - let mut ipv6_collector = StatisticsCollector::new(shared_state.statistics_ipv6, "6".into()); + let mut ipv4_collector = StatisticsCollector::new( + shared_state.statistics_ipv4, + #[cfg(feature = "prometheus")] + "4".into(), + ); + let mut ipv6_collector = StatisticsCollector::new( + shared_state.statistics_ipv6, + #[cfg(feature = "prometheus")] + "6".into(), + ); loop { ::std::thread::sleep(Duration::from_secs(config.statistics.interval)); @@ -70,8 +78,14 @@ pub fn run_statistics_worker( } } - let statistics_ipv4 = ipv4_collector.collect_from_shared(&config); - let statistics_ipv6 = ipv6_collector.collect_from_shared(&config); + let statistics_ipv4 = ipv4_collector.collect_from_shared( + #[cfg(feature = "prometheus")] + &config, + ); + let statistics_ipv6 = ipv6_collector.collect_from_shared( + #[cfg(feature = "prometheus")] + &config, + ); if config.statistics.print_to_stdout { println!("General:");