Flask
The Flask integration adds support for the Flask Web Framework.
Install
Install sentry-sdk from PyPI with the flask extra:
pip install --upgrade 'sentry-sdk[flask]'Configure
To configure the SDK, initialize it with the integration before or after your app has been initialized:
import sentry_sdk
from flask import Flask
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[FlaskIntegration()]
)
app = Flask(__name__)Verify
Verify your Sentry installation by creating a route that triggers an error:
@app.route('/debug-sentry')
def trigger_error():
division_by_zero = 1 / 0Visiting this route will trigger an error that will be captured by Sentry.
Behavior
The Sentry Python SDK will install the Flask integration for all of your apps. It hooks into Flaskās signals, not anything on the app object.
All unhandled exceptions will be reported. Note that
@app.errorhandler(Exception)will prevent all exceptions from being sent to Sentry while@app.errorhandler(500)will not.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 set
send_default_piitoTrue.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.
If you use
flask-loginand have setsend_default_pii=Truein your call toinit, user data is attached to the event. Apart from the user ID we will try to access the attributesemailandusernameon your user object to capture some more data.Logging with
app.loggeror any logger will create breadcrumbs when the Logging integration is enabled (done by default).
Options
You can pass the following keyword arguments to FlaskIntegration():
transaction_style:Copied@app.route("/myurl/<foo>") def myendpoint(): return "ok"In the above code, you would set the transaction to:
/myurl/<foo>if you settransaction_style="url". This matches the behavior of the old Raven SDK.myendpointif you settransaction_style="endpoint"
The default is
"endpoint".
User Feedback
You can use the user feedback feature with this integration. For more information see User Feedback.
- Package:
- pypi:sentry-sdk
- Version:
- 0.18.0
- Repository:
- https://github.com/getsentry/sentry-python