Skip to content

Commit 2db6a04

Browse files
authored
Add span activate and deactivate apis (#4447)
1 parent cfbb8db commit 2db6a04

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
168168
- `span_id`
169169
- `parent_span_id`: you can supply a `parent_span` instead
170170
- The `Scope.transaction` property has been removed. To obtain the root span (previously transaction), use `Scope.root_span`. To set the root span's (transaction's) name, use `Scope.set_transaction_name()`.
171-
- The `Scope.span =` setter has been removed.
171+
- The `Scope.span =` setter has been removed. Please use the new `span.activate()` api instead if you want to activate a new span manually instead of using the `start_span` context manager.
172172
- Passing a list or `None` for `failed_request_status_codes` in the Starlette integration is no longer supported. Pass a set of integers instead.
173173
- The `span` argument of `Scope.trace_propagation_meta` is no longer supported.
174174
- Setting `Scope.user` directly is no longer supported. Use `Scope.set_user()` instead.

sentry_sdk/tracing.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,21 @@ def __repr__(self):
270270
)
271271
)
272272

273-
def __enter__(self):
274-
# type: () -> Span
275-
# XXX use_span? https://github.com/open-telemetry/opentelemetry-python/blob/3836da8543ce9751051e38a110c0468724042e62/opentelemetry-api/src/opentelemetry/trace/__init__.py#L547
276-
#
277-
# create a Context object with parent set as current span
273+
def activate(self):
274+
# type: () -> None
278275
ctx = otel_trace.set_span_in_context(self._otel_span)
279276
# set as the implicit current context
280277
self._ctx_token = context.attach(ctx)
281278

279+
def deactivate(self):
280+
# type: () -> None
281+
if self._ctx_token:
282+
context.detach(self._ctx_token)
283+
del self._ctx_token
284+
285+
def __enter__(self):
286+
# type: () -> Span
287+
self.activate()
282288
return self
283289

284290
def __exit__(self, ty, value, tb):
@@ -294,8 +300,7 @@ def __exit__(self, ty, value, tb):
294300
self.set_status(SPANSTATUS.OK)
295301

296302
self.finish()
297-
context.detach(self._ctx_token)
298-
del self._ctx_token
303+
self.deactivate()
299304

300305
@property
301306
def description(self):

0 commit comments

Comments
 (0)