Skip to content

Commit

Permalink
fix(sentry-tracing): switch sentry spans on enter and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Sep 25, 2024
1 parent 96b35c7 commit 7a4b72c
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,48 @@ 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,
parent_sentry_span,
});
}

/// 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::<SentrySpanData>() {
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::<SentrySpanData>() {
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>) {
Expand All @@ -239,16 +272,12 @@ where
};

let mut extensions = span.extensions_mut();
let SentrySpanData {
sentry_span,
parent_sentry_span,
} = match extensions.remove::<SentrySpanData>() {
let SentrySpanData { sentry_span, .. } = match extensions.remove::<SentrySpanData>() {
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
Expand Down

0 comments on commit 7a4b72c

Please sign in to comment.