Skip to content
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

fix(django): Add sync_capable to SentryWrappingMiddleware #3510

Merged
merged 3 commits into from
Sep 11, 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
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def sentry_wrapped_method(*args, **kwargs):
class SentryWrappingMiddleware(
_asgi_middleware_mixin_factory(_check_middleware_span) # type: ignore
):
sync_capable = getattr(middleware, "sync_capable", True)
async_capable = DJANGO_SUPPORTS_ASYNC_MIDDLEWARE and getattr(
middleware, "async_capable", False
)
Expand Down
34 changes: 34 additions & 0 deletions tests/integrations/django/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Optional

import pytest

from sentry_sdk.integrations.django.middleware import _wrap_middleware


def _sync_capable_middleware_factory(sync_capable):
# type: (Optional[bool]) -> type
"""Create a middleware class with a sync_capable attribute set to the value passed to the factory.
If the factory is called with None, the middleware class will not have a sync_capable attribute.
"""
sc = sync_capable # rename so we can set sync_capable in the class

class TestMiddleware:
nonlocal sc
if sc is not None:
sync_capable = sc

return TestMiddleware


@pytest.mark.parametrize(
("middleware", "sync_capable"),
(
(_sync_capable_middleware_factory(True), True),
(_sync_capable_middleware_factory(False), False),
(_sync_capable_middleware_factory(None), True),
),
)
def test_wrap_middleware_sync_capable_attribute(middleware, sync_capable):
wrapped_middleware = _wrap_middleware(middleware, "test_middleware")

assert wrapped_middleware.sync_capable is sync_capable
Loading