Skip to content

Commit bd293e5

Browse files
Expose the scope getters to top level API and use them everywhere (#3357)
* Expose the scope getters to top level API and use them everywhere * Going forward, we might have 2 different scope implementations so we can't have the `Scope` class being called everywhere directly since this will be abstracted away. * Update CHANGELOG.md Co-authored-by: Ivana Kellyer <[email protected]> * remove Scope._capture_internal_exception * review fixes * remove staticmethod * Fix sphinx circular import bs --------- Co-authored-by: Ivana Kellyer <[email protected]>
1 parent c9765cd commit bd293e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+433
-412
lines changed

CHANGELOG.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
```python
1616
import sentry_sdk
1717
from sentry_sdk.integrations.flask import FlaskIntegration
18-
18+
1919
sentry_sdk.init(
2020
# Do not use the Flask integration even if Flask is installed.
2121
disabled_integrations=[
@@ -68,7 +68,7 @@
6868
LangchainIntegration(tiktoken_encoding_name="cl100k_base"),
6969
],
7070
)
71-
```
71+
```
7272

7373
- PyMongo: Send query description as valid JSON (#3291) by @0Calories
7474
- Remove Python 2 compatibility code (#3284) by @szokeasaurusrex
@@ -183,7 +183,7 @@ This change fixes a regression in our cron monitoring feature, which caused cron
183183
```python
184184
from sentry_sdk.integrations.starlette import StarletteIntegration
185185
from sentry_sdk.integrations.fastapi import FastApiIntegration
186-
186+
187187
sentry_sdk.init(
188188
# ...
189189
integrations=[
@@ -312,9 +312,9 @@ This change fixes a regression in our cron monitoring feature, which caused cron
312312
integrations=[AnthropicIntegration()],
313313
)
314314

315-
client = Anthropic()
315+
client = Anthropic()
316316
```
317-
Check out [the Anthropic docs](https://docs.sentry.io/platforms/python/integrations/anthropic/) for details.
317+
Check out [the Anthropic docs](https://docs.sentry.io/platforms/python/integrations/anthropic/) for details.
318318

319319
- **New integration:** [Huggingface Hub](https://docs.sentry.io/platforms/python/integrations/huggingface/) (#3033) by @colin-sentry
320320

@@ -369,13 +369,13 @@ This change fixes a regression in our cron monitoring feature, which caused cron
369369

370370
## 2.0.0
371371

372-
This is the first major update in a *long* time!
372+
This is the first major update in a *long* time!
373373

374374
We dropped support for some ancient languages and frameworks (Yes, Python 2.7 is no longer supported). Additionally we refactored a big part of the foundation of the SDK (how data inside the SDK is handled).
375375

376376
We hope you like it!
377377

378-
For a shorter version of what you need to do, to upgrade to Sentry SDK 2.0 see: https://docs.sentry.io/platforms/python/migration/1.x-to-2.x
378+
For a shorter version of what you need to do, to upgrade to Sentry SDK 2.0 see: https://docs.sentry.io/platforms/python/migration/1.x-to-2.x
379379

380380
### New Features
381381

@@ -415,7 +415,7 @@ For a shorter version of what you need to do, to upgrade to Sentry SDK 2.0 see:
415415

416416
# later in the code execution:
417417

418-
scope = sentry_sdk.Scope.get_current_scope()
418+
scope = sentry_sdk.get_current_scope()
419419
scope.set_transaction_name("new-transaction-name")
420420
```
421421
- The classes listed in the table below are now abstract base classes. Therefore, they can no longer be instantiated. Subclasses can only be instantiated if they implement all of the abstract methods.
@@ -492,7 +492,7 @@ For a shorter version of what you need to do, to upgrade to Sentry SDK 2.0 see:
492492
# do something with the forked scope
493493
```
494494

495-
- `configure_scope` is deprecated. Use the new isolation scope directly via `Scope.get_isolation_scope()` instead.
495+
- `configure_scope` is deprecated. Use the new isolation scope directly via `get_isolation_scope()` instead.
496496

497497
Before:
498498

@@ -504,9 +504,9 @@ For a shorter version of what you need to do, to upgrade to Sentry SDK 2.0 see:
504504
After:
505505

506506
```python
507-
from sentry_sdk.scope import Scope
507+
from sentry_sdk import get_isolation_scope
508508

509-
scope = Scope.get_isolation_scope()
509+
scope = get_isolation_scope()
510510
# do something with `scope`
511511
```
512512

@@ -563,7 +563,7 @@ This is the final 1.x release for the forseeable future. Development will contin
563563
"failure_issue_threshold": 5,
564564
"recovery_threshold": 5,
565565
}
566-
566+
567567
@monitor(monitor_slug='<monitor-slug>', monitor_config=monitor_config)
568568
def tell_the_world():
569569
print('My scheduled task...')
@@ -578,14 +578,14 @@ This is the final 1.x release for the forseeable future. Development will contin
578578
```python
579579
import django.db.models.signals
580580
import sentry_sdk
581-
581+
582582
sentry_sdk.init(
583583
...
584584
integrations=[
585585
DjangoIntegration(
586586
...
587587
signals_denylist=[
588-
django.db.models.signals.pre_init,
588+
django.db.models.signals.pre_init,
589589
django.db.models.signals.post_init,
590590
],
591591
),
@@ -608,7 +608,7 @@ This is the final 1.x release for the forseeable future. Development will contin
608608
tags["extra"] = "foo"
609609
del tags["release"]
610610
return True
611-
611+
612612
sentry_sdk.init(
613613
...
614614
_experiments={

MIGRATION_GUIDE.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh
4242

4343
# later in the code execution:
4444

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

@@ -132,18 +132,18 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh
132132
After:
133133

134134
```python
135-
from sentry_sdk.scope import Scope
135+
from sentry_sdk import get_current_scope
136136

137-
scope = Scope.get_current_scope()
137+
scope = get_current_scope()
138138
# do something with `scope`
139139
```
140140

141141
Or:
142142

143143
```python
144-
from sentry_sdk.scope import Scope
144+
from sentry_sdk import get_isolation_scope
145145

146-
scope = Scope.get_isolation_scope()
146+
scope = get_isolation_scope()
147147
# do something with `scope`
148148
```
149149

sentry_sdk/__init__.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
from sentry_sdk.hub import Hub
21
from sentry_sdk.scope import Scope
32
from sentry_sdk.transport import Transport, HttpTransport
43
from sentry_sdk.client import Client
5-
from sentry_sdk._init_implementation import init
64

75
from sentry_sdk.api import * # noqa
86

97
from sentry_sdk.consts import VERSION # noqa
108

11-
from sentry_sdk.crons import monitor # noqa
12-
from sentry_sdk.tracing import trace # noqa
13-
149
__all__ = [ # noqa
1510
"Hub",
1611
"Scope",
1712
"Client",
1813
"Transport",
1914
"HttpTransport",
20-
"init",
2115
"integrations",
22-
"trace",
2316
# From sentry_sdk.api
17+
"init",
2418
"add_breadcrumb",
2519
"capture_event",
2620
"capture_exception",
@@ -30,6 +24,9 @@
3024
"flush",
3125
"get_baggage",
3226
"get_client",
27+
"get_global_scope",
28+
"get_isolation_scope",
29+
"get_current_scope",
3330
"get_current_span",
3431
"get_traceparent",
3532
"is_initialized",
@@ -46,10 +43,15 @@
4643
"set_user",
4744
"start_span",
4845
"start_transaction",
46+
"trace",
47+
"monitor",
4948
]
5049

5150
# Initialize the debug support after everything is loaded
5251
from sentry_sdk.debug import init_debug_support
5352

5453
init_debug_support()
5554
del init_debug_support
55+
56+
# circular imports
57+
from sentry_sdk.hub import Hub

sentry_sdk/_init_implementation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _init(*args, **kwargs):
3939
This takes the same arguments as the client constructor.
4040
"""
4141
client = sentry_sdk.Client(*args, **kwargs)
42-
sentry_sdk.Scope.get_global_scope().set_client(client)
42+
sentry_sdk.get_global_scope().set_client(client)
4343
_check_python_deprecations()
4444
rv = _InitGuard(client)
4545
return rv

0 commit comments

Comments
 (0)