|
| 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 | +## Configure |
| 22 | + |
| 23 | +If you have the `aiohttp` package in your dependencies, the AIOHTTP integration will be enabled automatically when you initialize the Sentry SDK. |
| 24 | + |
| 25 | +<PlatformContent includePath="getting-started-config" /> |
| 26 | + |
| 27 | +## Verify |
| 28 | + |
| 29 | +```python |
| 30 | +from aiohttp import web |
| 31 | + |
| 32 | +sentry_sdk.init(...) # same as above |
| 33 | + |
| 34 | +async def hello(request): |
| 35 | + 1 / 0 # raises an error |
| 36 | + return web.Response(text="Hello, world") |
| 37 | + |
| 38 | +app = web.Application() |
| 39 | +app.add_routes([web.get('/', hello)]) |
| 40 | + |
| 41 | +web.run_app(app) |
| 42 | +``` |
| 43 | + |
| 44 | +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. |
| 45 | + |
| 46 | +It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 47 | + |
| 48 | +## Behavior |
| 49 | + |
| 50 | +- The Sentry Python SDK will install the AIOHTTP integration for all of your apps. |
| 51 | +- All exceptions leading to an Internal Server Error are reported. |
| 52 | +- _The AIOHTTP integration currently does not attach the request body_, see [GitHub issue](https://github.com/getsentry/sentry-python/issues/220). |
| 53 | +- Logging with any logger will create breadcrumbs when the [Logging](/platforms/python/integrations/logging/) integration is enabled (done by default). |
| 54 | + |
| 55 | +### Tracing |
| 56 | + |
| 57 | +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). |
| 58 | + |
| 59 | +- `url.path` |
| 60 | +- `url.query` |
| 61 | +- `url.scheme` |
| 62 | +- `url.full` |
| 63 | +- `http.request.method` |
| 64 | +- `http.request.header.{header}` |
| 65 | +- `server.address` |
| 66 | +- `server.port` |
| 67 | + |
| 68 | +These attributes will also be sent to Sentry. If you don't want that, you can filter them out using a custom [`before_send`](/platforms/python/configuration/options/#before_send) function. |
| 69 | + |
| 70 | +## Options |
| 71 | + |
| 72 | +By adding `AioHttpIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `AioHttpIntegration` to change its behavior: |
| 73 | + |
| 74 | +```python |
| 75 | +import sentry_sdk |
| 76 | +from sentry_sdk.integrations.aiohttp import AioHttpIntegration |
| 77 | + |
| 78 | +sentry_sdk.init( |
| 79 | + # same as above |
| 80 | + integrations=[ |
| 81 | + AioHttpIntegration( |
| 82 | + transaction_style="...", # type: str |
| 83 | + failed_request_status_codes={...} # type: collections.abc.Set[int] |
| 84 | + ), |
| 85 | + ], |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +You can pass the following keyword arguments to `AioHttpIntegration()`: |
| 90 | + |
| 91 | +### `transaction_style` |
| 92 | + |
| 93 | +Configure the way Sentry names transactions: |
| 94 | + |
| 95 | +- `GET /path/{id}` if you set `transaction_style="method_and_path_pattern"` |
| 96 | +- `<module_name>.hello` if you set `transaction_style="handler_name"` |
| 97 | + |
| 98 | +The default is `"handler_name"`. |
| 99 | + |
| 100 | +### `failed_request_status_codes` |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +Examples of valid `failed_request_status_codes`: |
| 105 | + |
| 106 | +- `{500}` will only report `HTTPException` with status 500 (i.e. `HTTPInternalServerError`). |
| 107 | +- `{400, *range(500, 600)}` will report `HTTPException` with status 400 (i.e. `HTTPBadRequest`) as well as those in the 5xx range. |
| 108 | +- `set()` (the empty set) will not report any `HTTPException` to Sentry. |
| 109 | + |
| 110 | +The default is `{*range(500, 600)}`, meaning that any `HTTPException` with a status in the 5xx range is reported to Sentry. |
| 111 | + |
| 112 | +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()`. |
| 113 | + |
| 114 | + |
| 115 | +## Supported Versions |
| 116 | + |
| 117 | +- AIOHTTP: 3.5+ |
| 118 | +- Python: 3.7+ |
0 commit comments