Skip to content

Commit b978efe

Browse files
authored
fix(python): Fix new_scope example (#14394)
The example for `new_scope` didn't actually use `new_scope`.
1 parent 336af5e commit b978efe

File tree

2 files changed

+17
-15
lines changed
  • docs/platforms/python/enriching-events/scopes
  • platform-includes/enriching-events/scopes/configure-scope

2 files changed

+17
-15
lines changed

docs/platforms/python/enriching-events/scopes/index.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ However, if your use case requires direct access to the scope object, you can us
2727

2828
<Alert>
2929

30-
Avoid calling top-level APIs inside the <PlatformIdentifier name="new-scope" /> context manager. The top-level API might interact with a different scope from what <PlatformIdentifier name="new-scope" /> yields, causing unintended results. While within the <PlatformIdentifier name="new-scope" /> context manager, please call methods directly on the scope that <PlatformIdentifier name="new-scope" /> yields!
30+
Avoid calling top-level APIs inside the <PlatformIdentifier name="new-scope" /> context manager. The top-level API might interact with a different scope from what <PlatformIdentifier name="new-scope" /> yields, causing unintended results. While within the <PlatformIdentifier name="new-scope" /> context manager, please call methods directly on the scope that <PlatformIdentifier name="new-scope" /> yields!
3131

3232
</Alert>
3333

34-
Using <PlatformIdentifier name="new-scope" /> allows you to attach additional information, such as adding custom tags or informing Sentry about the currently authenticated user.
34+
Using <PlatformIdentifier name="new-scope" /> allows you to attach additional information, such as adding custom tags.
3535

3636
<PlatformContent includePath="enriching-events/scopes/configure-scope" />
3737

38-
You can also apply this configuration when unsetting a user at logout:
39-
40-
<PlatformContent includePath="enriching-events/unset-user" />
41-
4238
To learn what useful information can be associated with scopes see
4339
[the context documentation](../context/).
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
```python
22
import sentry_sdk
33

4-
scope = sentry_sdk.get_current_scope()
5-
scope.set_tag("my-tag", "my value")
6-
scope.user = {"id": 42, "email": "[email protected]"}
4+
sentry_sdk.init(...)
75

8-
# reset all scope data
9-
scope.clear()
6+
with sentry_sdk.new_scope() as scope:
7+
scope.set_tag("inside-new-scope", "yes")
8+
try:
9+
raise RuntimeError("With new_scope")
10+
except:
11+
# This exception will have the tag set
12+
sentry_sdk.capture_exception()
1013

11-
# Or:
12-
13-
sentry_sdk.set_tag("my-tag", "my value")
14-
sentry_sdk.set_user({"id": 42, "email": "[email protected]"})
14+
# The scope is automatically cleared and the previous scope
15+
# is restored after the `with` block
16+
try:
17+
raise RuntimeError("Outside of new_scope")
18+
except:
19+
# This exception won't have the custom tag set
20+
sentry_sdk.capture_exception()
1521
```

0 commit comments

Comments
 (0)