Created
January 11, 2013 02:56
-
-
Save cgallemore/4507616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_data_from_request(request): | |
""" | |
Convenience function to create data from a webapp2 request object. | |
:param request: | |
:return: dict | |
""" | |
# request.params combines the GET and POST data, this would give us some | |
# extra info to this particular request. | |
params = dict(request.params) | |
# We try and set the user on the request, so let's try and grab it and set | |
# in in our extra data. | |
params.update({'user': getattr(request, 'user', '')}) | |
return { | |
'sentry.interfaces.Http': { | |
'method': request.method, | |
'url': request.path_url, | |
'data': params, # Shows up in sentry under the Body section of the Request. | |
'query_string': request.query_string, | |
'headers': dict(request.headers), | |
'env': dict(( | |
('REMOTE_ADDR', request.environ['REMOTE_ADDR']), | |
('SERVER_NAME', request.environ['SERVER_NAME']), | |
('SERVER_PORT', request.environ['SERVER_PORT']), | |
)), | |
} | |
} | |
class Sentry(object): | |
def __init__(self, config, client_cls=Client, is_enabled=True): | |
self.client = None | |
if is_enabled: | |
self.client = client_cls( | |
dsn=config.SENTRY.get('dsn'), | |
name=config.SENTRY.get('name', get_default_version_hostname()), | |
servers=config.SENTRY.get('sentry_servers'), | |
key=config.SENTRY.get('sentry_key'), | |
public_key=config.SENTRY.get('public_key'), | |
secret_key=config.SENTRY.get('secret_key'), | |
project=config.SENTRY.get('project'), | |
site=config.SENTRY.get('site'), | |
include_paths=config.SENTRY.get('include_paths'), | |
exclude_paths=config.SENTRY.get('exclude_paths') | |
) | |
def capture_exception(self, *args, **kwargs): | |
if not self.client: | |
return | |
request = kwargs.get('request', None) | |
data = kwargs.get('data', {}) | |
if request: | |
data.update(get_data_from_request(request)) | |
return self.client.captureException(exc_info=kwargs.get('exc_info'), | |
data=data) | |
def capture_message(self, message, **kwargs): | |
if not self.client: | |
return | |
return self.client.captureMessage(message, **kwargs) | |
class SentryMixin(object): | |
""" | |
Mixin to provide basic support to log events to Sentry. | |
""" | |
sentry = Sentry(config) | |
def handle_exception(self, exception, debug): | |
if not isinstance(exception, HTTPNotFound): | |
setattr(self.request, 'user', self.get_current_user()) | |
self.sentry.capture_exception(request=self.request, exc_info=True) | |
super(SentryMixin, self).handle_exception(exception, debug) | |
def _capture_query(self, exc): | |
# Capture the SQL query and log to sentry | |
if self.sentry.client: | |
self.sentry.capture_exception(data={ | |
'sentry.interfaces.Query': { | |
'query': exc.statement | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment