AIOHTTP

Learn about using Sentry with AIOHTTP.

The AIOHTTP integration adds support for the AIOHTTP server web framework.

If you use AIOHTTP as your HTTP client and want to instrument outgoing HTTP requests, have a look at the AIOHTTP client documentation.

Install sentry-sdk from PyPI with the aiohttp extra:

Copied
pip install --upgrade sentry-sdk[aiohttp]

If you're on Python 3.6, you also need the aiocontextvars package:

Copied
pip install --upgrade aiocontextvars

If you have the aiohttp package in your dependencies, the AIOHTTP integration will be enabled automatically when you initialize the Sentry SDK.

Configuration should happen as early as possible in your application's lifecycle.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
)

Copied
from aiohttp import web

sentry_sdk.init(...)  # same as above

async def hello(request):
    1 / 0  # raises an error
    return web.Response(text="Hello, world")

app = web.Application()
app.add_routes([web.get('/', hello)])

web.run_app(app)

When you point your browser to http://localhost:8080/ a transaction will be created in the Performance section of sentry.io. 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.

  • The Sentry Python SDK will install the AIOHTTP integration for all of your apps.
  • All exceptions leading to an Internal Server Error are reported.
  • The AIOHTTP integration currently does not attach the request body, see GitHub issue.
  • Logging with any logger will create breadcrumbs when the Logging integration is enabled (done by default).

By adding AioHttpIntegration to your sentry_sdk.init() call explicitly, you can set options for AioHttpIntegration to change its behavior:

Copied
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

sentry_sdk.init(
    # same as above
    integrations=[
        AioHttpIntegration(
            transaction_style="method_and_path_pattern",
        ),
    ],
)

You can pass the following keyword arguments to AioHttpIntegration():

Configure the way Sentry names transactions:

  • GET /path/{id} if you set transaction_style="method_and_path_pattern"
  • <module_name>.hello if you set transaction_style="handler_name"

The default is "handler_name".

  • AIOHTTP: 3.5+
  • Python: 3.7+
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").