Skip to content
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typing = [
"httpx2",
"botocore-stubs",
"werkzeug",
"starlette>=1.3.1",
]
test = [
"dataclasses ; python_full_version < '3.7'",
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
if TYPE_CHECKING:
from typing import Any, Callable, ContextManager, Optional, Tuple, Union

from starlette.types import Receive, Scope, Send # type: ignore[import-not-found]
from starlette.types import Receive, Scope, Send

from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing import Span
Expand Down
46 changes: 27 additions & 19 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,39 @@
)

if TYPE_CHECKING:
from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union
from typing import (
Any,
Awaitable,
Callable,
Container,
Dict,
Optional,
Tuple,
Union,
)

from sentry_sdk._types import Event, HttpStatusCodeRange

try:
import starlette # type: ignore
import starlette
from starlette import __version__ as STARLETTE_VERSION
from starlette.applications import Starlette # type: ignore
from starlette.datastructures import ( # type: ignore
from starlette.applications import Starlette
from starlette.datastructures import (
UploadFile,
)
from starlette.middleware import Middleware # type: ignore
from starlette.middleware.authentication import ( # type: ignore
from starlette.middleware import Middleware
from starlette.middleware.authentication import (
AuthenticationMiddleware,
)
from starlette.requests import Request # type: ignore
from starlette.routing import Match # type: ignore
from starlette.types import ASGIApp, Receive, Send # type: ignore
from starlette.requests import Request
from starlette.routing import Match
from starlette.types import ASGIApp, Receive, Send
from starlette.types import Scope as StarletteScope
except ImportError:
raise DidNotEnable("Starlette is not installed")

try:
# Starlette 0.20
from starlette.middleware.exceptions import ExceptionMiddleware # type: ignore
from starlette.middleware.exceptions import ExceptionMiddleware
except ImportError:
# Startlette 0.19.1
from starlette.exceptions import ExceptionMiddleware # type: ignore
Expand Down Expand Up @@ -156,8 +164,10 @@ def setup_once() -> None:
patch_templates()


def _enable_span_for_middleware(middleware_class: "Any") -> type:
old_call = middleware_class.__call__
def _enable_span_for_middleware(
middleware_class: "Any",
) -> "Any":
old_call: "Callable[..., Awaitable[Any]]" = middleware_class.__call__

async def _create_span_call(
app: "Any",
Expand Down Expand Up @@ -430,7 +440,7 @@ def _sentry_middleware_init(
if cls == ExceptionMiddleware:
patch_exception_middleware(cls)

Middleware.__init__ = _sentry_middleware_init
Middleware.__init__ = _sentry_middleware_init # type: ignore[method-assign]


def patch_asgi_app(root_path_in_path: "_RootPathInPath") -> None:
Expand Down Expand Up @@ -462,7 +472,7 @@ async def _sentry_patched_asgi_app(

return await middleware(scope, receive, send)

Starlette.__call__ = _sentry_patched_asgi_app
Starlette.__call__ = _sentry_patched_asgi_app # type: ignore[method-assign]


# This was vendored in from Starlette to support Starlette 0.19.1 because
Expand Down Expand Up @@ -662,7 +672,7 @@ def patch_templates() -> None:

# https://github.com/Kludex/starlette/commit/96479daca2e4bd8157f68d914fd162aa94eff73a
try:
from starlette.templating import Jinja2Templates # type: ignore
from starlette.templating import Jinja2Templates
except ImportError:
return

Expand Down Expand Up @@ -692,7 +702,7 @@ def add_sentry_trace_meta(request: "Request") -> "Dict[str, Any]":

return old_jinja2templates_init(self, *args, **kwargs)

Jinja2Templates.__init__ = _sentry_jinja2templates_init
Jinja2Templates.__init__ = _sentry_jinja2templates_init # type: ignore[method-assign]


class StarletteRequestExtractor:
Expand All @@ -701,8 +711,6 @@ class StarletteRequestExtractor:
(like form data or cookies) and adds it to the Sentry event.
"""

request: "Request" = None

def __init__(self: "StarletteRequestExtractor", request: "Request") -> None:
self.request = request

Expand Down
13 changes: 13 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading