Skip to content

Commit f6dc946

Browse files
authored
feat(python): Use new scope API (#10892)
1 parent 1013f16 commit f6dc946

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

develop-docs/sdk/hub_and_scope_refactoring.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ Here's how the new APIs roughly map to the old-style APIs. In case of `configure
183183
</tr>
184184
<tr>
185185
<td rowspan="2" style={{verticalAlign: "middle"}}><code>with configure_scope() as scope</code></td>
186-
<td><code>scope = Scope.get_isolation_scope()</code></td>
186+
<td><code>scope = get_isolation_scope()</code></td>
187187
<td>Should affect the whole transaction (one request/response cycle, one execution of a task, etc.).</td>
188188
</tr>
189189
<tr>
190-
<td><code>scope = Scope.get_current_scope()</code></td>
190+
<td><code>scope = get_current_scope()</code></td>
191191
<td>Should affect the whole span or a part of code isolated via a forked current scope with <code>new_scope</code>.</td>
192192
</tr>
193193
<tr>

docs/platforms/python/migration/1.x-to-2.x.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The old APIs will continue to work in 2.0, but we recommend migrating to their n
6767

6868
### Activating a Custom Hub
6969

70-
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:
70+
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:
7171

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

107107
`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.
108108

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

111111
### Scope Configuring
112112

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

120120
```python diff
121-
- with configure_scope() as scope:
121+
- with sentry_sdk.configure_scope() as scope:
122122
- # do something with scope
123-
+ scope = Scope.get_current_scope()
123+
+ scope = sentry_sdk.get_current_scope()
124124
+ # do something with scope
125125
```
126126

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

129129
```python diff
130-
- with configure_scope() as scope:
130+
- with sentry_sdk.configure_scope() as scope:
131131
- # do something with scope
132-
+ scope = Scope.get_isolation_scope()
132+
+ scope = sentry_sdk.get_isolation_scope()
133133
+ # do something with scope
134134
```
135135

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

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

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

156156
```python diff
157-
- with push_scope() as scope:
157+
- with sentry_sdk.push_scope() as scope:
158158
- # do something
159-
+ with new_scope() as scope:
159+
+ with sentry_sdk.new_scope() as scope:
160160
+ # do something
161161
```
162162

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

165165
```python diff
166-
- with push_scope() as scope:
166+
- with sentry_sdk.push_scope() as scope:
167167
- # do something
168-
+ with isolation_scope() as scope:
168+
+ with sentry_sdk.isolation_scope() as scope:
169169
+ # do something
170170
```
171171

platform-includes/enriching-events/scopes/configure-scope/python.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
```python
22
import sentry_sdk
3-
from sentry_sdk.scope import Scope
43

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

0 commit comments

Comments
 (0)