diff --git a/agent/src/main.rs b/agent/src/main.rs index ea7568b87..0477c414d 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -34,6 +34,9 @@ lazy_static! { pub static ref DISCOVERY_RESPONSE_TIME_METRIC: HistogramVec = prometheus::register_histogram_vec!("akri_discovery_response_time", "Akri Discovery Response Time", &["configuration"]).unwrap(); } +/// Environment variable name for setting metrics port +const METRICS_PORT_LABEL: &str = "METRICS_PORT"; + /// This is the entry point for the Akri Agent. /// It must be built on unix systems, since the underlying libraries for the `DevicePluginService` unix socket connection are unix only. #[cfg(unix)] @@ -53,9 +56,14 @@ async fn main() -> Result<(), Box let mut tasks = Vec::new(); + let port = match std::env::var(METRICS_PORT_LABEL) { + Ok(p) => p.parse::()?, + Err(_) => 8080, + }; + // Start server for Prometheus metrics tasks.push(tokio::spawn(async move { - run_metrics_server().await.unwrap(); + run_metrics_server(port).await.unwrap(); })); let discovery_handler_map = Arc::new(Mutex::new(HashMap::new())); diff --git a/controller/src/main.rs b/controller/src/main.rs index 82d6d0c35..fafe5b15e 100644 --- a/controller/src/main.rs +++ b/controller/src/main.rs @@ -16,6 +16,9 @@ lazy_static! { pub static ref BROKER_POD_COUNT_METRIC: IntGaugeVec = prometheus::register_int_gauge_vec!("akri_broker_pod_count", "Akri Broker Pod Count", &["configuration", "node"]).unwrap(); } +/// Environment variable name for setting metrics port +const METRICS_PORT_LABEL: &str = "METRICS_PORT"; + /// This is the entry point for the controller. #[tokio::main] async fn main() -> Result<(), Box> { @@ -37,9 +40,14 @@ async fn main() -> Result<(), Box let instance_watch_synchronization = synchronization.clone(); let mut tasks = Vec::new(); + let port = match std::env::var(METRICS_PORT_LABEL) { + Ok(p) => p.parse::()?, + Err(_) => 8080, + }; + // Start server for prometheus metrics tasks.push(tokio::spawn(async move { - run_metrics_server().await.unwrap(); + run_metrics_server(port).await.unwrap(); })); // Handle existing instances diff --git a/samples/brokers/udev-video-broker/src/main.rs b/samples/brokers/udev-video-broker/src/main.rs index 407186b68..d69364558 100644 --- a/samples/brokers/udev-video-broker/src/main.rs +++ b/samples/brokers/udev-video-broker/src/main.rs @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box info!("{} Udev Broker logging started", API_NAMESPACE); tokio::spawn(async move { - run_metrics_server().await.unwrap(); + run_metrics_server(8080).await.unwrap(); }); let env_var_query = ActualEnvVarQuery {}; diff --git a/shared/src/akri/metrics.rs b/shared/src/akri/metrics.rs index 3b5687785..ae416c533 100644 --- a/shared/src/akri/metrics.rs +++ b/shared/src/akri/metrics.rs @@ -19,10 +19,11 @@ async fn metrics_handler() -> Result { } /// Serves prometheus metrics over a web service at /metrics -pub async fn run_metrics_server() -> Result<(), Box> -{ - info!("starting metrics server on port 8080 at /metrics"); +pub async fn run_metrics_server( + port: u16, +) -> Result<(), Box> { + info!("starting metrics server on port {} at /metrics", port); let metrics_route = warp::path!("metrics").and_then(metrics_handler); - warp::serve(metrics_route).run(([0, 0, 0, 0], 8080)).await; + warp::serve(metrics_route).run(([0, 0, 0, 0], port)).await; Ok(()) }