|
| 1 | +//! Utility module to house implementation and declaration of MetricsArgs since it's being used in |
| 2 | +//! multiple places, it's just being referenced from this module. |
| 3 | +
|
| 4 | +use std::net::IpAddr; |
| 5 | + |
| 6 | +use clap::Parser; |
| 7 | + |
| 8 | +use crate::PrometheusServer; |
| 9 | + |
| 10 | +/// Configuration for Prometheus metrics. |
| 11 | +#[derive(Debug, Clone, Parser)] |
| 12 | +#[command(next_help_heading = "Metrics")] |
| 13 | +pub struct MetricsArgs { |
| 14 | + /// Controls whether Prometheus metrics are enabled. Disabled by default. |
| 15 | + #[arg( |
| 16 | + long = "metrics.enabled", |
| 17 | + global = true, |
| 18 | + default_value_t = false, |
| 19 | + env = "KONA_METRICS_ENABLED" |
| 20 | + )] |
| 21 | + pub enabled: bool, |
| 22 | + |
| 23 | + /// The port to serve Prometheus metrics on. |
| 24 | + #[arg(long = "metrics.port", global = true, default_value = "9090", env = "KONA_METRICS_PORT")] |
| 25 | + pub port: u16, |
| 26 | + |
| 27 | + /// The IP address to use for Prometheus metrics. |
| 28 | + #[arg( |
| 29 | + long = "metrics.addr", |
| 30 | + global = true, |
| 31 | + default_value = "0.0.0.0", |
| 32 | + env = "KONA_METRICS_ADDR" |
| 33 | + )] |
| 34 | + pub addr: IpAddr, |
| 35 | +} |
| 36 | + |
| 37 | +impl Default for MetricsArgs { |
| 38 | + fn default() -> Self { |
| 39 | + Self::parse_from::<[_; 0], &str>([]) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl MetricsArgs { |
| 44 | + /// Initialize the Prometheus metrics recorder. |
| 45 | + /// |
| 46 | + /// This function should be called at the beginning of the program. |
| 47 | + pub fn init(&self) -> Result<(), metrics_exporter_prometheus::BuildError> { |
| 48 | + if self.enabled { |
| 49 | + PrometheusServer::init(self.addr, self.port, None)?; |
| 50 | + } |
| 51 | + |
| 52 | + Ok(()) |
| 53 | + } |
| 54 | + |
| 55 | + /// Initialize the Prometheus metrics recorder and run a callback to initialize |
| 56 | + /// subsystem-specific metrics if metrics are enabled. |
| 57 | + /// |
| 58 | + /// This function should be called at the beginning of the program. |
| 59 | + pub fn init_with<F>(&self, f: F) -> Result<(), metrics_exporter_prometheus::BuildError> |
| 60 | + where |
| 61 | + F: FnOnce(), |
| 62 | + { |
| 63 | + if self.enabled { |
| 64 | + PrometheusServer::init(self.addr, self.port, None)?; |
| 65 | + f(); |
| 66 | + } |
| 67 | + |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use std::net::{IpAddr, Ipv4Addr}; |
| 75 | + |
| 76 | + use clap::Parser; |
| 77 | + |
| 78 | + use super::*; |
| 79 | + |
| 80 | + /// Helper struct to parse MetricsArgs within a test CLI structure. |
| 81 | + #[derive(Parser, Debug)] |
| 82 | + struct TestCli { |
| 83 | + #[command(flatten)] |
| 84 | + metrics: MetricsArgs, |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_default_metrics_args() { |
| 89 | + let cli = TestCli::parse_from(["test_app"]); |
| 90 | + assert!(!cli.metrics.enabled, "Default for metrics.enabled should be false."); |
| 91 | + assert_eq!(cli.metrics.port, 9090, "Default for metrics.port should be 9090."); |
| 92 | + assert_eq!( |
| 93 | + cli.metrics.addr, |
| 94 | + IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), |
| 95 | + "Default for metrics.addr should be 0.0.0.0." |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_metrics_args_from_cli() { |
| 101 | + let cli = TestCli::parse_from([ |
| 102 | + "test_app", |
| 103 | + "--metrics.enabled", |
| 104 | + "--metrics.port", |
| 105 | + "9999", |
| 106 | + "--metrics.addr", |
| 107 | + "127.0.0.1", |
| 108 | + ]); |
| 109 | + assert!(cli.metrics.enabled, "metrics.enabled should be true."); |
| 110 | + assert_eq!(cli.metrics.port, 9999, "metrics.port should be parsed from CLI."); |
| 111 | + assert_eq!( |
| 112 | + cli.metrics.addr, |
| 113 | + IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), |
| 114 | + "metrics.addr should be parsed from CLI." |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments