From 2ad536db1716ca8bb83fff8fb2f391417ba92bb1 Mon Sep 17 00:00:00 2001 From: "Aode (Lion)" Date: Mon, 21 Mar 2022 22:41:51 -0500 Subject: [PATCH] Promote console from compile flag to runtime flag --- Cargo.toml | 3 +-- src/config.rs | 21 +++------------------ src/init_tracing.rs | 39 +++++++++++++++++++++++++++------------ src/main.rs | 1 - 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 66abe89..d8c5ff3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -console = ["console-subscriber"] default = ["object-storage"] object-storage = ["reqwest", "rust-s3"] io-uring = [ @@ -30,7 +29,7 @@ async-trait = "0.1.51" awc = { version = "3.0.0", default-features = false, features = ["rustls"] } base64 = "0.13.0" config = "0.12.0" -console-subscriber = { version = "0.1", optional = true } +console-subscriber = "0.1" dashmap = "5.1.0" futures-util = "0.3.17" mime = "0.3.1" diff --git a/src/config.rs b/src/config.rs index 3d43ad2..be1e09c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -82,7 +82,6 @@ pub(crate) struct Overrides { #[serde(skip_serializing_if = "Option::is_none")] sled_cache_capacity: Option, - #[cfg(feature = "console")] #[structopt( long, help = "Specify the number of events the console subscriber is allowed to buffer" @@ -122,19 +121,11 @@ impl Overrides { && self.max_image_height.is_none() && self.max_image_area.is_none() && self.sled_cache_capacity.is_none() - && self.default_console_settings() + && self.console_buffer_capacity.is_none() && self.api_key.is_none() && self.opentelemetry_url.is_none() && self.store.is_none() } - - fn default_console_settings(&self) -> bool { - #[cfg(feature = "console")] - return self.console_buffer_capacity.is_none(); - - #[cfg(not(feature = "console"))] - true - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] @@ -209,8 +200,7 @@ pub(crate) struct Config { max_image_height: usize, max_image_area: usize, sled_cache_capacity: u64, - #[cfg(feature = "console")] - console_buffer_capacity: usize, + console_buffer_capacity: Option, api_key: Option, opentelemetry_url: Option, store: Store, @@ -226,8 +216,6 @@ pub(crate) struct Defaults { max_image_height: usize, max_image_area: usize, sled_cache_capacity: u64, - #[cfg(feature = "console")] - console_buffer_capacity: usize, store: Store, } @@ -241,8 +229,6 @@ impl Defaults { max_image_height: 10_000, max_image_area: 40_000_000, sled_cache_capacity: 1024 * 1024 * 64, // 16 times smaller than sled's default of 1GB - #[cfg(feature = "console")] - console_buffer_capacity: 1024 * 128, store: Store::FileStore { path: None }, } } @@ -298,8 +284,7 @@ impl Config { self.sled_cache_capacity } - #[cfg(feature = "console")] - pub(crate) fn console_buffer_capacity(&self) -> usize { + pub(crate) fn console_buffer_capacity(&self) -> Option { self.console_buffer_capacity } diff --git a/src/init_tracing.rs b/src/init_tracing.rs index e4cd2d5..65a5d51 100644 --- a/src/init_tracing.rs +++ b/src/init_tracing.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "console")] use console_subscriber::ConsoleLayer; use opentelemetry::{ sdk::{propagation::TraceContextPropagator, Resource}, @@ -9,14 +8,15 @@ use tracing::subscriber::set_global_default; use tracing_error::ErrorLayer; use tracing_log::LogTracer; use tracing_subscriber::{ - filter::Targets, fmt::format::FmtSpan, layer::SubscriberExt, Layer, Registry, + filter::Targets, fmt::format::FmtSpan, layer::SubscriberExt, registry::LookupSpan, Layer, + Registry, }; use url::Url; pub(super) fn init_tracing( servic_name: &'static str, opentelemetry_url: Option<&Url>, - #[cfg(feature = "console")] buffer_capacity: usize, + buffer_capacity: Option, ) -> anyhow::Result<()> { LogTracer::init()?; @@ -30,20 +30,35 @@ pub(super) fn init_tracing( .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) .with_filter(targets.clone()); - #[cfg(feature = "console")] - let console_layer = ConsoleLayer::builder() - .with_default_env() - .event_buffer_capacity(buffer_capacity) - .server_addr(([0, 0, 0, 0], 6669)) - .spawn(); - let subscriber = Registry::default() .with(format_layer) .with(ErrorLayer::default()); - #[cfg(feature = "console")] - let subscriber = subscriber.with(console_layer); + if let Some(buffer_capacity) = buffer_capacity { + let console_layer = ConsoleLayer::builder() + .with_default_env() + .event_buffer_capacity(buffer_capacity) + .server_addr(([0, 0, 0, 0], 6669)) + .spawn(); + let subscriber = subscriber.with(console_layer); + + with_otel(subscriber, targets, servic_name, opentelemetry_url) + } else { + with_otel(subscriber, targets, servic_name, opentelemetry_url) + } +} + +fn with_otel( + subscriber: S, + targets: Targets, + servic_name: &'static str, + opentelemetry_url: Option<&Url>, +) -> anyhow::Result<()> +where + S: SubscriberExt + Send + Sync, + for<'a> S: LookupSpan<'a>, +{ if let Some(url) = opentelemetry_url { let tracer = opentelemetry_otlp::new_pipeline() diff --git a/src/main.rs b/src/main.rs index bce5b6a..4b2a059 100644 --- a/src/main.rs +++ b/src/main.rs @@ -885,7 +885,6 @@ async fn main() -> anyhow::Result<()> { init_tracing( "pict-rs", CONFIG.opentelemetry_url(), - #[cfg(feature = "console")] CONFIG.console_buffer_capacity(), )?;