pub fn init_if_enabled(enabled: bool) -> bool {
if !enabled {
return false;
}
real::init()
}
#[cfg(feature = "otel")]
mod real {
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*, EnvFilter};
pub(super) fn init() -> bool {
let filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap_or_default();
let layer = tracing_subscriber::fmt::layer()
.json()
.with_span_events(FmtSpan::CLOSE)
.with_target(true);
let installed = tracing_subscriber::registry()
.with(filter)
.with(layer)
.try_init()
.is_ok();
if installed {
tracing::info!(
target = "ssg::otel",
"OpenTelemetry build tracing enabled (JSON to stdout)"
);
}
installed
}
}
#[cfg(not(feature = "otel"))]
mod real {
pub(super) fn init() -> bool {
log::warn!(
"[--trace] requested but this binary was built without the \
`otel` feature. Rebuild with `cargo build --features otel` \
to enable build-pipeline tracing."
);
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn init_disabled_is_noop_returns_false() {
assert!(!init_if_enabled(false));
}
#[cfg(not(feature = "otel"))]
#[test]
fn init_enabled_without_feature_warns_and_returns_false() {
assert!(!init_if_enabled(true));
}
}