Flask
Learn about using Sentry with Flask.
The Flask integration adds support for the Flask Web Framework.
Install sentry-sdk
from PyPI with the flask
extra:
pip install --upgrade "sentry-sdk[flask]"
If you have the flask
package in your dependencies, the Flask integration will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
Our Python SDK will install the Flask integration for all of your apps. It hooks into Flask’s signals, not anything on the app object.
from flask import Flask
sentry_sdk.init(...) # same as above
app = Flask(__name__)
@app.route("/")
def hello_world():
1 / 0 # raises an error
return "<p>Hello, World!</p>"
When you point your browser to http://localhost:5000/ a transaction in the Performance section of sentry.io will be created. 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.
uWSGI and Sentry SDK
If you're using uWSGI, note that it doesn't support threads by default. This might lead to unexpected behavior when using the Sentry SDK, from features not working properly to uWSGI workers crashing.
To enable threading support in uWSGI, make sure you have both --enable-threads
and --py-call-uwsgi-fork-hooks
on.
After initialization:
- If you use
flask-login
and setsend_default_pii=True
in your call toinit
, user data (current user id, email address, username) will be attached to the event. - Request data will be attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads.
- Logs emitted by
app.logger
or any logger will be recorded as breadcrumbs by the Logging integration (this integration is enabled by default).
If you add FlaskIntegration
explicitly to your sentry_sdk.init()
call you can set options for FlaskIntegration
to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
# ...
integrations = [
FlaskIntegration(
transaction_style="url",
http_methods_to_capture=("GET",),
),
],
)
You can pass the following keyword arguments to FlaskIntegration()
:
transaction_style
:Sets the format or style that transactions are named.
Copied@app.route("/myurl/<foo>") def myendpoint(): return "<p>Hello, World!</p>"
In the above code, you would set the transaction to:
/myurl/<foo>
if you settransaction_style="url"
.myendpoint
if you settransaction_style="endpoint"
.
The default is
"endpoint"
.http_methods_to_capture
:A tuple containing all the HTTP methods that should create a transaction in Sentry.
The default is
("CONNECT", "DELETE", "GET", "PATCH", "POST", "PUT", "TRACE",)
.(Note that
OPTIONS
andHEAD
are missing by default.)Added in 2.15.0
The
http_methods_to_capture
option.
- Flask: 1.0+
- Python: 3.6+
The versions above apply for Sentry Python SDK version 2.0+
, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x
major line of releases.
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").