Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery tasks uncaught exceptions not being sent to Sentry

I'm currently working with Celery tasks in a Django based project. We have raven configured to send all uncaught exceptions and log messages to Sentry, as described in the documentation.

Everything works pretty good, except for uncaught exceptions inside celery tasks. For example, if I run this task:

@app.task
def test_logging():
    log.error('Testing logging inside a task')
    raise IndexError('Testing exception inside a task')

I only see in Sentry the log.error(...) but not the IndexError uncaught exception. I've tried using a try-except block around the exception with a log.exception(...) inside and it did work, but I think it's not scalable to catch all exceptions like this.

So, the problem are only uncaught exceptions that somehow are not handled properly.

These are my current package versions:

celery (3.1.17)
raven (5.1.1)
Django (1.7.1)

Would you help me to move in some direction?

Thanks for your time!

like image 851
Martin Zugnoni Avatar asked Dec 18 '14 16:12

Martin Zugnoni


1 Answers

As described by DRC in the comment up there, we finally got to the solution using this approach: https://docs.getsentry.com/hosted/clients/python/integrations/celery/

Basically doing this:

import celery

class Celery(celery.Celery):

    def on_configure(self):
        if hasattr(settings, 'RAVEN_CONFIG') and settings.RAVEN_CONFIG['dsn']:
            import raven
            from raven.contrib.celery import (register_signal,
                                              register_logger_signal)

            client = raven.Client(settings.RAVEN_CONFIG['dsn'])
            register_logger_signal(client)
            register_signal(client)


app = Celery('myapp')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

Thanks for your time.

like image 169
Martin Zugnoni Avatar answered Oct 01 '22 10:10

Martin Zugnoni