Skip to content

feat(python): Use new scope API #10892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions develop-docs/sdk/hub_and_scope_refactoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ Here's how the new APIs roughly map to the old-style APIs. In case of `configure
</tr>
<tr>
<td rowspan="2" style={{verticalAlign: "middle"}}><code>with configure_scope() as scope</code></td>
<td><code>scope = Scope.get_isolation_scope()</code></td>
<td><code>scope = get_isolation_scope()</code></td>
<td>Should affect the whole transaction (one request/response cycle, one execution of a task, etc.).</td>
</tr>
<tr>
<td><code>scope = Scope.get_current_scope()</code></td>
<td><code>scope = get_current_scope()</code></td>
<td>Should affect the whole span or a part of code isolated via a forked current scope with <code>new_scope</code>.</td>
</tr>
<tr>
Expand Down
22 changes: 11 additions & 11 deletions docs/platforms/python/migration/1.x-to-2.x.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The old APIs will continue to work in 2.0, but we recommend migrating to their n

### Activating a Custom Hub

If you were using a custom hub to isolate event data, we recommend using a custom isolation scope instead. To activate the custom isolation scope, you will need to explicitly pass the isolation scope to the `use_isolation_scope`, which creates a context manager that activates the scope. Here is an example of how to change your code to make it work:
If you were using a custom hub to isolate event data, we recommend using a custom isolation scope instead. To activate the custom isolation scope, you will need to explicitly pass the isolation scope to `use_isolation_scope`, which creates a context manager that activates the scope. Here is an example of how to change your code to make it work:

```python diff
import sentry_sdk
Expand Down Expand Up @@ -106,7 +106,7 @@ If you previously used `with sentry_sdk.Hub(sentry_sdk.Hub.current)` to clone th

`sentry_sdk.isolation_scope` is a context manager that creates a new isolation scope by forking the current isolation scope. The forked isolation scope is activated during the context manager's lifetime, providing a similar level of isolation within the context manager as the previous `Hub`-based approach.

Note that using `sentry_sdk.isolation_scope()` is equivalent to `sentry_sdk.scope.use_isolation_scope(sentry_sdk.Scope.get_isolation_scope().fork())`.
Note that using `sentry_sdk.isolation_scope()` is equivalent to `sentry_sdk.scope.use_isolation_scope(sentry_sdk.get_isolation_scope().fork())`.

### Scope Configuring

Expand All @@ -118,18 +118,18 @@ a task, and so on) or just a specific part of the code.
If you want your scope change to affect a smaller unit of code such as a span, use the current scope.

```python diff
- with configure_scope() as scope:
- with sentry_sdk.configure_scope() as scope:
- # do something with scope
+ scope = Scope.get_current_scope()
+ scope = sentry_sdk.get_current_scope()
+ # do something with scope
```

If you want your scope change to affect the whole transaction, use the isolation scope.

```python diff
- with configure_scope() as scope:
- with sentry_sdk.configure_scope() as scope:
- # do something with scope
+ scope = Scope.get_isolation_scope()
+ scope = sentry_sdk.get_isolation_scope()
+ # do something with scope
```

Expand All @@ -143,7 +143,7 @@ transaction = sentry_sdk.transaction(...)
- with sentry_sdk.configure_scope() as scope:
- scope.set_transaction_name("new-transaction-name")

+ scope = sentry_sdk.Scope.get_current_scope()
+ scope = sentry_sdk.get_current_scope()
+ scope.set_transaction_name("new-transaction-name")
```

Expand All @@ -154,18 +154,18 @@ Scope pushing translates to forking a scope in `2.0`. You either want to fork th
If you only want to apply something to a smaller, localized part of the code, fork the current scope with `new_scope()`:

```python diff
- with push_scope() as scope:
- with sentry_sdk.push_scope() as scope:
- # do something
+ with new_scope() as scope:
+ with sentry_sdk.new_scope() as scope:
+ # do something
```

If you're wrapping a whole request-response cycle or a whole execution of a task, fork the isolation scope:

```python diff
- with push_scope() as scope:
- with sentry_sdk.push_scope() as scope:
- # do something
+ with isolation_scope() as scope:
+ with sentry_sdk.isolation_scope() as scope:
+ # do something
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
```python
import sentry_sdk
from sentry_sdk.scope import Scope

scope = Scope.get_current_scope()
scope = sentry_sdk.get_current_scope()
scope.set_tag("my-tag", "my value")
scope.user = {"id": 42, "email": "[email protected]"}

Expand Down
Loading