Skip to content

Commit 74aed1a

Browse files
committed
Add span activate and deactivate apis
1 parent cfbb8db commit 74aed1a

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,20 @@ 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+
if self._ctx_token:
281+
context.detach(self._ctx_token)
282+
del self._ctx_token
283+
284+
def __enter__(self):
285+
# type: () -> Span
286+
self.activate()
282287
return self
283288

284289
def __exit__(self, ty, value, tb):
@@ -294,8 +299,7 @@ def __exit__(self, ty, value, tb):
294299
self.set_status(SPANSTATUS.OK)
295300

296301
self.finish()
297-
context.detach(self._ctx_token)
298-
del self._ctx_token
302+
self.deactivate()
299303

300304
@property
301305
def description(self):

0 commit comments

Comments
 (0)