From ede4ae6450c38034c2955c5b4215f327831b71fd Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Tue, 31 Mar 2026 12:15:41 +0200 Subject: [PATCH] Use `get_otel_context` instead of accessing `OtelData` directly `OtelData` is going to become private in a future version of `tracing-opentelemetry`. Use the new `get_otel_context` function introduced in 0.32.1 to extract the trace ID from the span context. --- Cargo.lock | 3 +++ crates/context/src/fmt.rs | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3995e181e..a2feff303 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4124,6 +4124,7 @@ dependencies = [ "opentelemetry_sdk", "prost", "thiserror 2.0.17", + "tonic", ] [[package]] @@ -6417,6 +6418,8 @@ dependencies = [ "percent-encoding", "pin-project", "sync_wrapper", + "tokio", + "tokio-rustls", "tokio-stream", "tower-layer", "tower-service", diff --git a/crates/context/src/fmt.rs b/crates/context/src/fmt.rs index 47e72fcae..5d52625fc 100644 --- a/crates/context/src/fmt.rs +++ b/crates/context/src/fmt.rs @@ -1,12 +1,13 @@ +// Copyright 2025, 2026 Element Creations Ltd. // Copyright 2025 New Vector Ltd. // // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial // Please see LICENSE files in the repository root for full details. use console::{Color, Style}; -use opentelemetry::TraceId; +use opentelemetry::{TraceId, trace::TraceContextExt as _}; use tracing::{Level, Subscriber}; -use tracing_opentelemetry::OtelData; +use tracing_opentelemetry::get_otel_context; use tracing_subscriber::{ fmt::{ FormatEvent, FormatFields, @@ -127,8 +128,11 @@ where // If we have a OTEL span, we can add the trace ID to the end of the log line if let Some(span) = ctx.lookup_current() - && let Some(otel) = span.extensions().get::() - && let Some(trace_id) = otel.trace_id() + && let Some(trace_id) = tracing::dispatcher::get_default(|dispatch| { + let otel_cx = get_otel_context(&mut span.extensions_mut(), dispatch)?; + let trace_id = otel_cx.span().span_context().trace_id(); + Some(trace_id) + }) && trace_id != TraceId::INVALID { let label = Style::new()