Skip to content

Instantly share code, notes, and snippets.

@beniwohli
Created July 2, 2015 20:58
Show Gist options
  • Save beniwohli/8fcaab7e7fa4c63034a9 to your computer and use it in GitHub Desktop.
Save beniwohli/8fcaab7e7fa4c63034a9 to your computer and use it in GitHub Desktop.
import logging
from django.conf import settings as django_settings
from opbeat.contrib.django.models import client
def _is_ignorable_404(uri):
"""
Returns True if the given request *shouldn't* notify the site managers.
"""
urls = getattr(django_settings, 'IGNORABLE_404_URLS', ())
return any(pattern.search(uri) for pattern in urls)
class Opbeat404CatchMiddleware(object):
def process_response(self, request, response):
if (response.status_code != 404
or _is_ignorable_404(request.get_full_path())):
return response
data = client.get_data_from_request(request)
data.update({
'level': logging.INFO,
'logger': 'http404',
})
result = client.capture_message(
message='Page Not Found: %s' % request.build_absolute_uri(),
data=data,
)
request.opbeat = {
'app_id': data.get('app_id', client.app_id),
'id': client.get_ident(result),
}
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment