Search code examples
twistedsentryraven

What is the Recommended Twisted Sentry/Raven Integration?


There are many integrations for raven, including python logging. On the one side, twisted does not use python's logging. And on the other side, there is no direct integration for raven in twisted.

So what is the current best practice for using raven in a twisted based setup?


Solution

  • raven's captureException can only be called without arguments if there is an exception active, which is not always the case when a log observer is called. So, instead, pull the exception information out of the Failure that gets logged:

    from twisted.python import log
    from raven import Client
    
    
    client = Client(dsn='twisted+http://YOUR_DSN_HERE')
    
    def logToSentry(event):
        if not event.get('isError') or 'failure' not in event:
            return
    
        f = event['failure']
        client.captureException((f.type, f.value, f.getTracebackObject()))
    
    log.addObserver(logToSentry)