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",
"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",

View File

@@ -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::<OtelData>()
&& 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()