Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 42e2618

Browse files
committedOct 21, 2024
Refactoring tracing span activation/deactivation
1 parent 91de703 commit 42e2618

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed
 

‎sentry-tracing/src/layer.rs

+38-8
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,50 @@ where
221221
sentry_span.set_data(key, value);
222222
}
223223

224-
sentry_core::configure_scope(|scope| scope.set_span(Some(sentry_span.clone())));
225-
226224
let mut extensions = span.extensions_mut();
227225
extensions.insert(SentrySpanData {
228226
sentry_span,
229227
parent_sentry_span,
230228
});
231229
}
232230

233-
/// When a span gets closed, finish the underlying sentry span, and set back
231+
///Called when span is entered
232+
///
233+
///When tracing Span is entered, it becomes ongoing, therefore we shall update current scope with it
234+
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
235+
let span = match ctx.span(&id) {
236+
Some(span) => span,
237+
None => return,
238+
};
239+
240+
let extensions = span.extensions();
241+
let span = match extensions.get::<SentrySpanData>() {
242+
Some(data) => &data.sentry_span,
243+
_ => return,
244+
};
245+
246+
sentry_core::configure_scope(|scope| scope.set_span(Some(span.clone())));
247+
}
248+
249+
///Called when span is exited
250+
///
251+
///When tracing Span exits, it awaits, therefore we shall set parent span as ongoing, if any
252+
fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) {
253+
let span = match ctx.span(&id) {
254+
Some(span) => span,
255+
None => return,
256+
};
257+
258+
let extensions = span.extensions();
259+
let span = match extensions.get::<SentrySpanData>() {
260+
Some(data) => &data.parent_sentry_span,
261+
_ => return,
262+
};
263+
264+
sentry_core::configure_scope(|scope| scope.set_span(span.clone()));
265+
}
266+
267+
/// When a span gets dropped/closed, finish the underlying sentry span, and set back
234268
/// its parent as the *current* sentry span.
235269
fn on_close(&self, id: span::Id, ctx: Context<'_, S>) {
236270
let span = match ctx.span(&id) {
@@ -239,16 +273,12 @@ where
239273
};
240274

241275
let mut extensions = span.extensions_mut();
242-
let SentrySpanData {
243-
sentry_span,
244-
parent_sentry_span,
245-
} = match extensions.remove::<SentrySpanData>() {
276+
let SentrySpanData { sentry_span, .. } = match extensions.remove::<SentrySpanData>() {
246277
Some(data) => data,
247278
None => return,
248279
};
249280

250281
sentry_span.finish();
251-
sentry_core::configure_scope(|scope| scope.set_span(parent_sentry_span));
252282
}
253283

254284
/// Implement the writing of extra data to span

0 commit comments

Comments
 (0)
Please sign in to comment.