From 84633eaa9099470f688b2c20ab5febb024a60456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Przytu=C5=82a?= <59482568+wprzytula@users.noreply.github.com> Date: Thu, 8 Sep 2022 00:55:18 +0200 Subject: [PATCH] opentelemetry: fix `on_event` not respecting events' explicit parents (#2296) ## Motivation One can want to have an event added from outside context of a span, especially in an async code (e.g. between polls of the future, or between polling multiple futures instrumented with that span). Then it is expected that the event will be attached indeed to the span specified as the parent and not the contextual one. Fixes: #2295 ## Solution See #2295 --- tracing-opentelemetry/src/subscriber.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tracing-opentelemetry/src/subscriber.rs b/tracing-opentelemetry/src/subscriber.rs index 9a781aa9bf..f0936271e8 100644 --- a/tracing-opentelemetry/src/subscriber.rs +++ b/tracing-opentelemetry/src/subscriber.rs @@ -620,8 +620,8 @@ where /// [`ERROR`]: tracing::Level::ERROR /// [`Error`]: opentelemetry::trace::StatusCode::Error fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) { - // Ignore events that are not in the context of a span - if let Some(span) = ctx.lookup_current() { + // Ignore events that have no explicit parent set *and* are not in the context of a span + if let Some(span) = ctx.event_span(event) { // Performing read operations before getting a write lock to avoid a deadlock // See https://github.com/tokio-rs/tracing/issues/763 #[cfg(feature = "tracing-log")]