From f90e7201cbc130961f17e585e8871eda7ce309b1 Mon Sep 17 00:00:00 2001 From: Mateusz Charytoniuk Date: Thu, 21 Nov 2024 12:22:29 +0100 Subject: [PATCH] chore: put statsd and ratatui dashboard behind feature flags --- Cargo.toml | 10 +++++++--- src/balancer/mod.rs | 4 +++- src/balancer/upstream_peer_pool.rs | 2 ++ src/cmd/balancer.rs | 16 +++++++++++----- src/errors/app_error.rs | 1 + src/main.rs | 24 ++++++++++++++++++------ 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd45f65..646ec01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,16 @@ askama = "0.12.1" askama_actix = "0.14.0" async-trait = "0.1.83" bytes = "1.8.0" -cadence = "1.5.0" +cadence = { version = "1.5.0", optional = true } clap = { version = "4.5.20", features = ["derive"] } -crossterm = "0.28.1" +crossterm = { version = "0.28.1", optional = true } env_logger = "0.11.5" futures = "0.3.31" futures-util = { version = "0.3.31", features = ["tokio-io"] } log = "0.4.22" mime_guess = "2.0.5" pingora = { version = "0.4.0", features = ["proxy"] } -ratatui = "0.29.0" +ratatui = { version = "0.29.0", optional = true } reqwest = { version = "0.12.9", features = ["json", "stream"] } rust-embed = "8.5.0" serde = { version = "1.0.215", features = ["derive"] } @@ -30,3 +30,7 @@ tokio-stream = { version = "0.1.16", features = ["sync"] } url = { version = "2.5.3", features = ["serde"] } uuid = { version = "1.11.0", features = ["serde", "v4"] } +[features] +default = ["ratatui_dashboard", "statsd_reporter"] +ratatui_dashboard = ["dep:crossterm", "dep:ratatui"] +statsd_reporter = ["dep:cadence"] diff --git a/src/balancer/mod.rs b/src/balancer/mod.rs index a1d4604..5284684 100644 --- a/src/balancer/mod.rs +++ b/src/balancer/mod.rs @@ -1,7 +1,9 @@ pub mod http_route; pub mod management_service; pub mod proxy_service; -pub mod statsd_service; pub mod status_update; pub mod upstream_peer; pub mod upstream_peer_pool; + +#[cfg(feature = "statsd_reporter")] +pub mod statsd_service; diff --git a/src/balancer/upstream_peer_pool.rs b/src/balancer/upstream_peer_pool.rs index cc0a262..6376996 100644 --- a/src/balancer/upstream_peer_pool.rs +++ b/src/balancer/upstream_peer_pool.rs @@ -100,6 +100,7 @@ impl UpstreamPeerPool { }) } + #[cfg(feature = "statsd_reporter")] // returns (slots_idle, slots_processing) tuple pub fn total_slots(&self) -> Result<(usize, usize)> { self.with_agents_read(|agents| { @@ -127,6 +128,7 @@ impl UpstreamPeerPool { }) } + #[cfg(feature = "statsd_reporter")] #[inline] fn with_agents_read(&self, cb: TCallback) -> Result where diff --git a/src/cmd/balancer.rs b/src/cmd/balancer.rs index ef9315c..b7e7c2e 100644 --- a/src/cmd/balancer.rs +++ b/src/cmd/balancer.rs @@ -2,23 +2,28 @@ use pingora::{ proxy::http_proxy_service, server::{configuration::Opt, Server}, }; -use std::{net::SocketAddr, sync::Arc, time::Duration}; +use std::{net::SocketAddr, sync::Arc}; + +#[cfg(feature = "statsd_reporter")] +use std::time::Duration; use crate::balancer::management_service::ManagementService; use crate::balancer::proxy_service::ProxyService; -use crate::balancer::statsd_service::StatsdService; use crate::balancer::upstream_peer_pool::UpstreamPeerPool; use crate::errors::result::Result; +#[cfg(feature = "statsd_reporter")] +use crate::balancer::statsd_service::StatsdService; + pub fn handle( management_addr: &SocketAddr, management_dashboard_enable: bool, reverseproxy_addr: &SocketAddr, rewrite_host_header: bool, slots_endpoint_enable: bool, - statsd_addr: Option, - statsd_prefix: String, - statsd_reporting_interval: Duration, + #[cfg(feature = "statsd_reporter")] statsd_addr: Option, + #[cfg(feature = "statsd_reporter")] statsd_prefix: String, + #[cfg(feature = "statsd_reporter")] statsd_reporting_interval: Duration, ) -> Result<()> { let mut pingora_server = Server::new(Opt { upgrade: false, @@ -50,6 +55,7 @@ pub fn handle( upstream_peer_pool.clone(), )); + #[cfg(feature = "statsd_reporter")] if let Some(statsd_addr) = statsd_addr { let statsd_service = StatsdService::new( statsd_addr, diff --git a/src/errors/app_error.rs b/src/errors/app_error.rs index 0238fac..de39779 100644 --- a/src/errors/app_error.rs +++ b/src/errors/app_error.rs @@ -6,6 +6,7 @@ pub enum AppError { #[error("Unable to communicate with actor: {0}")] ActixActorMailboxError(#[from] actix::MailboxError), + #[cfg(feature = "statsd_reporter")] #[error("Cadence error: {0}")] CadenceMetrixError(#[from] cadence::MetricError), diff --git a/src/main.rs b/src/main.rs index 92d6c12..c27f2e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,23 +103,28 @@ enum Commands { /// Enable the slots endpoint (not recommended) slots_endpoint_enable: bool, + #[cfg(feature = "statsd_reporter")] #[arg(long, value_parser = parse_socket_addr)] /// Address of the statsd server to report metrics to statsd_addr: Option, + #[cfg(feature = "statsd_reporter")] #[arg(long, default_value = "paddler")] /// Prefix for statsd metrics statsd_prefix: String, + #[cfg(feature = "statsd_reporter")] #[arg(long, default_value = "10", value_parser = parse_duration)] /// Interval (in seconds) at which the balancer will report metrics to statsd statsd_reporting_interval: Duration, }, - // Command-line dashboard for monitoring the balancer - // Dashboard { - // #[arg(long, value_parser = parse_socket_addr)] - // management_addr: SocketAddr, - // }, + #[cfg(feature = "ratatui_dashboard")] + /// Command-line dashboard for monitoring the balancer + Dashboard { + #[arg(long, value_parser = parse_socket_addr)] + /// Address of the management server that the dashboard will connect to + management_addr: SocketAddr, + }, } fn main() -> Result<()> { @@ -154,8 +159,11 @@ fn main() -> Result<()> { reverseproxy_addr, rewrite_host_header, slots_endpoint_enable, + #[cfg(feature = "statsd_reporter")] statsd_addr, + #[cfg(feature = "statsd_reporter")] statsd_prefix, + #[cfg(feature = "statsd_reporter")] statsd_reporting_interval, }) => { cmd::balancer::handle( @@ -164,12 +172,16 @@ fn main() -> Result<()> { reverseproxy_addr, rewrite_host_header.to_owned(), slots_endpoint_enable.to_owned(), + #[cfg(feature = "statsd_reporter")] statsd_addr.to_owned(), + #[cfg(feature = "statsd_reporter")] statsd_prefix.to_owned(), + #[cfg(feature = "statsd_reporter")] statsd_reporting_interval.to_owned(), )?; } - // Some(Commands::Dashboard { management_addr }) => {} + #[cfg(feature = "ratatui_dashboard")] + Some(Commands::Dashboard { management_addr }) => {} None => {} }