From 1f192146e97b5e5e007f1e64fa9826d90c174ac5 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Mon, 9 Sep 2024 17:59:04 +0200 Subject: [PATCH 1/2] fix(django): Add `sync_capable` to `SentryWrappingMiddleware` Fixes #3506 --- sentry_sdk/integrations/django/middleware.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/integrations/django/middleware.py b/sentry_sdk/integrations/django/middleware.py index 981d192864..2cde251fd3 100644 --- a/sentry_sdk/integrations/django/middleware.py +++ b/sentry_sdk/integrations/django/middleware.py @@ -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 ) From 35a1388a0cc58a0a9fdaa5a8e87d70257d634d6e Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 10 Sep 2024 11:21:19 +0200 Subject: [PATCH 2/2] test(django): Test that `sync_capable` set on wrapped middleware --- tests/integrations/django/test_middleware.py | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/integrations/django/test_middleware.py diff --git a/tests/integrations/django/test_middleware.py b/tests/integrations/django/test_middleware.py new file mode 100644 index 0000000000..2a8d94f623 --- /dev/null +++ b/tests/integrations/django/test_middleware.py @@ -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