diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index ae7d273079..f30e95e7f6 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 @@ -95,17 +96,19 @@ 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: # 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", - ) + 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 = [ @@ -382,12 +385,10 @@ def inner(event, hint): def _guess_if_using_async(extensions): - # type: (List[SchemaExtension]) -> bool + # type: (List[SchemaExtension]) -> Optional[bool] if StrawberrySentryAsyncExtension in extensions: return True elif StrawberrySentrySyncExtension in extensions: return False - return bool( - {"starlette", "starlite", "litestar", "fastapi"} & set(_get_installed_modules()) - ) + return None 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(