Skip to content

Commit 43133b3

Browse files
authored
chore: Drop even more deprecated stuff (#4193)
Drop: - setting `Scope.transaction` directly - old way of setting `failed_request_status_codes` - setting the `span` argument of `Scope.trace_propagation_meta` - setting `Scope.user` directly
1 parent 445ebc9 commit 43133b3

File tree

7 files changed

+15
-227
lines changed

7 files changed

+15
-227
lines changed

MIGRATION_GUIDE.md

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
157157
- `profiles_sample_rate` and `profiler_mode` were removed from options available via `_experiments`. Use the top-level `profiles_sample_rate` and `profiler_mode` options instead.
158158
- `Transport.capture_event` has been removed. Use `Transport.capture_envelope` instead.
159159
- Function transports are no longer supported. Subclass the `Transport` instead.
160+
- Setting `Scope.transaction` directly is no longer supported. Use `Scope.set_transaction_name()` instead.
161+
- Passing a list or `None` for `failed_request_status_codes` in the Starlette integration is no longer supported. Pass a set of integers instead.
162+
- The `span` argument of `Scope.trace_propagation_meta` is no longer supported.
163+
- Setting `Scope.user` directly is no longer supported. Use `Scope.set_user()` instead.
160164
- `start_transaction` (`start_span`) no longer takes a `baggage` argument. Use the `continue_trace()` context manager instead to propagate baggage.
161165

162166
### Deprecated

sentry_sdk/integrations/_wsgi_common.py

+2-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sentry_sdk
55
from sentry_sdk.scope import should_send_default_pii
6-
from sentry_sdk.utils import AnnotatedValue, logger, SENSITIVE_DATA_SUBSTITUTE
6+
from sentry_sdk.utils import AnnotatedValue, SENSITIVE_DATA_SUBSTITUTE
77

88
try:
99
from django.http.request import RawPostDataException
@@ -19,7 +19,7 @@
1919
from typing import MutableMapping
2020
from typing import Optional
2121
from typing import Union
22-
from sentry_sdk._types import Event, HttpStatusCodeRange
22+
from sentry_sdk._types import Event
2323

2424

2525
SENSITIVE_ENV_KEYS = (
@@ -240,37 +240,3 @@ def _request_headers_to_span_attributes(headers):
240240
attributes[f"http.request.header.{header.lower()}"] = value
241241

242242
return attributes
243-
244-
245-
def _in_http_status_code_range(code, code_ranges):
246-
# type: (object, list[HttpStatusCodeRange]) -> bool
247-
for target in code_ranges:
248-
if isinstance(target, int):
249-
if code == target:
250-
return True
251-
continue
252-
253-
try:
254-
if code in target:
255-
return True
256-
except TypeError:
257-
logger.warning(
258-
"failed_request_status_codes has to be a list of integers or containers"
259-
)
260-
261-
return False
262-
263-
264-
class HttpCodeRangeContainer:
265-
"""
266-
Wrapper to make it possible to use list[HttpStatusCodeRange] as a Container[int].
267-
Used for backwards compatibility with the old `failed_request_status_codes` option.
268-
"""
269-
270-
def __init__(self, code_ranges):
271-
# type: (list[HttpStatusCodeRange]) -> None
272-
self._code_ranges = code_ranges
273-
274-
def __contains__(self, item):
275-
# type: (object) -> bool
276-
return _in_http_status_code_range(item, self._code_ranges)

sentry_sdk/integrations/django/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,13 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
415415
if hasattr(urlconf, "handler404"):
416416
handler = urlconf.handler404
417417
if isinstance(handler, str):
418-
scope.transaction = handler
418+
scope.set_transaction_name(handler)
419419
else:
420-
scope.transaction = transaction_from_function(
420+
name = transaction_from_function(
421421
getattr(handler, "view_class", handler)
422422
)
423+
if isinstance(name, str):
424+
scope.set_transaction_name(name)
423425
except Exception:
424426
pass
425427

sentry_sdk/integrations/starlette.py

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import functools
3-
import warnings
43
from collections.abc import Set
54
from copy import deepcopy
65
from json import JSONDecodeError
@@ -14,7 +13,6 @@
1413
)
1514
from sentry_sdk.integrations._wsgi_common import (
1615
DEFAULT_HTTP_METHODS_TO_CAPTURE,
17-
HttpCodeRangeContainer,
1816
_is_json_content_type,
1917
request_body_within_bounds,
2018
)
@@ -37,9 +35,9 @@
3735
from typing import TYPE_CHECKING
3836

3937
if TYPE_CHECKING:
40-
from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union
38+
from typing import Any, Awaitable, Callable, Dict, Optional, Tuple
4139

42-
from sentry_sdk._types import Event, HttpStatusCodeRange
40+
from sentry_sdk._types import Event
4341

4442
try:
4543
import starlette # type: ignore
@@ -89,7 +87,7 @@ class StarletteIntegration(Integration):
8987
def __init__(
9088
self,
9189
transaction_style="url", # type: str
92-
failed_request_status_codes=_DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Union[Set[int], list[HttpStatusCodeRange], None]
90+
failed_request_status_codes=_DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Set[int]
9391
middleware_spans=True, # type: bool
9492
http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: tuple[str, ...]
9593
):
@@ -103,24 +101,7 @@ def __init__(
103101
self.middleware_spans = middleware_spans
104102
self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture))
105103

106-
if isinstance(failed_request_status_codes, Set):
107-
self.failed_request_status_codes = (
108-
failed_request_status_codes
109-
) # type: Container[int]
110-
else:
111-
warnings.warn(
112-
"Passing a list or None for failed_request_status_codes is deprecated. "
113-
"Please pass a set of int instead.",
114-
DeprecationWarning,
115-
stacklevel=2,
116-
)
117-
118-
if failed_request_status_codes is None:
119-
self.failed_request_status_codes = _DEFAULT_FAILED_REQUEST_STATUS_CODES
120-
else:
121-
self.failed_request_status_codes = HttpCodeRangeContainer(
122-
failed_request_status_codes
123-
)
104+
self.failed_request_status_codes = failed_request_status_codes
124105

125106
@staticmethod
126107
def setup_once():
@@ -332,7 +313,7 @@ def _add_user_to_sentry_scope(scope):
332313
user_info.setdefault("email", starlette_user.email)
333314

334315
sentry_scope = sentry_sdk.get_isolation_scope()
335-
sentry_scope.user = user_info
316+
sentry_scope.set_user(user_info)
336317

337318

338319
def patch_authentication_middleware(middleware_class):

sentry_sdk/scope.py

-46
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,6 @@ def trace_propagation_meta(self, *args, **kwargs):
565565
Return meta tags which should be injected into HTML templates
566566
to allow propagation of trace information.
567567
"""
568-
span = kwargs.pop("span", None)
569-
if span is not None:
570-
warnings.warn(
571-
"The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future.",
572-
DeprecationWarning,
573-
stacklevel=2,
574-
)
575-
576568
meta = ""
577569

578570
sentry_trace = self.get_traceparent()
@@ -716,33 +708,6 @@ def transaction(self):
716708
# transaction) or a non-orphan span on the scope
717709
return self._span.containing_transaction
718710

719-
@transaction.setter
720-
def transaction(self, value):
721-
# type: (Any) -> None
722-
# would be type: (Optional[str]) -> None, see https://github.com/python/mypy/issues/3004
723-
"""When set this forces a specific transaction name to be set.
724-
725-
Deprecated: use set_transaction_name instead."""
726-
727-
# XXX: the docstring above is misleading. The implementation of
728-
# apply_to_event prefers an existing value of event.transaction over
729-
# anything set in the scope.
730-
# XXX: note that with the introduction of the Scope.transaction getter,
731-
# there is a semantic and type mismatch between getter and setter. The
732-
# getter returns a Span, the setter sets a transaction name.
733-
# Without breaking version compatibility, we could make the setter set a
734-
# transaction name or transaction (self._span) depending on the type of
735-
# the value argument.
736-
737-
warnings.warn(
738-
"Assigning to scope.transaction directly is deprecated: use scope.set_transaction_name() instead.",
739-
DeprecationWarning,
740-
stacklevel=2,
741-
)
742-
self._transaction = value
743-
if self._span and self._span.containing_transaction:
744-
self._span.containing_transaction.name = value
745-
746711
def set_transaction_name(self, name, source=None):
747712
# type: (str, Optional[str]) -> None
748713
"""Set the transaction name and optionally the transaction source."""
@@ -766,17 +731,6 @@ def transaction_source(self):
766731
# type: () -> Optional[str]
767732
return self._transaction_info.get("source")
768733

769-
@_attr_setter
770-
def user(self, value):
771-
# type: (Optional[Dict[str, Any]]) -> None
772-
"""When set a specific user is bound to the scope. Deprecated in favor of set_user."""
773-
warnings.warn(
774-
"The `Scope.user` setter is deprecated in favor of `Scope.set_user()`.",
775-
DeprecationWarning,
776-
stacklevel=2,
777-
)
778-
self.set_user(value)
779-
780734
def set_user(self, value):
781735
# type: (Optional[Dict[str, Any]]) -> None
782736
"""Sets a user for the scope."""

tests/integrations/fastapi/test_fastapi.py

-43
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
FASTAPI_VERSION = parse_version(fastapi.__version__)
2121

2222
from tests.integrations.conftest import parametrize_test_configurable_status_codes
23-
from tests.integrations.starlette import test_starlette
2423

2524

2625
def fastapi_app_factory():
@@ -528,48 +527,6 @@ def test_transaction_name_in_middleware(
528527
)
529528

530529

531-
@test_starlette.parametrize_test_configurable_status_codes_deprecated
532-
def test_configurable_status_codes_deprecated(
533-
sentry_init,
534-
capture_events,
535-
failed_request_status_codes,
536-
status_code,
537-
expected_error,
538-
):
539-
with pytest.warns(DeprecationWarning):
540-
starlette_integration = StarletteIntegration(
541-
failed_request_status_codes=failed_request_status_codes
542-
)
543-
544-
with pytest.warns(DeprecationWarning):
545-
fast_api_integration = FastApiIntegration(
546-
failed_request_status_codes=failed_request_status_codes
547-
)
548-
549-
sentry_init(
550-
integrations=[
551-
starlette_integration,
552-
fast_api_integration,
553-
]
554-
)
555-
556-
events = capture_events()
557-
558-
app = FastAPI()
559-
560-
@app.get("/error")
561-
async def _error():
562-
raise HTTPException(status_code)
563-
564-
client = TestClient(app)
565-
client.get("/error")
566-
567-
if expected_error:
568-
assert len(events) == 1
569-
else:
570-
assert not events
571-
572-
573530
@pytest.mark.skipif(
574531
FASTAPI_VERSION < (0, 80),
575532
reason="Requires FastAPI >= 0.80, because earlier versions do not support HTTP 'HEAD' requests",

tests/integrations/starlette/test_starlette.py

-76
Original file line numberDiff line numberDiff line change
@@ -1182,82 +1182,6 @@ def test_span_origin(sentry_init, capture_events):
11821182
assert span["origin"] == "auto.http.starlette"
11831183

11841184

1185-
class NonIterableContainer:
1186-
"""Wraps any container and makes it non-iterable.
1187-
1188-
Used to test backwards compatibility with our old way of defining failed_request_status_codes, which allowed
1189-
passing in a list of (possibly non-iterable) containers. The Python standard library does not provide any built-in
1190-
non-iterable containers, so we have to define our own.
1191-
"""
1192-
1193-
def __init__(self, inner):
1194-
self.inner = inner
1195-
1196-
def __contains__(self, item):
1197-
return item in self.inner
1198-
1199-
1200-
parametrize_test_configurable_status_codes_deprecated = pytest.mark.parametrize(
1201-
"failed_request_status_codes,status_code,expected_error",
1202-
[
1203-
(None, 500, True),
1204-
(None, 400, False),
1205-
([500, 501], 500, True),
1206-
([500, 501], 401, False),
1207-
([range(400, 499)], 401, True),
1208-
([range(400, 499)], 500, False),
1209-
([range(400, 499), range(500, 599)], 300, False),
1210-
([range(400, 499), range(500, 599)], 403, True),
1211-
([range(400, 499), range(500, 599)], 503, True),
1212-
([range(400, 403), 500, 501], 401, True),
1213-
([range(400, 403), 500, 501], 405, False),
1214-
([range(400, 403), 500, 501], 501, True),
1215-
([range(400, 403), 500, 501], 503, False),
1216-
([], 500, False),
1217-
([NonIterableContainer(range(500, 600))], 500, True),
1218-
([NonIterableContainer(range(500, 600))], 404, False),
1219-
],
1220-
)
1221-
"""Test cases for configurable status codes (deprecated API).
1222-
Also used by the FastAPI tests.
1223-
"""
1224-
1225-
1226-
@parametrize_test_configurable_status_codes_deprecated
1227-
def test_configurable_status_codes_deprecated(
1228-
sentry_init,
1229-
capture_events,
1230-
failed_request_status_codes,
1231-
status_code,
1232-
expected_error,
1233-
):
1234-
with pytest.warns(DeprecationWarning):
1235-
starlette_integration = StarletteIntegration(
1236-
failed_request_status_codes=failed_request_status_codes
1237-
)
1238-
1239-
sentry_init(integrations=[starlette_integration])
1240-
1241-
events = capture_events()
1242-
1243-
async def _error(request):
1244-
raise HTTPException(status_code)
1245-
1246-
app = starlette.applications.Starlette(
1247-
routes=[
1248-
starlette.routing.Route("/error", _error, methods=["GET"]),
1249-
],
1250-
)
1251-
1252-
client = TestClient(app)
1253-
client.get("/error")
1254-
1255-
if expected_error:
1256-
assert len(events) == 1
1257-
else:
1258-
assert not events
1259-
1260-
12611185
@pytest.mark.skipif(
12621186
STARLETTE_VERSION < (0, 21),
12631187
reason="Requires Starlette >= 0.21, because earlier versions do not support HTTP 'HEAD' requests",

0 commit comments

Comments
 (0)