Skip to content
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

feat(api): Add top-level API for getting scopes #2988

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 5 additions & 5 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh

# later in the code execution:

scope = sentry_sdk.Scope.get_current_scope()
scope = sentry_sdk.get_current_scope()
scope.set_transaction_name("new-transaction-name")
```
- 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.
Expand Down Expand Up @@ -130,18 +130,18 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh
After:

```python
from sentry_sdk.scope import Scope
import sentry_sdk

scope = Scope.get_current_scope()
scope = sentry_sdk.get_current_scope()
# do something with `scope`
```

Or:

```python
from sentry_sdk.scope import Scope
import sentry_sdk

scope = Scope.get_isolation_scope()
scope = sentry_sdk.get_isolation_scope()
# do something with `scope`
```

Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"flush",
"get_baggage",
"get_client",
"get_current_scope",
"get_current_span",
"get_global_scope",
"get_isolation_scope",
"get_traceparent",
"is_initialized",
"isolation_scope",
Expand Down
21 changes: 21 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def overload(x):
"flush",
"get_baggage",
"get_client",
"get_current_scope",
"get_current_span",
"get_global_scope",
"get_isolation_scope",
"get_traceparent",
"is_initialized",
"isolation_scope",
Expand Down Expand Up @@ -366,3 +369,21 @@ def continue_trace(environ_or_headers, op=None, name=None, source=None):
return Scope.get_isolation_scope().continue_trace(
environ_or_headers, op, name, source
)


def get_global_scope():
# type: () -> Scope
"""Returns the global scope."""
return Scope.get_global_scope()


def get_isolation_scope():
# type: () -> Scope
"""Returns the currently active isolation scope."""
return Scope.get_isolation_scope()


def get_current_scope():
# type: () -> Scope
"""Returns the currently active current scope."""
return Scope.get_current_scope()
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
continue_trace,
get_baggage,
get_client,
get_current_scope,
get_current_span,
get_global_scope,
get_isolation_scope,
get_traceparent,
is_initialized,
start_transaction,
Expand Down Expand Up @@ -135,3 +138,9 @@ def test_get_client():
assert client is not None
assert client.__class__ == NonRecordingClient
assert not client.is_active()


def test_get_scope():
assert get_current_scope() == Scope.get_current_scope()
assert get_isolation_scope() == Scope.get_isolation_scope()
assert get_global_scope() == Scope.get_global_scope()
Loading