Skip to main content

An aiohttp middleware for reporting errors to Sentry

Project description

CI Status

An aiohttp server middleware for reporting failed requests to Sentry

Usage

Just add SentryMiddleware as a middleware:

from aiohttp import web
from aiohttp_sentry import SentryMiddleware
app = web.Application(middlewares=[SentryMiddleware()])

Configuration

If you want to customize error reporting, you can use the optional sentry_kwargs parameter, which is a dict of kwargs passed to the lower-level Sentry library, raven. With this, you can specify environment details, filter out specific exceptions, and so on:

from aiohttp import web
from aiohttp_sentry import SentryMiddleware
app = web.Application(
    middlewares=(
        SentryMiddleware({
            'environment': 'foo',
            'release': 'bar',
            'ignore_exceptions': 'aiohttp.HTTPClientError'
        }),
        # ...
    ),
)

If you are using the standard library’s logging module, we have a convenient parameter to patch it for you, to have logger calls send events to Sentry automatically:

import logging
from aiohttp import web
from aiohttp_sentry import SentryMiddleware

app = web.Application(
    middlewares=[SentryMiddleware(patch_logging=True, sentry_log_level=logging.WARNING)],
)

Attaching Data to Events

By default, aiohttp-sentry passes this data alongside reported exceptions:

  • HTTP scheme

  • HTTP method

  • URL

  • Query String

  • Request Headers (including cookies)

  • Requester’s IP address

If you need more data in sentry, you can do that by subclassing from SentryMiddleware and overriding the get_extra_data method, which returns all the above by default. Here’s what that looks like:

class DetailedSentryMiddleware(SentryMiddleware):

    async def get_extra_data(self, request):
        return {
            **await super().get_extra_data(request),
            'settings': request.app['settings'],
        }

While get_extra_data is a coroutine, which means it can make database queries, API calls, or other I/O operations, use this carefully! Make sure you understand the implications of executing expensive operations every time an error happens. If the root cause of the error is an overloaded database, you are just going to make the problem worse, while not even being able to get the extra info you wanted.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-sentry-0.6.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

aiohttp_sentry-0.6.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-sentry-0.6.0.tar.gz.

File metadata

  • Download URL: aiohttp-sentry-0.6.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for aiohttp-sentry-0.6.0.tar.gz
Algorithm Hash digest
SHA256 ea059c72553ae12f66bac020d4af98aa37399754b647f01258b5ec04f34d3bda
MD5 d164d4816474f9f64e3f44db5a4527e7
BLAKE2b-256 7833b0053af4c7027e88606e8ff2498a7f4fe8c36397619bfcd07b5a20f5ffc5

See more details on using hashes here.

File details

Details for the file aiohttp_sentry-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_sentry-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for aiohttp_sentry-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05ef17aabc74c4c464a405e0a5c95f062f80ad0178d9cb2b5e613d0656741ea6
MD5 104cb7eece9e5cb42205be2fab6757a0
BLAKE2b-256 bcfb05ebe534849733bf1d94cf6b51e3ec14c58e217e6a8f7ae95bb1a9cfd8ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page