Skip to content

Instantly share code, notes, and snippets.

@mattrobenolt
Forked from mallyvai/wsgi.py
Last active October 27, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrobenolt/a9c4a80ea0cbb298d22e to your computer and use it in GitHub Desktop.
Save mattrobenolt/a9c4a80ea0cbb298d22e to your computer and use it in GitHub Desktop.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from raven.utils.wsgi import (
get_current_url, get_headers, get_environ)
class Sentry(object):
def __init__(self, application):
self.application = application
@property
def client(self):
from raven.contrib.django.models import client
return client
def __call__(self, environ, start_response):
self.client.http_context(self.get_http_context(environ))
try:
iterable = self.application(environ, start_response)
except Exception:
self.handle_exception(environ)
raise
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
try:
for event in iterable:
yield event
except Exception:
self.handle_exception(environ)
raise
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
finally:
if iterable and hasattr(iterable, 'close') and callable(iterable.close):
try:
iterable.close()
except Exception:
self.handle_exception(environ)
except SystemExit as e:
if e.code != 0:
self.handle_exception(environ)
raise
self.client.context.clear()
def get_http_context(self, environ):
return {
'method': environ.get('REQUEST_METHOD'),
'url': get_current_url(environ, strip_querystring=True),
'query_string': environ.get('QUERY_STRING'),
'headers': dict(get_headers(environ)),
'env': dict(get_environ(environ)),
}
def process_response(self, request, response):
self.client.context.clear()
def handle_exception(self, environ=None):
return self.client.captureException()
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = Sentry(application)
application = DjangoWhiteNoise(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment