diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 41c4814146..d28dbd92d0 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -1,4 +1,5 @@ import inspect +import warnings from contextlib import contextmanager from sentry_sdk import tracing_utils, Client @@ -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() diff --git a/tests/test_api.py b/tests/test_api.py index a6c44260d7..1f2a1b783f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -11,6 +11,7 @@ is_initialized, start_transaction, set_tags, + configure_scope, ) from sentry_sdk.client import Client, NonRecordingClient @@ -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(): + ... diff --git a/tests/test_basics.py b/tests/test_basics.py index e1e84340a5..0bec698a35 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -15,7 +15,6 @@ from sentry_sdk import ( get_client, push_scope, - configure_scope, capture_event, capture_exception, capture_message, @@ -23,6 +22,7 @@ last_event_id, add_breadcrumb, isolation_scope, + new_scope, Hub, Scope, ) @@ -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!") @@ -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()) @@ -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 @@ -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): diff --git a/tests/test_client.py b/tests/test_client.py index 571912ab12..15a140d377 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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 @@ -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 diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 989bfeadd1..cc25f71cbb 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -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: @@ -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: @@ -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: