8000 feat(api): Remove `sentry_sdk.configure_scope` (#3406) · getsentry/sentry-python@275189e · GitHub
[go: up one dir, main page]

Skip to content

Commit 275189e

Browse files
feat(api): Remove sentry_sdk.configure_scope (#3406)
Also, remove any tests for `sentry_sdk.configure_scope`. Since Strawberry's deprecated [Sentry tracing extensions](https://strawberry.rocks/docs/extensions/sentry-tracing) import `sentry_sdk.configure_scope`, importing `strawberry.extensions.tracing.SentryTracingExtension` (or `SentryTracingExtensionSync`) will result in an unhandled exception. Therefore, these imports, and any functionality associated with them, have also been removed. This itself is not a breaking change, as it is necessitated by the removal of `sentry_sdk.configure_scope`. BREAKING CHANGE: Remove `sentry_sdk.configure_scope`. Closes: #3402
1 parent 81cdbd4 commit 275189e

File tree

9 files changed

+2
-284
lines changed

9 files changed

+2
-284
lines changed

docs/api.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ Client Management
5151
Managing Scope (advanced)
5252
=========================
5353

54-
.. autofunction:: sentry_sdk.api.configure_scope
5554
.. autofunction:: sentry_sdk.api.push_scope
5655

5756
.. autofunction:: sentry_sdk.api.new_scope
58-

sentry_sdk/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"capture_event",
2020
"capture_exception",
2121
"capture_message",
22-
"configure_scope",
2322
"continue_trace",
2423
"flush",
2524
"get_baggage",

sentry_sdk/api.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import inspect
22
import warnings
3-
from contextlib import contextmanager
43

54
from sentry_sdk import tracing_utils, Client
65
from sentry_sdk._init_implementation import init
@@ -16,7 +15,6 @@
1615

1716
from typing import Any
1817
from typing import Dict
19-
from typing import Generator
2018
from typing import Optional
2119
from typing import overload
2220
from typing import Callable
@@ -55,7 +53,6 @@ def overload(x):
5553
"capture_event",
5654
"capture_exception",
5755
"capture_message",
58-
"configure_scope",
5956
"continue_trace",
6057
"flush",
6158
"get_baggage",
@@ -194,56 +191,6 @@ def add_breadcrumb(
194191
return get_isolation_scope().add_breadcrumb(crumb, hint, **kwargs)
195192

196193

197-
@overload
198-
def configure_scope():
199-
# type: () -> ContextManager[Scope]
200-
pass
201-
202-
203-
@overload
204-
def configure_scope( # noqa: F811
205-
callback, # type: Callable[[Scope], None]
206-
):
207-
# type: (...) -> None
208-
pass
209-
210-
211-
def configure_scope( # noqa: F811
212-
callback=None, # type: Optional[Callable[[Scope], None]]
213-
):
214-
# type: (...) -> Optional[ContextManager[Scope]]
215-
"""
216-
Reconfigures the scope.
217-
218-
:param callback: If provided, call the callback with the current scope.
219-
220-
:returns: If no callback is provided, returns a context manager that returns the scope.
221-
"""
222-
warnings.warn(
223-
"sentry_sdk.configure_scope is deprecated and will be removed in the next major version. "
224-
"Please consult our migration guide to learn how to migrate to the new API: "
225-
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#scope-configuring",
226-
DeprecationWarning,
227-
stacklevel=2,
228-
)
229-
230-
scope = get_isolation_scope()
231-
scope.generate_propagation_context()
232-
233-
if callback is not None:
234-
# TODO: used to return None when client is None. Check if this changes behavior.
235-
callback(scope)
236-
237-
return None
238-
239-
@contextmanager
240-
def inner():
241-
# type: () -> Generator[Scope, None, None]
242-
yield scope
243-
244-
return inner()
245-
246-
247194
@overload
248195
def push_scope():
249196
# type: () -> ContextManager[Scope]

sentry_sdk/integrations/strawberry.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
from strawberry import Schema
3232
from strawberry.extensions import SchemaExtension # type: ignore
3333
from strawberry.extensions.tracing.utils import should_skip_tracing as strawberry_should_skip_tracing # type: ignore
34-
from strawberry.extensions.tracing import ( # type: ignore
35-
SentryTracingExtension as StrawberrySentryAsyncExtension,
36-
SentryTracingExtensionSync as StrawberrySentrySyncExtension,
37-
)
3834
from strawberry.http import async_base_view, sync_base_view # type: ignore
3935
except ImportError:
4036
raise DidNotEnable("strawberry-graphql is not installed")
@@ -104,14 +100,6 @@ def _sentry_patched_schema_init(self, *args, **kwargs):
104100
"False" if should_use_async_extension else "True",
105101
)
106102

107-
# remove the built in strawberry sentry extension, if present
108-
extensions = [
109-
extension
110-
for extension in extensions
111-
if extension
112-
not in (StrawberrySentryAsyncExtension, StrawberrySentrySyncExtension)
113-
]
114-
115103
# add our extension
116104
extensions.append(
117105
SentryAsyncExtension if should_use_async_extension else SentrySyncExtension
@@ -412,11 +400,6 @@ def inner(event, hint):
412400

413401
def _guess_if_using_async(extensions):
414402
# type: (List[SchemaExtension]) -> bool
415-
if StrawberrySentryAsyncExtension in extensions:
416-
return True
417-
elif StrawberrySentrySyncExtension in extensions:
418-
return False
419-
420403
return bool(
421404
{"starlette", "starlite", "litestar", "fastapi"} & set(_get_installed_modules())
422405
)

tests/integrations/strawberry/test_strawberry.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
from fastapi import FastAPI
1111
from fastapi.testclient import TestClient
1212
from flask import Flask
13-
from strawberry.extensions.tracing import (
14-
SentryTracingExtension,
15-
SentryTracingExtensionSync,
16-
)
1713
from strawberry.fastapi import GraphQLRouter
1814
from strawberry.flask.views import GraphQLView
1915

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

145141

146-
def test_replace_existing_sentry_async_extension(sentry_init):
147-
sentry_init(integrations=[StrawberryIntegration()])
148-
149-
schema = strawberry.Schema(Query, extensions=[SentryTracingExtension])
150-
assert SentryTracingExtension not in schema.extensions
151-
assert SentrySyncExtension not in schema.extensions
152-
assert SentryAsyncExtension in schema.extensions
153-
154-
155-
def test_replace_existing_sentry_sync_extension(sentry_init):
156-
sentry_init(integrations=[StrawberryIntegration()])
157-
158-
schema = strawberry.Schema(Query, extensions=[SentryTracingExtensionSync])
159-
assert SentryTracingExtensionSync not in schema.extensions
160-
assert SentryAsyncExtension not in schema.extensions
161-
assert SentrySyncExtension in schema.extensions
162-
163-
164142
@parameterize_strawberry_test
165143
def test_capture_request_if_available_and_send_pii_is_on(
166144
request,

tests/new_scopes_compat/test_new_scopes_compat.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,6 @@
1111
"""
1212

1313

14-
def test_configure_scope_sdk1(sentry_init, capture_events):
15-
"""
16-
Mutate data in a `with configure_scope` block.
17-
18-
Checks the results of SDK 2.x against the results the same code returned in SDK 1.x.
19-
"""
20-
sentry_init()
21-
22-
events = capture_events()
23-
24-
sentry_sdk.set_tag("A", 1)
25-
sentry_sdk.capture_message("Event A")
26-
27-
with sentry_sdk.configure_scope() as scope: # configure scope
28-
sentry_sdk.set_tag("B1", 1)
29-
scope.set_tag("B2", 1)
30-
sentry_sdk.capture_message("Event B")
31-
32-
sentry_sdk.set_tag("Z", 1)
33-
sentry_sdk.capture_message("Event Z")
34-
35-
(event_a, event_b, event_z) = events
36-
37-
# Check against the results the same code returned in SDK 1.x
38-
assert event_a["tags"] == {"A": 1}
39-
assert event_b["tags"] == {"A": 1, "B1": 1, "B2": 1}
40-
assert event_z["tags"] == {"A": 1, "B1": 1, "B2": 1, "Z": 1}
41-
42-
4314
def test_push_scope_sdk1(sentry_init, capture_events):
4415
"""
4516
Mutate data in a `with push_scope` block

tests/new_scopes_compat/test_new_scopes_compat_event.py

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -335,71 +335,6 @@ def test_event(sentry_init, capture_envelopes, expected_error, expected_transact
335335

336336
envelopes = capture_envelopes()
337337

338-
with sentry_sdk.start_transaction(
339-
name="test_transaction", op="test_transaction_op"
340-
) as trx:
341-
with sentry_sdk.start_span(op="test_span") as span:
342-
with sentry_sdk.configure_scope() as scope: # configure scope
343-
_generate_event_data(scope)
344-
_faulty_function()
345-
346-
(error_envelope, transaction_envelope) = envelopes
347-
348-
error = error_envelope.get_event()
349-
transaction = transaction_envelope.get_transaction_event()
350-
attachment = error_envelope.items[-1]
351-
352-
assert error == expected_error(trx, span)
353-
assert transaction == expected_transaction(trx, span)
354-
assert attachment.headers == {
355-
"filename": "hello.txt",
356-
"type": "attachment",
357-
"content_type": "text/plain",
358-
}
359-
assert attachment.payload.bytes == b"Hello World"
360-
361< 10000 span class="diff-text-marker">-
362-
def test_event2(sentry_init, capture_envelopes, expected_error, expected_transaction):
363-
_init_sentry_sdk(sentry_init)
364-
365-
envelopes = capture_envelopes()
366-
367-
with Hub(Hub.current):
368-
sentry_sdk.set_tag("A", 1) # will not be added
369-
370-
with Hub.current: # with hub
371-
with sentry_sdk.push_scope() as scope:
372-
scope.set_tag("B", 1) # will not be added
373-
374-
with sentry_sdk.start_transaction(
375-
name="test_transaction", op="test_transaction_op"
376-
) as trx:
377-
with sentry_sdk.start_span(op="test_span") as span:
378-
with sentry_sdk.configure_scope() as scope: # configure scope
379-
_generate_event_data(scope)
380-
_faulty_function()
381-
382-
(error_envelope, transaction_envelope) = envelopes
383-
384-
error = error_envelope.get_event()
385-
transaction = transaction_envelope.get_transaction_event()
386-
attachment = error_envelope.items[-1]
387-
388-
assert error == expected_error(trx, span)
389-
assert transaction == expected_transaction(trx, span)
390-
assert attachment.headers == {
391-
"filename": "hello.txt",
392-
"type": "attachment",
393-
"content_type": "text/plain",
394-
}
395-
assert attachment.payload.bytes == b"Hello World"
396-
397-
398-
def test_event3(sentry_init, capture_envelopes, expected_error, expected_transaction):
399-
_init_sentry_sdk(sentry_init)
400-
401-
envelopes = capture_envelopes()
402-
403338
with Hub(Hub.current):
404339
sentry_sdk.set_tag("A", 1) # will not be added
405340

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

433368

434-
def test_event4(sentry_init, capture_envelopes, expected_error, expected_transaction):
435-
_init_sentry_sdk(sentry_init)
436-
437-
envelopes = capture_envelopes()
438-
439-
with Hub(Hub.current):
440-
sentry_sdk.set_tag("A", 1) # will not be added
441-
442-
with Hub(Hub.current): # with hub clone
443-
with sentry_sdk.push_scope() as scope:
444-
scope.set_tag("B", 1) # will not be added
445-
446-
with sentry_sdk.start_transaction(
447-
name="test_transaction", op="test_transaction_op"
448-
) as trx:
449-
with sentry_sdk.start_span(op="test_span") as span:
450-
with sentry_sdk.configure_scope() as scope: # configure scope
451-
_generate_event_data(scope)
452-
_faulty_function()
453-
454-
(error_envelope, transaction_envelope) = envelopes
455-
456-
error = error_envelope.get_event()
457-
transaction = transaction_envelope.get_transaction_event()
458-
attachment = error_envelope.items[-1]
459-
460-
assert error == expected_error(trx, span)
461-
assert transaction == expected_transaction(trx, span)
462-
assert attachment.headers == {
463-
"filename": "hello.txt",
464-
"type": "attachment",
465-
"content_type": "text/plain",
466-
}
467-
assert attachment.payload.bytes == b"Hello World"
468-
469-
470-
def test_event5(sentry_init, capture_envelopes, expected_error, expected_transaction):
369+
def test_event2(sentry_init, capture_envelopes, expected_error, expected_transaction):
471370
_init_sentry_sdk(sentry_init)
472371

473372
envelopes = capture_envelopes()

tests/test_api.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
is_initialized,
1212
start_transaction,
1313
set_tags,
14-
configure_scope,
1514
push_scope,
1615
get_global_scope,
1716
get_current_scope,
@@ -185,12 +184,6 @@ def test_set_tags(sentry_init, capture_events):
185184
}, "Updating tags with empty dict changed tags"
186185

187186

188-
def test_configure_scope_deprecation():
189-
with pytest.warns(DeprecationWarning):
190-
with configure_scope():
191-
...
192-
193-
194187
def test_push_scope_deprecation():
195188
with pytest.warns(DeprecationWarning):
196189
with push_scope():

0 commit comments

Comments
 (0)
0