Skip to content

feat(api): Remove sentry_sdk.configure_scope #3406

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 1 commit into from
Aug 7, 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
2 changes: 0 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ Client Management
Managing Scope (advanced)
=========================

.. autofunction:: sentry_sdk.api.configure_scope
.. autofunction:: sentry_sdk.api.push_scope

.. autofunction:: sentry_sdk.api.new_scope

1 change: 0 additions & 1 deletion sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"capture_event",
"capture_exception",
"capture_message",
"configure_scope",
"continue_trace",
"flush",
"get_baggage",
Expand Down
53 changes: 0 additions & 53 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
import warnings
from contextlib import contextmanager

from sentry_sdk import tracing_utils, Client
from sentry_sdk._init_implementation import init
Expand All @@ -16,7 +15,6 @@

from typing import Any
from typing import Dict
from typing import Generator
from typing import Optional
from typing import overload
from typing import Callable
Expand Down Expand Up @@ -55,7 +53,6 @@ def overload(x):
"capture_event",
"capture_exception",
"capture_message",
"configure_scope",
"continue_trace",
"flush",
"get_baggage",
Expand Down Expand Up @@ -194,56 +191,6 @@ def add_breadcrumb(
return get_isolation_scope().add_breadcrumb(crumb, hint, **kwargs)


@overload
def configure_scope():
# type: () -> ContextManager[Scope]
pass


@overload
def configure_scope( # noqa: F811
callback, # type: Callable[[Scope], None]
):
# type: (...) -> None
pass


def configure_scope( # noqa: F811
callback=None, # type: Optional[Callable[[Scope], None]]
):
# type: (...) -> Optional[ContextManager[Scope]]
"""
Reconfigures the scope.

:param callback: If provided, call the callback with the current scope.

: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 = get_isolation_scope()
scope.generate_propagation_context()

if callback is not None:
# TODO: used to return None when client is None. Check if this changes behavior.
callback(scope)

return None

@contextmanager
def inner():
# type: () -> Generator[Scope, None, None]
yield scope

return inner()


@overload
def push_scope():
# type: () -> ContextManager[Scope]
Expand Down
17 changes: 0 additions & 17 deletions sentry_sdk/integrations/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
from strawberry import Schema
from strawberry.extensions import SchemaExtension # type: ignore
from strawberry.extensions.tracing.utils import should_skip_tracing as strawberry_should_skip_tracing # type: ignore
from strawberry.extensions.tracing import ( # type: ignore
SentryTracingExtension as StrawberrySentryAsyncExtension,
SentryTracingExtensionSync as StrawberrySentrySyncExtension,
)
from strawberry.http import async_base_view, sync_base_view # type: ignore
except ImportError:
raise DidNotEnable("strawberry-graphql is not installed")
Expand Down Expand Up @@ -104,14 +100,6 @@ def _sentry_patched_schema_init(self, *args, **kwargs):
"False" if should_use_async_extension else "True",
)

# remove the built in strawberry sentry extension, if present
extensions = [
extension
for extension in extensions
if extension
not in (StrawberrySentryAsyncExtension, StrawberrySentrySyncExtension)
]

# add our extension
extensions.append(
SentryAsyncExtension if should_use_async_extension else SentrySyncExtension
Expand Down Expand Up @@ -412,11 +400,6 @@ def inner(event, hint):

def _guess_if_using_async(extensions):
# type: (List[SchemaExtension]) -> bool
if StrawberrySentryAsyncExtension in extensions:
return True
elif StrawberrySentrySyncExtension in extensions:
return False

return bool(
{"starlette", "starlite", "litestar", "fastapi"} & set(_get_installed_modules())
)
22 changes: 0 additions & 22 deletions tests/integrations/strawberry/test_strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient
from flask import Flask
from strawberry.extensions.tracing import (
SentryTracingExtension,
SentryTracingExtensionSync,
)
from strawberry.fastapi import GraphQLRouter
from strawberry.flask.views import GraphQLView

Expand Down Expand Up @@ -143,24 +139,6 @@ def test_infer_execution_type_from_installed_packages_sync(sentry_init):
assert SentrySyncExtension in schema.extensions


def test_replace_existing_sentry_async_extension(sentry_init):
sentry_init(integrations=[StrawberryIntegration()])

schema = strawberry.Schema(Query, extensions=[SentryTracingExtension])
assert SentryTracingExtension not in schema.extensions
assert SentrySyncExtension not in schema.extensions
assert SentryAsyncExtension in schema.extensions


def test_replace_existing_sentry_sync_extension(sentry_init):
sentry_init(integrations=[StrawberryIntegration()])

schema = strawberry.Schema(Query, extensions=[SentryTracingExtensionSync])
assert SentryTracingExtensionSync not in schema.extensions
assert SentryAsyncExtension not in schema.extensions
assert SentrySyncExtension in schema.extensions


@parameterize_strawberry_test
def test_capture_request_if_available_and_send_pii_is_on(
request,
Expand Down
29 changes: 0 additions & 29 deletions tests/new_scopes_compat/test_new_scopes_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,6 @@
"""


def test_configure_scope_sdk1(sentry_init, capture_events):
"""
Mutate data in a `with configure_scope` block.

Checks the results of SDK 2.x against the results the same code returned in SDK 1.x.
"""
sentry_init()

events = capture_events()

sentry_sdk.set_tag("A", 1)
sentry_sdk.capture_message("Event A")

with sentry_sdk.configure_scope() as scope: # configure scope
sentry_sdk.set_tag("B1", 1)
scope.set_tag("B2", 1)
sentry_sdk.capture_message("Event B")

sentry_sdk.set_tag("Z", 1)
sentry_sdk.capture_message("Event Z")

(event_a, event_b, event_z) = events

# Check against the results the same code returned in SDK 1.x
assert event_a["tags"] == {"A": 1}
assert event_b["tags"] == {"A": 1, "B1": 1, "B2": 1}
assert event_z["tags"] == {"A": 1, "B1": 1, "B2": 1, "Z": 1}


def test_push_scope_sdk1(sentry_init, capture_events):
"""
Mutate data in a `with push_scope` block
Expand Down
103 changes: 1 addition & 102 deletions tests/new_scopes_compat/test_new_scopes_compat_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,71 +335,6 @@ def test_event(sentry_init, capture_envelopes, expected_error, expected_transact

envelopes = capture_envelopes()

with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()

(error_envelope, transaction_envelope) = envelopes

error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]

assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"


def test_event2(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)

envelopes = capture_envelopes()

with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added

with Hub.current: # with hub
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added

with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()

(error_envelope, transaction_envelope) = envelopes

error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]

assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"


def test_event3(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)

envelopes = capture_envelopes()

with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added

Expand Down Expand Up @@ -431,43 +366,7 @@ def test_event3(sentry_init, capture_envelopes, expected_error, expected_transac
assert attachment.payload.bytes == b"Hello World"


def test_event4(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)

envelopes = capture_envelopes()

with Hub(Hub.current):
sentry_sdk.set_tag("A", 1) # will not be added

with Hub(Hub.current): # with hub clone
with sentry_sdk.push_scope() as scope:
scope.set_tag("B", 1) # will not be added

with sentry_sdk.start_transaction(
name="test_transaction", op="test_transaction_op"
) as trx:
with sentry_sdk.start_span(op="test_span") as span:
with sentry_sdk.configure_scope() as scope: # configure scope
_generate_event_data(scope)
_faulty_function()

(error_envelope, transaction_envelope) = envelopes

error = error_envelope.get_event()
transaction = transaction_envelope.get_transaction_event()
attachment = error_envelope.items[-1]

assert error == expected_error(trx, span)
assert transaction == expected_transaction(trx, span)
assert attachment.headers == {
"filename": "hello.txt",
"type": "attachment",
"content_type": "text/plain",
}
assert attachment.payload.bytes == b"Hello World"


def test_event5(sentry_init, capture_envelopes, expected_error, expected_transaction):
def test_event2(sentry_init, capture_envelopes, expected_error, expected_transaction):
_init_sentry_sdk(sentry_init)

envelopes = capture_envelopes()
Expand Down
7 changes: 0 additions & 7 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
is_initialized,
start_transaction,
set_tags,
configure_scope,
push_scope,
get_global_scope,
get_current_scope,
Expand Down Expand Up @@ -185,12 +184,6 @@ def test_set_tags(sentry_init, capture_events):
}, "Updating tags with empty dict changed tags"


def test_configure_scope_deprecation():
with pytest.warns(DeprecationWarning):
with configure_scope():
...


def test_push_scope_deprecation():
with pytest.warns(DeprecationWarning):
with push_scope():
Expand Down
Loading
Loading