The Flask integration adds support for the Flask Web Framework.
sentry-sdk from PyPI with the
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.
from flask import Flask import sentry_sdk sentry_sdk.init( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", enable_tracing=True, ) app = Flask(__name__)
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.
- If you use
send_default_pii=Truein your call to
init, 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.loggeror 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( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", enable_tracing=True, integrations = [ FlaskIntegration( transaction_style="url", ), ], )
You can pass the following keyword arguments to
@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 set
myendpointif you set
The default is
- Flask: 0.11+
- Python: 2.7+ (Flask 0.11+), 3.6 (Flask 2.0+)