|
| 1 | +--- |
| 2 | +title: AIOHTTP |
| 3 | +description: "Learn about using Sentry with AIOHTTP." |
| 4 | +--- |
| 5 | + |
| 6 | +The AIOHTTP integration adds support for the [AIOHTTP server web framework](https://docs.aiohttp.org/en/stable/web.html). |
| 7 | + |
| 8 | +If you use AIOHTTP as your HTTP client and want to instrument outgoing HTTP requests, have a look at the <PlatformLink to="/integrations/aiohttp/aiohttp-client/">AIOHTTP client documentation</PlatformLink>. |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +Install `sentry-sdk` from PyPI with the `aiohttp` extra: |
| 13 | + |
| 14 | +```bash {tabTitle:pip} |
| 15 | +pip install "sentry-sdk[aiohttp]" |
| 16 | +``` |
| 17 | +```bash {tabTitle:uv} |
| 18 | +uv add "sentry-sdk[aiohttp]" |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +If you're on Python 3.6, you also need the `aiocontextvars` package: |
| 23 | + |
| 24 | +```bash {tabTitle:pip} |
| 25 | +pip install "aiocontextvars" |
| 26 | +``` |
| 27 | +```bash {tabTitle:uv} |
| 28 | +uv add "aiocontextvars" |
| 29 | +``` |
| 30 | + |
| 31 | +## Configure |
| 32 | + |
| 33 | +If you have the `aiohttp` package in your dependencies, the AIOHTTP integration will be enabled automatically when you initialize the Sentry SDK. |
| 34 | + |
| 35 | +<PlatformContent includePath="getting-started-config" /> |
| 36 | + |
| 37 | +## Verify |
| 38 | + |
| 39 | +```python |
| 40 | +from aiohttp import web |
| 41 | + |
| 42 | +sentry_sdk.init(...) # same as above |
| 43 | + |
| 44 | +async def hello(request): |
| 45 | + 1 / 0 # raises an error |
| 46 | + return web.Response(text="Hello, world") |
| 47 | + |
| 48 | +app = web.Application() |
| 49 | +app.add_routes([web.get('/', hello)]) |
| 50 | + |
| 51 | +web.run_app(app) |
| 52 | +``` |
| 53 | + |
| 54 | +When you point your browser to [http://localhost:8080/](http://localhost:8080/) a transaction will be created in the Performance section of [sentry.io](https://sentry.io). Additionally, an error event will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction. |
| 55 | + |
| 56 | +It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 57 | + |
| 58 | +## Behavior |
| 59 | + |
| 60 | +- The Sentry Python SDK will install the AIOHTTP integration for all of your apps. |
| 61 | +- All exceptions leading to an Internal Server Error are reported. |
| 62 | +- _The AIOHTTP integration currently does not attach the request body_, see [GitHub issue](https://github.com/getsentry/sentry-python/issues/220). |
| 63 | +- Logging with any logger will create breadcrumbs when the [Logging](/platforms/python/integrations/logging/) integration is enabled (done by default). |
| 64 | + |
| 65 | +### Tracing |
| 66 | + |
| 67 | +A set of predefined span attributes will be attached to AIOHTTP transactions by default. These can also be used for sampling since they will also be accessible via the `sampling_context` dictionary in the [`traces_sampler`](/platforms/python/configuration/options/#traces_sampler). |
| 68 | + |
| 69 | +- `url.path` |
| 70 | +- `url.query` |
| 71 | +- `url.scheme` |
| 72 | +- `url.full` |
| 73 | +- `http.request.method` |
| 74 | +- `http.request.header.{header}` |
| 75 | +- `server.address` |
| 76 | +- `server.port` |
| 77 | + |
| 78 | +These attributes will also be sent to Sentry. If you don't want that, you can filter them out using a custom [`traces_sampler`](/platforms/python/configuration/options/#before_send) function. |
| 79 | + |
| 80 | +## Options |
| 81 | + |
| 82 | +By adding `AioHttpIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `AioHttpIntegration` to change its behavior: |
| 83 | + |
| 84 | +```python |
| 85 | +import sentry_sdk |
| 86 | +from sentry_sdk.integrations.aiohttp import AioHttpIntegration |
| 87 | + |
| 88 | +sentry_sdk.init( |
| 89 | + # same as above |
| 90 | + integrations=[ |
| 91 | + AioHttpIntegration( |
| 92 | + transaction_style="...", # type: str |
| 93 | + failed_request_status_codes={...} # type: collections.abc.Set[int] |
| 94 | + ), |
| 95 | + ], |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +You can pass the following keyword arguments to `AioHttpIntegration()`: |
| 100 | + |
| 101 | +### `transaction_style` |
| 102 | + |
| 103 | +Configure the way Sentry names transactions: |
| 104 | + |
| 105 | +- `GET /path/{id}` if you set `transaction_style="method_and_path_pattern"` |
| 106 | +- `<module_name>.hello` if you set `transaction_style="handler_name"` |
| 107 | + |
| 108 | +The default is `"handler_name"`. |
| 109 | + |
| 110 | +### `failed_request_status_codes` |
| 111 | + |
| 112 | +A `set` of integers that will determine when an `HTTPException` should be reported to Sentry. The `HTTPException` is reported to Sentry if its status code is contained in the `failed_request_status_codes` set. |
| 113 | + |
| 114 | +Examples of valid `failed_request_status_codes`: |
| 115 | + |
| 116 | +- `{500}` will only report `HTTPException` with status 500 (i.e. `HTTPInternalServerError`). |
| 117 | +- `{400, *range(500, 600)}` will report `HTTPException` with status 400 (i.e. `HTTPBadRequest`) as well as those in the 5xx range. |
| 118 | +- `set()` (the empty set) will not report any `HTTPException` to Sentry. |
| 119 | + |
| 120 | +The default is `{*range(500, 600)}`, meaning that any `HTTPException` with a status in the 5xx range is reported to Sentry. |
| 121 | + |
| 122 | +Regardless of how `failed_request_status_codes` is set, any exceptions raised by the handler, which are not of type `HTTPException` (or a subclass) are reported to Sentry. For example, if your request handler raises an unhandled `AttributeError`, the `AttributeError` gets reported to Sentry, even if you have set `failed_request_status_codes=set()`. |
| 123 | + |
| 124 | + |
| 125 | +## Supported Versions |
| 126 | + |
| 127 | +- AIOHTTP: 3.5+ |
| 128 | +- Python: 3.7+ |
0 commit comments