udp: only include prometheus fields in config if feature enabled

This commit is contained in:
Joakim Frostegård 2023-02-26 16:41:37 +01:00
parent 9bd4062e44
commit 3806faaff9
3 changed files with 41 additions and 9 deletions

View file

@ -1,6 +1,7 @@
use std::{net::SocketAddr, path::PathBuf}; use std::{net::SocketAddr, path::PathBuf};
use aquatic_common::{access_list::AccessListConfig, privileges::PrivilegeConfig}; use aquatic_common::{access_list::AccessListConfig, privileges::PrivilegeConfig};
use cfg_if::cfg_if;
use serde::Deserialize; use serde::Deserialize;
use aquatic_common::cli::LogLevel; use aquatic_common::cli::LogLevel;
@ -154,15 +155,25 @@ pub struct StatisticsConfig {
/// Path to save HTML file to /// Path to save HTML file to
pub html_file_path: PathBuf, pub html_file_path: PathBuf,
/// Run a prometheus endpoint /// Run a prometheus endpoint
#[cfg(feature = "prometheus")]
pub run_prometheus_endpoint: bool, pub run_prometheus_endpoint: bool,
/// Address to run prometheus endpoint on /// Address to run prometheus endpoint on
#[cfg(feature = "prometheus")]
pub prometheus_endpoint_address: SocketAddr, pub prometheus_endpoint_address: SocketAddr,
} }
impl StatisticsConfig { impl StatisticsConfig {
pub fn active(&self) -> bool { cfg_if! {
(self.interval != 0) if #[cfg(feature = "prometheus")] {
& (self.print_to_stdout | self.write_html_to_file | self.run_prometheus_endpoint) 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, print_to_stdout: false,
write_html_to_file: false, write_html_to_file: false,
html_file_path: "tmp/statistics.html".into(), html_file_path: "tmp/statistics.html".into(),
#[cfg(feature = "prometheus")]
run_prometheus_endpoint: false, run_prometheus_endpoint: false,
#[cfg(feature = "prometheus")]
prometheus_endpoint_address: SocketAddr::from(([0, 0, 0, 0], 9000)), prometheus_endpoint_address: SocketAddr::from(([0, 0, 0, 0], 9000)),
} }
} }

View file

@ -14,16 +14,18 @@ pub struct StatisticsCollector {
last_update: Instant, last_update: Instant,
pending_histograms: Vec<Histogram<u64>>, pending_histograms: Vec<Histogram<u64>>,
last_complete_histogram: PeerHistogramStatistics, last_complete_histogram: PeerHistogramStatistics,
#[cfg(feature = "prometheus")]
ip_version: String, ip_version: String,
} }
impl StatisticsCollector { impl StatisticsCollector {
pub fn new(shared: Arc<Statistics>, ip_version: String) -> Self { pub fn new(shared: Arc<Statistics>, #[cfg(feature = "prometheus")] ip_version: String) -> Self {
Self { Self {
shared, shared,
last_update: Instant::now(), last_update: Instant::now(),
pending_histograms: Vec::new(), pending_histograms: Vec::new(),
last_complete_histogram: Default::default(), last_complete_histogram: Default::default(),
#[cfg(feature = "prometheus")]
ip_version, 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 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_connect = Self::fetch_and_reset(&self.shared.responses_sent_connect);
let responses_sent_announce = Self::fetch_and_reset(&self.shared.responses_sent_announce); let responses_sent_announce = Self::fetch_and_reset(&self.shared.responses_sent_announce);

View file

@ -57,8 +57,16 @@ pub fn run_statistics_worker(
None None
}; };
let mut ipv4_collector = StatisticsCollector::new(shared_state.statistics_ipv4, "4".into()); let mut ipv4_collector = StatisticsCollector::new(
let mut ipv6_collector = StatisticsCollector::new(shared_state.statistics_ipv6, "6".into()); 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 { loop {
::std::thread::sleep(Duration::from_secs(config.statistics.interval)); ::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_ipv4 = ipv4_collector.collect_from_shared(
let statistics_ipv6 = ipv6_collector.collect_from_shared(&config); #[cfg(feature = "prometheus")]
&config,
);
let statistics_ipv6 = ipv6_collector.collect_from_shared(
#[cfg(feature = "prometheus")]
&config,
);
if config.statistics.print_to_stdout { if config.statistics.print_to_stdout {
println!("General:"); println!("General:");