Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 356
Elrond Avatar asked Sep 27 '13 21:09

Elrond


2 Answers

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)
like image 105
habnabit Avatar answered Nov 23 '22 22:11

habnabit


user1252307 answer is a great start, but on the sentry side you get a relatively unhelpful dictionary and no stack trace.

If you are trying to view and track down unexpected exceptions try this small change to the log_sentry function:

from twisted.python import log
from raven import Client

client = Client(dsn='twisted+http://YOUR_DSN_HERE')

def log_sentry(dictionary):
    if dictionary.get('isError'):
        if 'failure' in dictionary:
            client.captureException() # Send the current exception info to Sentry.
        else:
            #format the dictionary in whatever way you want
            client.captureMessage(dictionary)

log.addObserver(log_sentry)

There maybe a better way to filter non exception based error message, and this might try to emit exception information that doesn't exist where there are failures which aren't exceptions.

like image 27
amjoconn Avatar answered Nov 24 '22 00:11

amjoconn