Skip to content

test(sessions): Replace push_scope #3354

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 5 commits into from
Jul 26, 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
9 changes: 9 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import warnings
from contextlib import contextmanager

from sentry_sdk import tracing_utils, Client
Expand Down Expand Up @@ -185,6 +186,14 @@ def configure_scope( # noqa: F811

:returns: If no callback is provided, returns a context manager that returns the scope.
"""
warnings.warn(
"sentry_sdk.configure_scope is deprecated and will be removed in the next major version. "
"Please consult our migration guide to learn how to migrate to the new API: "
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#scope-configuring",
DeprecationWarning,
stacklevel=2,
)

scope = Scope.get_isolation_scope()
scope.generate_propagation_context()

Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
is_initialized,
start_transaction,
set_tags,
configure_scope,
)

from sentry_sdk.client import Client, NonRecordingClient
Expand Down Expand Up @@ -179,3 +180,9 @@ def test_set_tags(sentry_init, capture_events):
"tag2": "updated",
"tag3": "new",
}, "Updating tags with empty dict changed tags"


def test_configure_scope_deprecation():
with pytest.warns(DeprecationWarning):
with configure_scope():
...
25 changes: 11 additions & 14 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
from sentry_sdk import (
get_client,
push_scope,
configure_scope,
capture_event,
capture_exception,
capture_message,
start_transaction,
last_event_id,
add_breadcrumb,
isolation_scope,
new_scope,
Hub,
Scope,
)
Expand Down Expand Up @@ -74,13 +74,11 @@ def test_processors(sentry_init, capture_events):
sentry_init()
events = capture_events()

with configure_scope() as scope:

def error_processor(event, exc_info):
event["exception"]["values"][0]["value"] += " whatever"
return event
def error_processor(event, exc_info):
event["exception"]["values"][0]["value"] += " whatever"
return event

scope.add_error_processor(error_processor, ValueError)
Scope.get_isolation_scope().add_error_processor(error_processor, ValueError)

try:
raise ValueError("aha!")
Expand Down Expand Up @@ -432,9 +430,9 @@ def test_attachments(sentry_init, capture_envelopes):

this_file = os.path.abspath(__file__.rstrip("c"))

with configure_scope() as scope:
scope.add_attachment(bytes=b"Hello World!", filename="message.txt")
scope.add_attachment(path=this_file)
scope = Scope.get_isolation_scope()
scope.add_attachment(bytes=b"Hello World!", filename="message.txt")
scope.add_attachment(path=this_file)

capture_exception(ValueError())

Expand Down Expand Up @@ -466,8 +464,7 @@ def test_attachments_graceful_failure(
sentry_init()
envelopes = capture_envelopes()

with configure_scope() as scope:
scope.add_attachment(path="non_existent")
Scope.get_isolation_scope().add_attachment(path="non_existent")
capture_exception(ValueError())

(envelope,) = envelopes
Expand Down Expand Up @@ -610,14 +607,14 @@ def before_send(event, hint):
sentry_init(debug=True, before_send=before_send)
events = capture_events()

with push_scope() as scope:
with new_scope() as scope:

@scope.add_event_processor
def foo(event, hint):
event["message"] += "foo"
return event

with push_scope() as scope:
with new_scope() as scope:

@scope.add_event_processor
def bar(event, hint):
Expand Down
17 changes: 9 additions & 8 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ def capture_envelope(self, envelope):
assert output.count(b"HI") == num_messages


def test_configure_scope_available(sentry_init, request, monkeypatch):
def test_configure_scope_available(
sentry_init, request, monkeypatch, suppress_deprecation_warnings
):
"""
Test that scope is configured if client is configured

Expand Down Expand Up @@ -686,14 +688,13 @@ def test_cyclic_data(sentry_init, capture_events):
sentry_init()
events = capture_events()

with configure_scope() as scope:
data = {}
data["is_cyclic"] = data
data = {}
data["is_cyclic"] = data

other_data = ""
data["not_cyclic"] = other_data
data["not_cyclic2"] = other_data
scope.set_extra("foo", data)
other_data = ""
data["not_cyclic"] = other_data
data["not_cyclic2"] = other_data
sentry_sdk.Scope.get_isolation_scope().set_extra("foo", data)

capture_message("hi")
(event,) = events
Expand Down
7 changes: 3 additions & 4 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def test_aggregates(sentry_init, capture_envelopes):
envelopes = capture_envelopes()

with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
with sentry_sdk.new_scope() as scope:
try:
scope = sentry_sdk.Scope.get_current_scope()
scope.set_user({"id": "42"})
raise Exception("all is wrong")
except Exception:
Expand Down Expand Up @@ -92,7 +91,7 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode(
envelopes = capture_envelopes()

with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
with sentry_sdk.new_scope():
try:
raise Exception("all is wrong")
except Exception:
Expand Down Expand Up @@ -127,7 +126,7 @@ def test_no_thread_on_shutdown_no_errors(sentry_init):
side_effect=RuntimeError("can't create new thread at interpreter shutdown"),
):
with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
with sentry_sdk.new_scope():
try:
raise Exception("all is wrong")
except Exception:
Expand Down
Loading