Last active
December 8, 2018 00:10
-
-
Save ashwoods/dca6103d3aec8b94b19bd4bab328f605 to your computer and use it in GitHub Desktop.
Sentry Django & Celery >=4.0 integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If following the official celery documentation for integration django | |
# you might have a `celery.py` file that looks like this. | |
from __future__ import absolute_import, unicode_literals | |
import os | |
import logging | |
from celery import Celery | |
# from celery.utils.log import get_task_logger | |
from celery import signals | |
# Using the standard logging or the recommended celery.utils.log works, | |
# but you need to specify a different name in loggers configuration in settings.py | |
# if `disable_existing_loggers` is set to true. | |
#logger = get_task_logger(__name__) | |
logger = logging.getLogger(__name__) | |
# set the default Django settings module for the 'celery' program. | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local') | |
app = Celery('my_django_app') | |
# Using a string here means the worker doesn't have to serialize | |
# the configuration object to child processes. | |
# - namespace='CELERY' means all celery-related configuration keys | |
# should have a `CELERY_` prefix. | |
app.config_from_object('django.conf:settings', namespace='CELERY') | |
# Load task modules from all registered Django app configs. | |
app.autodiscover_tasks() | |
# Celery > 4.0 tries to configure logging | |
# and very often breaks the configuration. | |
# See: https://github.com/celery/celery/issues/2437 | |
# Need to set `CELERYD_HIJACK_ROOT_LOGGER = False` | |
# in django settings.py and enable the following signal | |
@signals.setup_logging.connect | |
def setup_logging(**kwargs): | |
"""Setup logging.""" | |
pass | |
class RadCeleryException(Exception): | |
pass | |
@app.task(bind=True) | |
def debug_logging(self): | |
logger.error('We just logged something very bad!') | |
@app.task(bind=True) | |
def debug_exception(self): | |
raise RadCeleryException("We are under attack!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# When configuring celery with django integration, there is no need | |
# to follow the celery integration instructions. Just configure | |
# the django integration and the django sentry client should do the rest. | |
# There is a bug with celery > 4.0, so it's necesary to set | |
# CELERYD_HIJACK_ROOT_LOGGER = False | |
# These examples are using django-environ, just change as needed. | |
... | |
... | |
CELERY_BROKER_URL = env('CELERY_BROKER_URL', default='django://') | |
CELERYD_HIJACK_ROOT_LOGGER = False | |
SENTRY_DSN = env('DJANGO_SENTRY_DSN') | |
SENTRY_CLIENT = env('DJANGO_SENTRY_CLIENT', default='raven.contrib.django.raven_compat.DjangoClient') | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': True, # If this is set to true, you will have to add your celery task | |
# loggers to the logger configuration like the example below. | |
# Not required if set to False, but might get more noise from other loggers | |
'root': { | |
'level': 'WARNING', | |
'handlers': ['sentry', ], | |
}, | |
'formatters': { | |
'verbose': { | |
'format': '%(levelname)s %(asctime)s %(module)s ' | |
'%(process)d %(thread)d %(message)s' | |
}, | |
}, | |
'handlers': { | |
'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, | |
}, | |
'django.security.DisallowedHost': { | |
'level': 'ERROR', | |
'handlers': ['console', 'sentry', ], | |
'propagate': False, | |
}, | |
'my_django_app.celery': { | |
'level': 'ERROR', | |
'propagate': True, | |
} | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment