switch from raven to sentry_sdk

This commit is contained in:
Jens Langhammer 2019-04-04 21:48:50 +02:00
parent 8dbafa4bda
commit a21012bf0c
3 changed files with 29 additions and 33 deletions

View File

@ -3,10 +3,10 @@
import logging import logging
import os import os
import celery from celery import Celery, signals
from django.conf import settings from django.conf import settings
from raven import Client
from raven.contrib.celery import register_logger_signal, register_signal # from raven import Client
# set the default Django settings module for the 'celery' program. # set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.core.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.core.settings")
@ -14,31 +14,18 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.core.settings")
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
class Celery(celery.Celery): CELERY_APP = Celery('passbook')
"""Custom Celery class with Raven configured"""
# pylint: disable=method-hidden
def on_configure(self):
"""Update raven client"""
try:
client = Client(settings.RAVEN_CONFIG.get('dsn'))
# register a custom filter to filter out duplicate logs
register_logger_signal(client)
# hook into the Celery error handler
register_signal(client)
except RecursionError: # This error happens when pdoc is running
pass
# pylint: disable=unused-argument # pylint: disable=unused-argument
@celery.signals.setup_logging.connect @signals.setup_logging.connect
def config_loggers(*args, **kwags): def config_loggers(*args, **kwags):
"""Apply logging settings from settings.py to celery""" """Apply logging settings from settings.py to celery"""
logging.config.dictConfig(settings.LOGGING) logging.config.dictConfig(settings.LOGGING)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@celery.signals.after_task_publish.connect @signals.after_task_publish.connect
def after_task_publish(sender=None, headers=None, body=None, **kwargs): def after_task_publish(sender=None, headers=None, body=None, **kwargs):
"""Log task_id after it was published""" """Log task_id after it was published"""
info = headers if 'task' in headers else body info = headers if 'task' in headers else body
@ -46,22 +33,20 @@ def after_task_publish(sender=None, headers=None, body=None, **kwargs):
# pylint: disable=unused-argument # pylint: disable=unused-argument
@celery.signals.task_prerun.connect @signals.task_prerun.connect
def task_prerun(task_id, task, *args, **kwargs): def task_prerun(task_id, task, *args, **kwargs):
"""Log task_id on worker""" """Log task_id on worker"""
LOGGER.debug('%-40s started (name=%s)', task_id, task.__name__) LOGGER.debug('%-40s started (name=%s)', task_id, task.__name__)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@celery.signals.task_postrun.connect @signals.task_postrun.connect
def task_postrun(task_id, task, *args, retval=None, state=None, **kwargs): def task_postrun(task_id, task, *args, retval=None, state=None, **kwargs):
"""Log task_id on worker""" """Log task_id on worker"""
LOGGER.debug('%-40s finished (name=%s, state=%s)', LOGGER.debug('%-40s finished (name=%s, state=%s)',
task_id, task.__name__, state) task_id, task.__name__, state)
CELERY_APP = Celery('passbook')
# Using a string here means the worker doesn't have to serialize # Using a string here means the worker doesn't have to serialize
# the configuration object to child processes. # the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys # - namespace='CELERY' means all celery-related configuration keys

View File

@ -10,4 +10,4 @@ idna<2.8,>=2.5
markdown markdown
psycopg2 psycopg2
PyYAML PyYAML
raven sentry-sdk

View File

@ -14,7 +14,11 @@ import importlib
import os import os
import sys import sys
from celery.schedules import crontab
from django.contrib import messages from django.contrib import messages
from sentry_sdk import init as sentry_init
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration
from passbook import __version__ from passbook import __version__
from passbook.lib.config import CONFIG from passbook.lib.config import CONFIG
@ -66,7 +70,6 @@ INSTALLED_APPS = [
'django.contrib.postgres', 'django.contrib.postgres',
'rest_framework', 'rest_framework',
'drf_yasg', 'drf_yasg',
'raven.contrib.django.raven_compat',
'passbook.core.apps.PassbookCoreConfig', 'passbook.core.apps.PassbookCoreConfig',
'passbook.admin.apps.PassbookAdminConfig', 'passbook.admin.apps.PassbookAdminConfig',
'passbook.api.apps.PassbookAPIConfig', 'passbook.api.apps.PassbookAPIConfig',
@ -122,7 +125,6 @@ MIDDLEWARE = [
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware',
] ]
ROOT_URLCONF = 'passbook.core.urls' ROOT_URLCONF = 'passbook.core.urls'
@ -204,15 +206,24 @@ CELERY_BROKER_URL = 'amqp://%s' % CONFIG.get('rabbitmq')
CELERY_RESULT_BACKEND = 'rpc://' CELERY_RESULT_BACKEND = 'rpc://'
CELERY_ACKS_LATE = True CELERY_ACKS_LATE = True
CELERY_BROKER_HEARTBEAT = 0 CELERY_BROKER_HEARTBEAT = 0
CELERY_BEAT_SCHEDULE = {
# Raven settings 'cleanup-expired-nonces': {
RAVEN_CONFIG = { 'task': 'passbook.core.tasks.clean_nonces',
'dsn': ('https://55b5dd780bc14f4c96bba69b7a9abbcc:449af483bd0745' 'schedule': crontab(hour=1, minute=1)
'0d83be640d834e5458@sentry.services.beryju.org/8'), }
'release': VERSION,
'environment': 'dev' if DEBUG else 'production',
} }
sentry_init(
dsn=("https://55b5dd780bc14f4c96bba69b7a9abbcc:449af483bd0745"
"0d83be640d834e5458@sentry.services.beryju.org/8"),
integrations=[
DjangoIntegration(),
CeleryIntegration(),
],
send_default_pii=True
)
# CherryPY settings # CherryPY settings
with CONFIG.cd('web'): with CONFIG.cd('web'):
CHERRYPY_SERVER = { CHERRYPY_SERVER = {