Skip to content

Commit cc58b3d

Browse files
committed
fix: fix trace_id in logs when nested in tracing crate's spans
1 parent a89ab3f commit cc58b3d

File tree

1 file changed

+14
-16
lines changed
  • opentelemetry-appender-tracing/src

1 file changed

+14
-16
lines changed

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,28 +247,26 @@ where
247247
#[cfg(feature = "experimental_use_tracing_span_context")]
248248
if let Some(span) = _ctx.event_span(event) {
249249
use tracing_opentelemetry::OtelData;
250-
let opt_span_id = span
251-
.extensions()
252-
.get::<OtelData>()
253-
.and_then(|otd| otd.builder.span_id);
254-
255-
let opt_trace_id = span.scope().last().and_then(|root_span| {
256-
root_span
257-
.extensions()
258-
.get::<OtelData>()
259-
.and_then(|otd| otd.builder.trace_id)
260-
});
261-
262-
if let Some((trace_id, span_id)) = opt_trace_id.zip(opt_span_id) {
263-
log_record.set_trace_context(trace_id, span_id, None);
264-
}
265-
266250
if let Some(otd) = span.extensions().get::<OtelData>() {
267251
if let Some(attributes) = &otd.builder.attributes {
268252
for attribute in attributes {
269253
log_record.add_attribute(attribute.key.clone(), &attribute.value);
270254
}
271255
}
256+
257+
if let Some(span_id) = otd.builder.span_id {
258+
let opt_trace_id = otd.builder.trace_id.or_else(|| {
259+
span.scope().last().and_then(|root_span| {
260+
root_span
261+
.extensions()
262+
.get::<OtelData>()
263+
.and_then(|otd| otd.builder.trace_id)
264+
})
265+
});
266+
if let Some(trace_id) = opt_trace_id {
267+
log_record.set_trace_context(trace_id, span_id, None);
268+
}
269+
}
272270
}
273271
}
274272

0 commit comments

Comments
 (0)