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
19 changes: 10 additions & 9 deletions sentry_sdk/integrations/strawberry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import hashlib
import warnings
from inspect import isawaitable

import sentry_sdk
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
45 changes: 10 additions & 35 deletions tests/integrations/strawberry/test_strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down