AIOHTTP

Installing Airbrake in a AIOHTTP application

AIOHTTP

The pybrake package makes it quick and easy to monitor your AIOHTTP app’s performance. It only takes a few minutes to start collecting real performance data so let’s jump right in!

Key Features


Installation & Configuration

Set up the Pybrake’s middleware and project config for your web application

Step 1: Install the latest version of pybrake

Airbrake for AIOHTTP uses our official Python notifier pybrake. To install run:

pip install -U pybrake

Step 2: Configure the Airbrake AIOHTTP middleware

First, import AIOHTTP middleware from the pybrake package

from pybrake.middleware.aiohttp import pybrake_middleware

"""
The pybrake provides middleware for error notification and APM.
The default value for the two keyword parameters in pybrake middleware is 'None'.

:param overrides: Override error notification with a dict object based on the answer status code.
                    example as below one.
:param sqlEngine: Pass database connection engine in middleware if your 
                    application uses SQLAlchemy and you wish to monitor 
                    query stats.
"""

Next, register middleware into the AIOHTTP application object and configure the pybrake config parameters

pybrake_middleware = pybrake_middleware()

app = web.Application(middlewares=[pybrake_middleware])

app['PYBRAKE'] = dict(
  project_id=123,
  project_key='FIXME',
  environment='production'  # optional
)

Also, you can pass custom handlers to pybrake_middleware:

# middlewares.py

import aiohttp_jinja2
from pybrake.middleware.aiohttp import pybrake_middleware


async def handle_404(request):
    return aiohttp_jinja2.render_template('404.html', request, {})


async def handle_500(request):
    return aiohttp_jinja2.render_template('500.html', request, {})


def setup_middlewares(app):
    pybrake_middleware = pybrake_middleware(override={
        404: handle_404,
        500: handle_500
    })

    app.middlewares.append(pybrake_middleware)

Now you are ready to start reporting errors and performance monitoring to Airbrake from your AIOHTTP app. Please look into sample example to get more idea.


Additional Settings

Please visit our Pybrake overview for details on useful features like:


Troubleshoot

If you have any questions or concerns, please address them here.