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.
This commit is contained in:
Quentin Gliech
2026-03-31 12:15:41 +02:00
parent 2ed7c3529b
commit ede4ae6450
2 changed files with 11 additions and 4 deletions

3
Cargo.lock generated
View File

@@ -4124,6 +4124,7 @@ dependencies = [
"opentelemetry_sdk", "opentelemetry_sdk",
"prost", "prost",
"thiserror 2.0.17", "thiserror 2.0.17",
"tonic",
] ]
[[package]] [[package]]
@@ -6417,6 +6418,8 @@ dependencies = [
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"sync_wrapper", "sync_wrapper",
"tokio",
"tokio-rustls",
"tokio-stream", "tokio-stream",
"tower-layer", "tower-layer",
"tower-service", "tower-service",

View File

@@ -1,12 +1,13 @@
// Copyright 2025, 2026 Element Creations Ltd.
// Copyright 2025 New Vector Ltd. // Copyright 2025 New Vector Ltd.
// //
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details. // Please see LICENSE files in the repository root for full details.
use console::{Color, Style}; use console::{Color, Style};
use opentelemetry::TraceId; use opentelemetry::{TraceId, trace::TraceContextExt as _};
use tracing::{Level, Subscriber}; use tracing::{Level, Subscriber};
use tracing_opentelemetry::OtelData; use tracing_opentelemetry::get_otel_context;
use tracing_subscriber::{ use tracing_subscriber::{
fmt::{ fmt::{
FormatEvent, FormatFields, 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 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() if let Some(span) = ctx.lookup_current()
&& let Some(otel) = span.extensions().get::<OtelData>() && let Some(trace_id) = tracing::dispatcher::get_default(|dispatch| {
&& let Some(trace_id) = otel.trace_id() 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 && trace_id != TraceId::INVALID
{ {
let label = Style::new() let label = Style::new()