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.

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="...",  # type: str
            failed_request_status_codes={...}  # type: collections.abc.Set[int]
        ),
    ],
)

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".

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.

Examples of valid failed_request_status_codes:

  • {500} will only report HTTPException with status 500 (i.e. HTTPInternalServerError).
  • {400, *range(500, 600)} will report HTTPException with status 400 (i.e. HTTPBadRequest) as well as those in the 5xx range.
  • set() (the empty set) will not report any HTTPException to Sentry.

The default is {*range(500, 600)}, meaning that any HTTPException with a status in the 5xx range is reported to Sentry.

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().

  • 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").