An aiohttp middleware for reporting errors to Sentry
Project description
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:
Warning
This modifies your logging configuration globally when you instantiate the middleware. Even if you don’t end up using the middleware instance for a request, all your logs will be sent to Sentry.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size aiohttp_sentry-0.6.0-py3-none-any.whl (4.7 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size aiohttp-sentry-0.6.0.tar.gz (3.9 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for aiohttp_sentry-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05ef17aabc74c4c464a405e0a5c95f062f80ad0178d9cb2b5e613d0656741ea6 |
|
MD5 | 104cb7eece9e5cb42205be2fab6757a0 |
|
BLAKE2-256 | bcfb05ebe534849733bf1d94cf6b51e3ec14c58e217e6a8f7ae95bb1a9cfd8ec |