|
| 1 | +--- |
| 2 | +title: Flask |
| 3 | +redirect_from: |
| 4 | + - /platforms/python/flask/ |
| 5 | +description: "Learn about using Sentry with Flask." |
| 6 | +--- |
| 7 | + |
| 8 | +The Flask integration adds support for the [Flask Web Framework](https://flask.palletsprojects.com). |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +Install `sentry-sdk` from PyPI with the `flask` extra: |
| 13 | + |
| 14 | +```bash |
| 15 | +pip install --upgrade "sentry-sdk[flask]" |
| 16 | +``` |
| 17 | + |
| 18 | +## Configure |
| 19 | + |
| 20 | +If you have the `flask` package in your dependencies, the Flask integration will be enabled automatically when you initialize the Sentry SDK. |
| 21 | + |
| 22 | +<SignInNote /> |
| 23 | + |
| 24 | +```python |
| 25 | +from flask import Flask |
| 26 | +import sentry_sdk |
| 27 | + |
| 28 | +sentry_sdk.init( |
| 29 | + dsn="___PUBLIC_DSN___", |
| 30 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 31 | + # of transactions for performance monitoring. |
| 32 | + traces_sample_rate=1.0, |
| 33 | +) |
| 34 | + |
| 35 | +app = Flask(__name__) |
| 36 | +``` |
| 37 | + |
| 38 | +<Note> |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +</Note> |
| 43 | + |
| 44 | +## Verify |
| 45 | + |
| 46 | +```python |
| 47 | +from flask import Flask |
| 48 | + |
| 49 | +sentry_sdk.init(...) # same as above |
| 50 | + |
| 51 | +app = Flask(__name__) |
| 52 | + |
| 53 | +@app.route("/") |
| 54 | +def hello_world(): |
| 55 | + 1/0 # raises an error |
| 56 | + return "<p>Hello, World!</p>" |
| 57 | +``` |
| 58 | + |
| 59 | +When you point your browser to [http://localhost:5000/](http://localhost:5000/) a transaction in the Performance section of [sentry.io](https://sentry.io) will be created. Additionally, an error event will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction. |
| 60 | + |
| 61 | +It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 62 | + |
| 63 | +## Behavior |
| 64 | + |
| 65 | +After initialization: |
| 66 | + |
| 67 | +- If you use `flask-login` and set `send_default_pii=True` in your call to `init`, user data (current user id, email address, username) will be attached to the event. |
| 68 | +- Request data will be attached to all events: **HTTP method, URL, headers, form data, JSON payloads**. Sentry excludes raw bodies and multipart file uploads. |
| 69 | +- Logs emitted by `app.logger` or _any_ logger will be recorded as breadcrumbs by the [Logging](/platforms/python/guides/logging/) integration (this integration is enabled by default). |
| 70 | + |
| 71 | +## Options |
| 72 | + |
| 73 | +If you add `FlaskIntegration` explicitly to your `sentry_sdk.init()` call you can set options for `FlaskIntegration` to change its behavior: |
| 74 | + |
| 75 | +```python |
| 76 | +import sentry_sdk |
| 77 | +from sentry_sdk.integrations.flask import FlaskIntegration |
| 78 | + |
| 79 | +sentry_sdk.init( |
| 80 | + dsn="___PUBLIC_DSN___", |
| 81 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 82 | + # of transactions for performance monitoring. |
| 83 | + traces_sample_rate=1.0, |
| 84 | + integrations = [ |
| 85 | + FlaskIntegration( |
| 86 | + transaction_style="url", |
| 87 | + ), |
| 88 | + ], |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +You can pass the following keyword arguments to `FlaskIntegration()`: |
| 93 | + |
| 94 | +- `transaction_style`: |
| 95 | + |
| 96 | + ```python |
| 97 | + @app.route("/myurl/<foo>") |
| 98 | + def myendpoint(): |
| 99 | + return "<p>Hello, World!</p>" |
| 100 | + ``` |
| 101 | + |
| 102 | + In the above code, you would set the transaction to: |
| 103 | + |
| 104 | + - `/myurl/<foo>` if you set `transaction_style="url"`. |
| 105 | + - `myendpoint` if you set `transaction_style="endpoint"` |
| 106 | + |
| 107 | + The default is `"endpoint"`. |
| 108 | + |
| 109 | +## Supported Versions |
| 110 | + |
| 111 | +- Flask: 0.11+ |
| 112 | +- Python: 2.7+ (Flask 0.11+), 3.6 (Flask 2.0+) |
0 commit comments