diff --git a/src/garage/main.rs b/src/garage/main.rs index 4c54a251..403ba55c 100644 --- a/src/garage/main.rs +++ b/src/garage/main.rs @@ -7,7 +7,6 @@ extern crate tracing; mod cli; mod secrets; mod server; -#[cfg(feature = "telemetry-otlp")] mod tracing_setup; #[cfg(not(any(feature = "bundled-libs", feature = "system-libs")))] diff --git a/src/garage/server.rs b/src/garage/server.rs index e2ed6607..f9cd5631 100644 --- a/src/garage/server.rs +++ b/src/garage/server.rs @@ -15,8 +15,7 @@ use garage_web::WebServer; use garage_api_k2v::api_server::K2VApiServer; use crate::secrets::{fill_secrets, Secrets}; -#[cfg(feature = "telemetry-otlp")] -use crate::tracing_setup::*; +use crate::tracing_setup::init_tracing; async fn wait_from(mut chan: watch::Receiver) { while !*chan.borrow() { @@ -54,12 +53,7 @@ pub async fn run_server(config_file: PathBuf, secrets: Secrets) -> Result<(), Er if let Some(admin_trace_sink) = &config.admin.trace_sink { info!("Initialize tracing..."); - - #[cfg(feature = "telemetry-otlp")] init_tracing(admin_trace_sink, garage.system.id)?; - - #[cfg(not(feature = "telemetry-otlp"))] - error!("Garage was built without OTLP exporter, admin.trace_sink is ignored."); } info!("Initialize Admin API server and metrics collector..."); diff --git a/src/garage/tracing_setup.rs b/src/garage/tracing_setup.rs index 55fc4094..d5568c82 100644 --- a/src/garage/tracing_setup.rs +++ b/src/garage/tracing_setup.rs @@ -1,37 +1,53 @@ -use std::time::Duration; +pub use telemetry::init_tracing; -use opentelemetry::sdk::{ - trace::{self, IdGenerator, Sampler}, - Resource, -}; -use opentelemetry::KeyValue; -use opentelemetry_otlp::WithExportConfig; +#[cfg(not(feature = "telemetry-otlp"))] +mod telemetry { + use garage_util::data::Uuid; + use garage_util::error::Error; -use garage_util::data::*; -use garage_util::error::*; - -pub fn init_tracing(export_to: &str, node_id: Uuid) -> Result<(), Error> { - let node_id = hex::encode(&node_id.as_slice()[..8]); - - opentelemetry_otlp::new_pipeline() - .tracing() - .with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_endpoint(export_to) - .with_timeout(Duration::from_secs(3)), - ) - .with_trace_config( - trace::config() - .with_id_generator(IdGenerator::default()) - .with_sampler(Sampler::AlwaysOn) - .with_resource(Resource::new(vec![ - KeyValue::new("service.name", "garage"), - KeyValue::new("service.instance.id", node_id), - ])), - ) - .install_batch(opentelemetry::runtime::Tokio) - .ok_or_message("Unable to initialize tracing")?; - - Ok(()) + pub fn init_tracing(_: &str, _: Uuid) -> Result<(), Error> { + error!("Garage was built without OTLP exporter, admin.trace_sink is ignored."); + Ok(()) + } +} + +#[cfg(feature = "telemetry-otlp")] +mod telemetry { + use std::time::Duration; + + use opentelemetry::sdk::{ + trace::{self, IdGenerator, Sampler}, + Resource, + }; + use opentelemetry::KeyValue; + use opentelemetry_otlp::WithExportConfig; + + use garage_util::data::*; + use garage_util::error::*; + + pub fn init_tracing(export_to: &str, node_id: Uuid) -> Result<(), Error> { + let node_id = hex::encode(&node_id.as_slice()[..8]); + + opentelemetry_otlp::new_pipeline() + .tracing() + .with_exporter( + opentelemetry_otlp::new_exporter() + .tonic() + .with_endpoint(export_to) + .with_timeout(Duration::from_secs(3)), + ) + .with_trace_config( + trace::config() + .with_id_generator(IdGenerator::default()) + .with_sampler(Sampler::AlwaysOn) + .with_resource(Resource::new(vec![ + KeyValue::new("service.name", "garage"), + KeyValue::new("service.instance.id", node_id), + ])), + ) + .install_batch(opentelemetry::runtime::Tokio) + .ok_or_message("Unable to initialize tracing")?; + + Ok(()) + } }