From e24b3ac39b9c1aa7685db6d61b5f11a6c615f65d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 31 Jul 2023 22:21:29 +0200 Subject: [PATCH] feat(relay): serve metrics conditionally based on passed socket addr (#1833) While developing IPv6 support, I ran into a limitations with how I designed the prometheus metrics integration. Currently, we just use the IPv4 listen socket to server the metrics. That however no longer works with IPv6 support because the relay may now operate in IPv6 only mode for example. To circumvent this, we introduce a dedicated configuration option where the user needs to pass the socket addr for the metrics endpoint. If omitted, the metrics won't be served at all. --- rust/relay/src/main.rs | 10 +++++++++- rust/relay/src/metrics.rs | 6 ++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/rust/relay/src/main.rs b/rust/relay/src/main.rs index fcfaa39b8..effc5c1d3 100644 --- a/rust/relay/src/main.rs +++ b/rust/relay/src/main.rs @@ -32,6 +32,11 @@ struct Args { /// Must not be a wildcard-address. #[arg(long, env)] listen_ip4_addr: Ipv4Addr, + /// The address of the local interface where we should serve the prometheus metrics. + /// + /// The metrics will be available at `http:///metrics`. + #[arg(long, env)] + metrics_addr: Option, /// The websocket URL of the portal server to connect to. #[arg(long, env, default_value = "wss://api.firezone.dev")] portal_ws_url: Url, @@ -134,7 +139,10 @@ async fn main() -> Result<()> { let mut eventloop = Eventloop::new(server, channel, args.listen_ip4_addr, &mut metric_registry).await?; - tokio::spawn(relay::metrics::serve(args.listen_ip4_addr, metric_registry)); + + if let Some(metrics_addr) = args.metrics_addr { + tokio::spawn(relay::metrics::serve(metrics_addr, metric_registry)); + } tracing::info!("Listening for incoming traffic on UDP port 3478"); diff --git a/rust/relay/src/metrics.rs b/rust/relay/src/metrics.rs index e9508776a..dbe96f6b3 100644 --- a/rust/relay/src/metrics.rs +++ b/rust/relay/src/metrics.rs @@ -5,15 +5,13 @@ use axum::response::IntoResponse; use axum::routing::get; use axum::{Router, Server}; use prometheus_client::registry::Registry; -use std::net::{IpAddr, SocketAddr}; +use std::net::SocketAddr; use std::sync::Arc; const CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0"; -const PORT: u16 = 8080; -pub async fn serve(addr: impl Into, registry: Registry) -> Result<()> { +pub async fn serve(addr: impl Into, registry: Registry) -> Result<()> { let addr = addr.into(); - let addr = SocketAddr::new(addr, PORT); let service = Router::new() .route("/metrics", get(metrics))