Skip to content

Commit e636619

Browse files
authored
fix(strawberry): Remove autodetection, always use sync extension (#4984)
### Description #### Problem There are two ways to instrument strawberry-graphl apps: via a sync or an async extension. We enable one or the other based on what web framework is installed (async framework -> async extension, sync framework -> sync extension). This auto-detection can be overridden via an integration option. At some point (SDK 2.0?), we added `StrawberryIntegration` to auto-enabling integrations, which means the brittle auto-detection kicks in as soon as someone has `strawberry-graphl` installed. This can lead to issues, most notably when we auto-enable the async version of the extension even though the user's Strawberry app is actually sync. #### Options 1. Removing the auto-detection, always enabling the sync version if not specified otherwise. This way we'll never mistakenly enable async code in a sync app. We also had a [report](#3670 (comment)) at some point that the sync extension actually performs better than async, so enabling sync by default shouldn't be a big problem in async apps. 2. Removing `StrawberryIntegration` from auto-enabling integrations. Breaking change. People might just lose their traces and errors. 3. Improving the auto-detection. Best option, but out of ideas how to do this. Went with 1), all things considered it's the least breaking change (unless there's a way to do 3). Needs a big callout in the changelog anyway though. #### Issues Closes #4980 #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 8ab8f3b commit e636619

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

sentry_sdk/integrations/strawberry.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import hashlib
3+
import warnings
34
from inspect import isawaitable
45

56
import sentry_sdk
@@ -95,17 +96,19 @@ def _sentry_patched_schema_init(self, *args, **kwargs):
9596

9697
extensions = kwargs.get("extensions") or []
9798

99+
should_use_async_extension = None # type: Optional[bool]
98100
if integration.async_execution is not None:
99101
should_use_async_extension = integration.async_execution
100102
else:
101103
# try to figure it out ourselves
102104
should_use_async_extension = _guess_if_using_async(extensions)
103105

104-
logger.info(
105-
"Assuming strawberry is running %s. If not, initialize it as StrawberryIntegration(async_execution=%s).",
106-
"async" if should_use_async_extension else "sync",
107-
"False" if should_use_async_extension else "True",
108-
)
106+
if should_use_async_extension is None:
107+
warnings.warn(
108+
"Assuming strawberry is running sync. If not, initialize the integration as StrawberryIntegration(async_execution=True).",
109+
stacklevel=2,
110+
)
111+
should_use_async_extension = False
109112

110113
# remove the built in strawberry sentry extension, if present
111114
extensions = [
@@ -382,12 +385,10 @@ def inner(event, hint):
382385

383386

384387
def _guess_if_using_async(extensions):
385-
# type: (List[SchemaExtension]) -> bool
388+
# type: (List[SchemaExtension]) -> Optional[bool]
386389
if StrawberrySentryAsyncExtension in extensions:
387390
return True
388391
elif StrawberrySentrySyncExtension in extensions:
389392
return False
390393

391-
return bool(
392-
{"starlette", "starlite", "litestar", "fastapi"} & set(_get_installed_modules())
393-
)
394+
return None

tests/integrations/strawberry/test_strawberry.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,49 +103,24 @@ def create_app(schema):
103103
def test_async_execution_uses_async_extension(sentry_init):
104104
sentry_init(integrations=[StrawberryIntegration(async_execution=True)])
105105

106-
with mock.patch(
107-
"sentry_sdk.integrations.strawberry._get_installed_modules",
108-
return_value={"flask": "2.3.3"},
109-
):
110-
# actual installed modules should not matter, the explicit option takes
111-
# precedence
112-
schema = strawberry.Schema(Query)
113-
assert SentryAsyncExtension in schema.extensions
106+
schema = strawberry.Schema(Query)
107+
assert SentryAsyncExtension in schema.extensions
108+
assert SentrySyncExtension not in schema.extensions
114109

115110

116111
def test_sync_execution_uses_sync_extension(sentry_init):
117112
sentry_init(integrations=[StrawberryIntegration(async_execution=False)])
118113

119-
with mock.patch(
120-
"sentry_sdk.integrations.strawberry._get_installed_modules",
121-
return_value={"fastapi": "0.103.1", "starlette": "0.27.0"},
122-
):
123-
# actual installed modules should not matter, the explicit option takes
124-
# precedence
125-
schema = strawberry.Schema(Query)
126-
assert SentrySyncExtension in schema.extensions
127-
128-
129-
def test_infer_execution_type_from_installed_packages_async(sentry_init):
130-
sentry_init(integrations=[StrawberryIntegration()])
131-
132-
with mock.patch(
133-
"sentry_sdk.integrations.strawberry._get_installed_modules",
134-
return_value={"fastapi": "0.103.1", "starlette": "0.27.0"},
135-
):
136-
schema = strawberry.Schema(Query)
137-
assert SentryAsyncExtension in schema.extensions
114+
schema = strawberry.Schema(Query)
115+
assert SentrySyncExtension in schema.extensions
116+
assert SentryAsyncExtension not in schema.extensions
138117

139118

140-
def test_infer_execution_type_from_installed_packages_sync(sentry_init):
119+
def test_use_sync_extension_if_not_specified(sentry_init):
141120
sentry_init(integrations=[StrawberryIntegration()])
142-
143-
with mock.patch(
144-
"sentry_sdk.integrations.strawberry._get_installed_modules",
145-
return_value={"flask": "2.3.3"},
146-
):
147-
schema = strawberry.Schema(Query)
148-
assert SentrySyncExtension in schema.extensions
121+
schema = strawberry.Schema(Query)
122+
assert SentrySyncExtension in schema.extensions
123+
assert SentryAsyncExtension not in schema.extensions
149124

150125

151126
@pytest.mark.skipif(

0 commit comments

Comments
 (0)