sentry-rust
sentry-rust copied to clipboard
fix(sentry-tracing): switch sentry spans on enter and exit
I've been facing a bug with Sentry Trace where concurrent spans end up wrongly nested. At first, I thought this was some strange autogrouping bug and event sent a feedback (sorry Sentry support member 😅).
After digging a little bit, I discovered that the current tracing layer implementation sets the sentry span as active as soon as a tracing span is created, which should not be the case, as spans are only active once entered. Following the same logic, it also reverts to the parent span only when a span is closed, and not when it's exited.
A span can be entered and exited multiple times, for example, when a tracing Instrumented future polls and returns Pending. In case of concurrent Instrumented futures, the current impl ends up nesting Futures under the first created one.
sentry-tracing layer (wrong)
tracing-opentelemetry layer (correct)
I'd say that the best idea would be to not use
sentry_core::configure_scope(|s| s.get_span());when creating a new span to get the parent, and get it from the span extensions instead, but the current behavior might be relied upon already by someone.