Falcon for Python
Learn about using Sentry with Falcon.
The Falcon integration adds support for the Falcon web framework. The integration has been confirmed to work with Falcon 3.0 and later.
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.
Install sentry-sdk from PyPI:
pip install sentry-sdk
pip install sentry-sdk
uv add sentry-sdk
If you have the falcon package in your dependencies, the Falcon integration will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
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={
"user_info": 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
)
import sentry_sdk
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={
"user_info": 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
)
import falcon
sentry_sdk.init(...) # same as above
class HelloWorldResource:
def on_get(self, req, resp):
message = {
'hello': "world",
}
1 / 0 # raises an error
resp.media = message
app = falcon.App()
app.add_route('/', HelloWorldResource())
import falcon
sentry_sdk.init(...) # same as above
class HelloWorldResource:
def on_get(self, req, resp):
message = {
'hello': "world",
}
1 / 0 # raises an error
resp.media = message
app = falcon.App()
app.add_route('/', HelloWorldResource())
When you point your browser to http://localhost:8000/ a transaction will be created in the Performance section of sentry.io. Additionally, an error event will be sent to sentry.io and will be connected to the transaction.
It takes a couple of moments for the data to appear in sentry.io.
The Sentry Python SDK will install the Falcon integration for all of your apps. The integration hooks into the base
falcon.APIclass via monkey patching.All exceptions leading to an Internal Server Error are reported.
Request data is attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you configure which categories to collect with the
data_collectionoption or setsend_default_piitoTrue.send_default_piiwill be removed in version 3.0 of the SDK.Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.
In stream mode, these options still work as described, but apply to service spans instead of transactions.
By adding FalconIntegration to your sentry_sdk.init() call explicitly, you can set options for FalconIntegration to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.falcon import FalconIntegration
sentry_sdk.init(
# same as above
integrations=[
FalconIntegration(
transaction_style="path",
),
],
)
import sentry_sdk
from sentry_sdk.integrations.falcon import FalconIntegration
sentry_sdk.init(
# same as above
integrations=[
FalconIntegration(
transaction_style="path",
),
],
)
You can pass the following keyword arguments to FalconIntegration():
transaction_style:Copiedclass MessageResource: def on_get(self, req, resp, message_id): msg = database.get_message(message_id) resp.media = msg.as_json() app = falcon.API() app.add_route("/message/{message_id}", MessageResource())class MessageResource: def on_get(self, req, resp, message_id): msg = database.get_message(message_id) resp.media = msg.as_json() app = falcon.API() app.add_route("/message/{message_id}", MessageResource())In the above code, you would set the transaction to:
/myurl/b48a7686-ad8c-4c94-8c3b-412ec7f25db2123if you settransaction_style="path"./myurl/{message_id}if you settransaction_style="uri_template"
The default is
"uri_template".
- Falcon:
- 1.4+ (SDK <3.0)
- 3.0+ (SDK 3.0+)
- Python:
- 3.6+ (SDK <3.0)
- 3.7+ (SDK 3.0+)
If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK. The minimum versions for each major version are listed above.
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").