2

I'm using Django-RQ in a Heroku application to handle background tasks.

When an error occurs in my background tasks, it doesn't get sent to Sentry.

My logging settings in settings.py are below:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
        "rq_console": {
            "format": "%(asctime)s %(message)s",
            "datefmt": "%H:%M:%S",
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        "rq_console": {
            "level": "DEBUG",
            "class": "rq.utils.ColorizingStreamHandler",
            "formatter": "rq_console",
            "exclude": ["%(asctime)s"],
        },
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        "rq.worker": {
            "handlers": ["rq_console", "sentry"],
            "level": "DEBUG"
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

Appreciate any tips on what I'm doing wrong.

cristiano2lopes
  • 3,168
  • 2
  • 18
  • 22
AJ Hung
  • 21
  • 2

1 Answers1

5

You will need to develop a custom worker and register the sentry handler with python-rq.

I'am assuming you're using rq.Worker as the worker class (the default on django-rq).

import rq
from raven import Client
from raven.transport import HTTPTransport
from rq.contrib.sentry import register_sentry

class SentryAwareWorker(rq.Worker):

    def __init__(self, *args, **kwargs):
        super(SentryAwareWorker, self).__init__(*args, **kwargs)
        dsn = settings.RAVEN_CONFIG['dsn']
        client = Client(dsn, transport=HTTPTransport)
        register_sentry(client, self)

I present an option to get sentry dsn and to initialise the sentry client but you can do it however you want. The important part is register_sentry(client, self).

Then run your worker with:

python manage.py rqworker queue_1 queue_2 queue_n --worker-class path.to.SentryAwareWorker
cristiano2lopes
  • 3,168
  • 2
  • 18
  • 22
  • 2
    django-rq "now" allows to configure the custom worker class (and jobs) in settings: https://github.com/rq/django-rq#custom-job-and-worker-classes – ezdazuzena Nov 22 '18 at 16:07