diff --git a/sentry-tracing/src/layer.rs b/sentry-tracing/src/layer.rs index 8183bbfc..6790e422 100644 --- a/sentry-tracing/src/layer.rs +++ b/sentry-tracing/src/layer.rs @@ -221,8 +221,6 @@ where sentry_span.set_data(key, value); } - sentry_core::configure_scope(|scope| scope.set_span(Some(sentry_span.clone()))); - let mut extensions = span.extensions_mut(); extensions.insert(SentrySpanData { sentry_span, @@ -230,6 +228,41 @@ where }); } + /// Sets entered span as *current* sentry span. A tracing span can be + /// entered and existed multiple times, for example, when using a `tracing::Instrumented` future. + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { + let span = match ctx.span(&id) { + Some(span) => span, + None => return, + }; + + let extensions = span.extensions(); + let SentrySpanData { sentry_span, .. } = match extensions.get::() { + Some(data) => data, + None => return, + }; + + sentry_core::configure_scope(|scope| scope.set_span(Some(sentry_span.clone()))); + } + + /// Set exited span's parent as *current* sentry span. + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { + let span = match ctx.span(&id) { + Some(span) => span, + None => return, + }; + + let extensions = span.extensions(); + let SentrySpanData { + parent_sentry_span, .. + } = match extensions.get::() { + Some(data) => data, + None => return, + }; + + sentry_core::configure_scope(|scope| scope.set_span(parent_sentry_span.clone())); + } + /// When a span gets closed, finish the underlying sentry span, and set back /// its parent as the *current* sentry span. fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { @@ -239,16 +272,12 @@ where }; let mut extensions = span.extensions_mut(); - let SentrySpanData { - sentry_span, - parent_sentry_span, - } = match extensions.remove::() { + let SentrySpanData { sentry_span, .. } = match extensions.remove::() { Some(data) => data, None => return, }; sentry_span.finish(); - sentry_core::configure_scope(|scope| scope.set_span(parent_sentry_span)); } /// Implement the writing of extra data to span