Skip to content

Commit

Permalink
chore: put statsd and ratatui dashboard behind feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Nov 21, 2024
1 parent 49bd16c commit f90e720
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"]
4 changes: 3 additions & 1 deletion src/balancer/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/balancer/upstream_peer_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -127,6 +128,7 @@ impl UpstreamPeerPool {
})
}

#[cfg(feature = "statsd_reporter")]
#[inline]
fn with_agents_read<TCallback, TResult>(&self, cb: TCallback) -> Result<TResult>
where
Expand Down
16 changes: 11 additions & 5 deletions src/cmd/balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr>,
statsd_prefix: String,
statsd_reporting_interval: Duration,
#[cfg(feature = "statsd_reporter")] statsd_addr: Option<SocketAddr>,
#[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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/errors/app_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr>,

#[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<()> {
Expand Down Expand Up @@ -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(
Expand All @@ -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 => {}
}

Expand Down

0 comments on commit f90e720

Please sign in to comment.