sentry-rust icon indicating copy to clipboard operation
sentry-rust copied to clipboard

fix(sentry-tracing): switch sentry spans on enter and exit

Open saiintbrisson opened this issue 1 year ago • 7 comments
trafficstars

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)

Screenshot 2024-09-25 at 14 22 57

tracing-opentelemetry layer (correct)

Screenshot 2024-09-25 at 14 28 11

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.

saiintbrisson avatar Sep 25 '24 17:09 saiintbrisson