API¶
mixins.sentry
A RequestHandler mixin for sending exceptions to Sentry
-
class
sprockets.mixins.sentry.
SanitizeEmailsProcessor
(client)¶ Remove all email addresses from the payload sent to sentry.
-
class
sprockets.mixins.sentry.
SentryMixin
(*args, **kwargs)¶ Report unexpected exceptions to Sentry.
Mix this in over a
tornado.web.RequestHandler
to report unhandled exceptions to Sentry so that you can figure out what went wrong. In order to use this mix-in, all that you have to do is define the SENTRY_DSN environment variable that contains your projects Sentry DSN. Whenever a request comes it and the environment variable is set, this mix-in will create a newraven.base.Client
instance and make it available via thesentry_client
property.If an exception is caught by
_handle_request_exception()
, then it will be reported to Sentry in all it’s glory.-
sentry_client
¶ The
raven.base.Client
instance orNone
if sentry reporting is disabled. You can modify attributes of the client as required for your application – for example, you can add new modules by adding toself.sentry_client.include_paths
.
A
dict
of tag and value pairs to associated with any reported exceptions.
-
-
sprockets.mixins.sentry.
get_client
(application)¶ Retrieve the sentry client for application.
Parameters: application (tornado.web.Application) – application to retrieve the sentry client for. Returns: a raven.base.Client
instance orNone
Return type: raven.base.Client
-
sprockets.mixins.sentry.
install
(application, **kwargs)¶ Call this to install a sentry client into a Tornado application.
Parameters: - application (tornado.web.Application) – the application to install the client into.
- kwargs – keyword parameters to pass to the
raven.base.Client
initializer.
Returns: True
if the client was installed by this call andFalse
otherwise.This function should be called to initialize the Sentry client for your application. It will be called automatically with the default parameters by
SentryMixin
if you do not call it during the creation of your application. You should install the client explicitly so that you can set at least the following properties:- include_paths list of python modules to include in tracebacks.
This function ensures that
raven
,sprockets
,sys
, andtornado
are included but you probably want to include additional packages. - release the version of the application that is running
See the raven documentation for additional information.
Examples¶
The following application will report errors to sentry if you export the
SENTRY_DSN
environment variable and make a request to
http://localhost:8000/whatever provided that whatever is not an integer.
import sys
import signal
from tornado import ioloop, web
from sprockets.mixins import sentry
class Handler(sentry.SentryMixin, web.RequestHandler):
def initialize(self, **kwargs):
tags = kwargs.pop('tags', dict())
super().initialize(**kwargs)
self.sentry_tags.update(tags)
def get(self, status_code):
self.set_status(int(status_code))
def make_application(app_tags):
application = web.Application([(r'/(\S+)', Handler)])
sentry.install(application, include_paths=[__name__], tags=app_tags)
return application
def stop(signo, frame):
iol = ioloop.IOLoop.current()
iol.add_callback_from_signal(iol.stop)
if __name__ == '__main__':
app_tags = {}
for arg in sys.argv[1:]:
name, _, value = arg.partition('=')
app_tags[name] = value
signal.signal(signal.SIGINT, stop)
app = make_application(app_tags)
app.listen(8000)
ioloop.IOLoop.current().start()