Strawberry for Python
Learn how to import the Strawberry GraphQL integration and how it captures GraphQL errors and spans.
The Strawberry integration captures errors and operations from the Strawberry GraphQL library, which can then be viewed in Sentry.
If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See Streamed Spans for more information.
To get started, install sentry-sdk from PyPI.
pip install "sentry-sdk"
pip install "sentry-sdk"
uv add "sentry-sdk"
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
Add StrawberryIntegration() to your integrations list:
import sentry_sdk
from sentry_sdk.integrations.strawberry import StrawberryIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"graphql": {"document": True, "variables": True},
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
integrations=[
StrawberryIntegration(
# Set async_execution to True if you have
# at least one async resolver
async_execution=True,
),
],
)
import sentry_sdk
from sentry_sdk.integrations.strawberry import StrawberryIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"graphql": {"document": True, "variables": True},
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
integrations=[
StrawberryIntegration(
# Set async_execution to True if you have
# at least one async resolver
async_execution=True,
),
],
)
Make sure you have all the prerequisites installed:
pip install 'strawberry-graphql[debug-server]'
pip install 'strawberry-graphql[debug-server]'
Create a schema.py file with the below content:
import strawberry
sentry_sdk.init(...) # same as above
async def resolve_hello(root) -> str:
1 / 0
return "Hello world!"
@strawberry.type
class Query:
hello: str = strawberry.field(resolver=resolve_hello)
schema = strawberry.Schema(Query)
import strawberry
sentry_sdk.init(...) # same as above
async def resolve_hello(root) -> str:
1 / 0
return "Hello world!"
@strawberry.type
class Query:
hello: str = strawberry.field(resolver=resolve_hello)
schema = strawberry.Schema(Query)
To start the web server, run:
strawberry server schema
strawberry server schema
Navigate to http://127.0.0.1:8000/graphql in your browser. You should see the GraphiQL graphical interface.
Type query HelloQuery { hello } into the query input field then press the "Execute query" button. Your web server will be queried, which should result in an exception due to the ZeroDivisionError we've snuck into the resolve_hello resolver.
This will create a GraphQLError in the Issues section as well as a transaction in the Performance section of sentry.io. As long as the GraphQL query has a name (like HelloQuery in our example), the transaction will be named after it.
It might take a couple of moments for the error and transaction to appear in sentry.io.
There are several options you will get to choose from:
Strawberry supports both synchronous and asynchronous execution of GraphQL queries. If you have at least one async resolver, you should initialize StrawberryIntegration with async_execution=True, otherwise set it to False.
By default, the SDK will use the synchronous version of the integration.
sentry_sdk.init(
# ...
integrations=[
StrawberryIntegration(
async_execution=True # or False
),
],
)
sentry_sdk.init(
# ...
integrations=[
StrawberryIntegration(
async_execution=True # or False
),
],
)
The Strawberry integration can capture request and response bodies for each GraphQL error that happens. Since these may contain sensitive data, this isn't the default behavior.
To enable capturing request and response bodies, use the data_collection option. The graphql key controls the query document and its variables in the request body, and the outgoing_response entry in http_bodies controls the response body:
sentry_sdk.init(
# ...
data_collection={
"graphql": {"document": True, "variables": True},
"http_bodies": ["incoming_request", "outgoing_request", "outgoing_response"],
},
)
sentry_sdk.init(
# ...
data_collection={
"graphql": {"document": True, "variables": True},
"http_bodies": ["incoming_request", "outgoing_request", "outgoing_response"],
},
)
These are also the defaults when you pass a data_collection dictionary, so you only need to set the keys you want to change. See Data Collected for all available keys.
Alternatively, you can initialize the SDK with the send_default_pii option set to True, though send_default_pii will be removed in version 3.0 of the SDK:
sentry_sdk.init(
# ...
send_default_pii=True,
)
sentry_sdk.init(
# ...
send_default_pii=True,
)
Note
Since send_default_pii is a global SDK option, setting it to True will affect all integrations, not just Strawberry. Please make sure to remove sensitive data from events before enabling this option. With data_collection, you can enable only the categories you need instead.
Strawberry comes with a (now deprecated) built-in Sentry tracing extension that this integration is built on. To prevent duplicate traces, the Sentry SDK integration will deactivate the built-in Strawberry extension if you happen to be using both.
- strawberry-graphql: 0.209.5+
- Python: 3.8+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").