1111
1212from sentry_sdk .hub import Hub
1313from sentry_sdk .utils import capture_internal_exceptions , event_from_exception
14- from sentry_sdk .tracing import SpanContext
14+ from sentry_sdk .tracing import Span
1515from sentry_sdk ._compat import reraise
1616from sentry_sdk .integrations import Integration
1717from sentry_sdk .integrations .logging import ignore_logger
@@ -82,20 +82,30 @@ def _inner(*args, **kwargs):
8282 with hub .push_scope () as scope :
8383 scope ._name = "celery"
8484 scope .clear_breadcrumbs ()
85- _continue_trace (args [3 ].get ("headers" ) or {}, scope )
85+ span = _continue_trace (args [3 ].get ("headers" ) or {}, scope )
86+
87+ with capture_internal_exceptions ():
88+ scope .transaction = task .name
89+
8690 scope .add_event_processor (_make_event_processor (task , * args , ** kwargs ))
8791
88- return f (* args , ** kwargs )
92+ try :
93+ return f (* args , ** kwargs )
94+ finally :
95+ span .finish ()
96+ hub .capture_trace (span )
8997
9098 return _inner
9199
92100
93101def _continue_trace (headers , scope ):
94102 if headers :
95- span_context = SpanContext .continue_from_headers (headers )
103+ span = Span .continue_from_headers (headers )
96104 else :
97- span_context = SpanContext .start_trace ()
98- scope .set_span_context (span_context )
105+ span = Span .start_trace ()
106+
107+ scope .span = span
108+ return span
99109
100110
101111def _wrap_task_call (task , f ):
@@ -115,9 +125,6 @@ def _inner(*args, **kwargs):
115125
116126def _make_event_processor (task , uuid , args , kwargs , request = None ):
117127 def event_processor (event , hint ):
118- with capture_internal_exceptions ():
119- event ["transaction" ] = task .name
120-
121128 with capture_internal_exceptions ():
122129 extra = event .setdefault ("extra" , {})
123130 extra ["celery-job" ] = {
0 commit comments