From f2a9f38fd17da9c9cfb49d81686cde943ca4ff70 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 23 Oct 2025 09:22:01 +0200 Subject: [PATCH 1/3] fix(strawberry): Remove autodetection, always use sync extension --- sentry_sdk/integrations/strawberry.py | 24 +++------- .../strawberry/test_strawberry.py | 45 +++++-------------- 2 files changed, 15 insertions(+), 54 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index ae7d273079..7983c81ca5 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -1,5 +1,6 @@ import functools import hashlib +import warnings from inspect import isawaitable import sentry_sdk @@ -98,13 +99,10 @@ def _sentry_patched_schema_init(self, *args, **kwargs): if integration.async_execution is not None: should_use_async_extension = integration.async_execution else: - # try to figure it out ourselves - should_use_async_extension = _guess_if_using_async(extensions) - - logger.info( - "Assuming strawberry is running %s. If not, initialize it as StrawberryIntegration(async_execution=%s).", - "async" if should_use_async_extension else "sync", - "False" if should_use_async_extension else "True", + # use the sync extension by default + warnings.warn( + "Assuming strawberry is running sync. If not, initialize the integration as StrawberryIntegration(async_execution=True).", + stacklevel=2, ) # remove the built in strawberry sentry extension, if present @@ -379,15 +377,3 @@ def inner(event, hint): return event return inner - - -def _guess_if_using_async(extensions): - # type: (List[SchemaExtension]) -> bool - if StrawberrySentryAsyncExtension in extensions: - return True - elif StrawberrySentrySyncExtension in extensions: - return False - - return bool( - {"starlette", "starlite", "litestar", "fastapi"} & set(_get_installed_modules()) - ) diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index 7b40b238d2..ba645da257 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -103,49 +103,24 @@ def create_app(schema): def test_async_execution_uses_async_extension(sentry_init): sentry_init(integrations=[StrawberryIntegration(async_execution=True)]) - with mock.patch( - "sentry_sdk.integrations.strawberry._get_installed_modules", - return_value={"flask": "2.3.3"}, - ): - # actual installed modules should not matter, the explicit option takes - # precedence - schema = strawberry.Schema(Query) - assert SentryAsyncExtension in schema.extensions + schema = strawberry.Schema(Query) + assert SentryAsyncExtension in schema.extensions + assert SentrySyncExtension not in schema.extensions def test_sync_execution_uses_sync_extension(sentry_init): sentry_init(integrations=[StrawberryIntegration(async_execution=False)]) - with mock.patch( - "sentry_sdk.integrations.strawberry._get_installed_modules", - return_value={"fastapi": "0.103.1", "starlette": "0.27.0"}, - ): - # actual installed modules should not matter, the explicit option takes - # precedence - schema = strawberry.Schema(Query) - assert SentrySyncExtension in schema.extensions - - -def test_infer_execution_type_from_installed_packages_async(sentry_init): - sentry_init(integrations=[StrawberryIntegration()]) - - with mock.patch( - "sentry_sdk.integrations.strawberry._get_installed_modules", - return_value={"fastapi": "0.103.1", "starlette": "0.27.0"}, - ): - schema = strawberry.Schema(Query) - assert SentryAsyncExtension in schema.extensions + schema = strawberry.Schema(Query) + assert SentrySyncExtension in schema.extensions + assert SentryAsyncExtension not in schema.extensions -def test_infer_execution_type_from_installed_packages_sync(sentry_init): +def test_use_sync_extension_if_not_specified(sentry_init): sentry_init(integrations=[StrawberryIntegration()]) - - with mock.patch( - "sentry_sdk.integrations.strawberry._get_installed_modules", - return_value={"flask": "2.3.3"}, - ): - schema = strawberry.Schema(Query) - assert SentrySyncExtension in schema.extensions + schema = strawberry.Schema(Query) + assert SentrySyncExtension in schema.extensions + assert SentryAsyncExtension not in schema.extensions @pytest.mark.skipif( From 906e9d8c5360c4a06e9bbe17745bc6caa4c78783 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 23 Oct 2025 10:15:25 +0200 Subject: [PATCH 2/3] . --- sentry_sdk/integrations/strawberry.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 7983c81ca5..2cde6f5e7d 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -99,11 +99,15 @@ def _sentry_patched_schema_init(self, *args, **kwargs): if integration.async_execution is not None: should_use_async_extension = integration.async_execution else: - # use the sync extension by default - warnings.warn( - "Assuming strawberry is running sync. If not, initialize the integration as StrawberryIntegration(async_execution=True).", - stacklevel=2, - ) + # try to figure it out ourselves + should_use_async_extension = _guess_if_using_async(extensions) + + if should_use_async_extension is None: + warnings.warn( + "Assuming strawberry is running sync. If not, initialize the integration as StrawberryIntegration(async_execution=True).", + stacklevel=2, + ) + should_use_async_extension = False # remove the built in strawberry sentry extension, if present extensions = [ @@ -377,3 +381,13 @@ def inner(event, hint): return event return inner + + +def _guess_if_using_async(extensions): + # type: (List[SchemaExtension]) -> Optional[bool] + if StrawberrySentryAsyncExtension in extensions: + return True + elif StrawberrySentrySyncExtension in extensions: + return False + + return None From fbf13b734374e03c5330c5ebc5343c45efef01ce Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 23 Oct 2025 10:19:30 +0200 Subject: [PATCH 3/3] . --- sentry_sdk/integrations/strawberry.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 2cde6f5e7d..f30e95e7f6 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -96,6 +96,7 @@ def _sentry_patched_schema_init(self, *args, **kwargs): extensions = kwargs.get("extensions") or [] + should_use_async_extension = None # type: Optional[bool] if integration.async_execution is not None: should_use_async_extension = integration.async_execution else: