refactor: rework init_tracing to avoid unused variable

... when feature `telemetry-otlp` is not enabled
move feature management into tracing_setup module to make the server code cleaner.
This commit is contained in:
Gwen Lg
2026-01-28 11:38:52 +01:00
parent 7d05d9d520
commit 3b6daa7d0f
3 changed files with 51 additions and 42 deletions
-1
View File
@@ -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")))]
+1 -7
View File
@@ -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<bool>) {
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...");
+50 -34
View File
@@ -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(())
}
}