{"record":{"id":"171c01e7e7aa5eaf","repo":"influxdata/influxdb","slug":"cannot-set-global-tracing-subscriber","errorCode":null,"errorMessage":"Cannot set global tracing subscriber","messagePattern":"Cannot set global tracing subscriber","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"core/trogging/src/lib.rs","lineNumber":37,"sourceCode":"    EnvFilter, Layer,\n    fmt::{self, MakeWriter, writer::BoxMakeWriter},\n    layer::SubscriberExt,\n    registry::LookupSpan,\n};\n\n/// Maximum length of a log line.\n/// Space for a final trailing newline if truncated.\n///\n/// Docker \"chunks\" log message in 16KB chunks. The log driver receives log lines in such chunks\n/// but not all of the log drivers properly recombine the lines, and even those who do have their\n/// own max buffer sizes (e.g. for our fluentd config it's 32KB).\n/// To avoid surprises, for now, let's just truncate log lines right below 16K and make sure\n/// they are properly terminated with a newline if they were so before the truncation.\nconst MAX_LINE_LENGTH: usize = 16 * 1024 - 1;\n\n#[derive(Debug, Error)]\npub enum Error {\n    #[error(\"Cannot set global tracing subscriber\")]\n    SetGlobalDefaultError(#[from] tracing::dispatcher::SetGlobalDefaultError),\n\n    #[error(\"Cannot set global log subscriber\")]\n    SetLoggerError(#[from] tracing_log::log_tracer::SetLoggerError),\n\n    #[error(\"`tokio_console` feature not enabled\")]\n    TokioConsoleMissing,\n}\n\npub type Result<T, E = Error> = std::result::Result<T, E>;\n\n/// Builder to configure tracing and logging.\n#[derive(Debug)]\npub struct Builder<W = fn() -> io::Stdout> {\n    log_format: LogFormat,\n    log_filter: Option<EnvFilter>,\n    // used when log_filter is none.\n    default_log_filter: EnvFilter,","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/influxdata/influxdb/blob/06200ef96ba82c5f6727e5038a83af8e722c6875/core/trogging/src/lib.rs#L19-L55","documentation":"This is a variant of trogging's `Error` enum wrapping `tracing::dispatcher::SetGlobalDefaultError`. It occurs when initializing logging fails because a global tracing subscriber was already set (the global default can only be set once per process). The library converts the underlying tracing error into this user-readable message via `#[from]`.","triggerScenarios":"Calling trogging's builder init (e.g. `Builder::new()...build().init()` or `set_global_default`) after another component already installed a global tracing subscriber in the same process.","commonSituations":"A binary initializes logging twice (e.g. main() plus a library's init); running tests where each test tries to set a global subscriber; embedding the server in another app that has its own tracing setup.","solutions":["Initialize the global subscriber exactly once, early in main.","Guard with `tracing::dispatcher::has_been_set()` or ignore the second init attempt.","In tests, use a local default subscriber (tracing::subscriber::with_default) instead of the global one."],"exampleFix":"// before\nlet _ = trogging::Builder::new().build().unwrap().init();\ninit_logging_again(); // panics: already set\n// after\nif !tracing::dispatcher::has_been_set() {\n    let _ = trogging::Builder::new().build().unwrap().init();\n}","handlingStrategy":"try-catch","validationCode":"if tracing::dispatcher::has_been_set() {\n    eprintln!(\"global subscriber already set; skipping init\");\n}","typeGuard":"fn can_init_global_tracing() -> bool {\n    !tracing::dispatcher::has_been_set()\n}","tryCatchPattern":"match trogging::Builder::new().build().init() {\n    Ok(_) => {}\n    Err(trogging::Error::SetGlobalDefaultError(_)) => { /* already initialized */ }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Initialize logging exactly once, at the top of main","Make test logging setup idempotent with Once/OnceLock","Prefer with_default (thread-local) subscribers in tests"],"tags":["rust","tracing","logging","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"06200ef96ba82c5f6727e5038a83af8e722c6875","analyzedAt":"2026-09-19T12:55:30.003Z","contentChangedAt":"2026-09-19T12:55:30.003Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}