2025-12-01
This commit is contained in:
+21
-21
@@ -3,6 +3,7 @@ import functools
|
||||
import warnings
|
||||
from collections.abc import Set
|
||||
from copy import deepcopy
|
||||
from json import JSONDecodeError
|
||||
|
||||
import sentry_sdk
|
||||
from sentry_sdk.consts import OP
|
||||
@@ -21,15 +22,13 @@ from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
|
||||
from sentry_sdk.scope import should_send_default_pii
|
||||
from sentry_sdk.tracing import (
|
||||
SOURCE_FOR_STYLE,
|
||||
TRANSACTION_SOURCE_COMPONENT,
|
||||
TRANSACTION_SOURCE_ROUTE,
|
||||
TransactionSource,
|
||||
)
|
||||
from sentry_sdk.utils import (
|
||||
AnnotatedValue,
|
||||
capture_internal_exceptions,
|
||||
ensure_integration_enabled,
|
||||
event_from_exception,
|
||||
logger,
|
||||
parse_version,
|
||||
transaction_from_function,
|
||||
)
|
||||
@@ -104,9 +103,7 @@ class StarletteIntegration(Integration):
|
||||
self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture))
|
||||
|
||||
if isinstance(failed_request_status_codes, Set):
|
||||
self.failed_request_status_codes = (
|
||||
failed_request_status_codes
|
||||
) # type: Container[int]
|
||||
self.failed_request_status_codes = failed_request_status_codes # type: Container[int]
|
||||
else:
|
||||
warnings.warn(
|
||||
"Passing a list or None for failed_request_status_codes is deprecated. "
|
||||
@@ -329,7 +326,7 @@ def _add_user_to_sentry_scope(scope):
|
||||
user_info.setdefault("email", starlette_user.email)
|
||||
|
||||
sentry_scope = sentry_sdk.get_isolation_scope()
|
||||
sentry_scope.user = user_info
|
||||
sentry_scope.set_user(user_info)
|
||||
|
||||
|
||||
def patch_authentication_middleware(middleware_class):
|
||||
@@ -363,13 +360,13 @@ def patch_middlewares():
|
||||
|
||||
if not_yet_patched:
|
||||
|
||||
def _sentry_middleware_init(self, cls, **options):
|
||||
# type: (Any, Any, Any) -> None
|
||||
def _sentry_middleware_init(self, cls, *args, **kwargs):
|
||||
# type: (Any, Any, Any, Any) -> None
|
||||
if cls == SentryAsgiMiddleware:
|
||||
return old_middleware_init(self, cls, **options)
|
||||
return old_middleware_init(self, cls, *args, **kwargs)
|
||||
|
||||
span_enabled_cls = _enable_span_for_middleware(cls)
|
||||
old_middleware_init(self, span_enabled_cls, **options)
|
||||
old_middleware_init(self, span_enabled_cls, *args, **kwargs)
|
||||
|
||||
if cls == AuthenticationMiddleware:
|
||||
patch_authentication_middleware(cls)
|
||||
@@ -403,9 +400,9 @@ def patch_asgi_app():
|
||||
if integration
|
||||
else DEFAULT_HTTP_METHODS_TO_CAPTURE
|
||||
),
|
||||
asgi_version=3,
|
||||
)
|
||||
|
||||
middleware.__call__ = middleware._run_asgi3
|
||||
return await middleware(scope, receive, send)
|
||||
|
||||
Starlette.__call__ = _sentry_patched_asgi_app
|
||||
@@ -681,8 +678,10 @@ class StarletteRequestExtractor:
|
||||
# type: (StarletteRequestExtractor) -> Optional[Dict[str, Any]]
|
||||
if not self.is_json():
|
||||
return None
|
||||
|
||||
return await self.request.json()
|
||||
try:
|
||||
return await self.request.json()
|
||||
except JSONDecodeError:
|
||||
return None
|
||||
|
||||
|
||||
def _transaction_name_from_router(scope):
|
||||
@@ -694,7 +693,11 @@ def _transaction_name_from_router(scope):
|
||||
for route in router.routes:
|
||||
match = route.matches(scope)
|
||||
if match[0] == Match.FULL:
|
||||
return route.path
|
||||
try:
|
||||
return route.path
|
||||
except AttributeError:
|
||||
# routes added via app.host() won't have a path attribute
|
||||
return scope.get("path")
|
||||
|
||||
return None
|
||||
|
||||
@@ -714,12 +717,9 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
|
||||
|
||||
if name is None:
|
||||
name = _DEFAULT_TRANSACTION_NAME
|
||||
source = TRANSACTION_SOURCE_ROUTE
|
||||
source = TransactionSource.ROUTE
|
||||
|
||||
scope.set_transaction_name(name, source=source)
|
||||
logger.debug(
|
||||
"[Starlette] Set transaction name and source on scope: %s / %s", name, source
|
||||
)
|
||||
|
||||
|
||||
def _get_transaction_from_middleware(app, asgi_scope, integration):
|
||||
@@ -729,9 +729,9 @@ def _get_transaction_from_middleware(app, asgi_scope, integration):
|
||||
|
||||
if integration.transaction_style == "endpoint":
|
||||
name = transaction_from_function(app.__class__)
|
||||
source = TRANSACTION_SOURCE_COMPONENT
|
||||
source = TransactionSource.COMPONENT
|
||||
elif integration.transaction_style == "url":
|
||||
name = _transaction_name_from_router(asgi_scope)
|
||||
source = TRANSACTION_SOURCE_ROUTE
|
||||
source = TransactionSource.ROUTE
|
||||
|
||||
return name, source
|
||||
|
||||
Reference in New Issue
Block a user