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 os
import celery
from celery import Celery, signals
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.
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__)
class Celery(celery.Celery):
"""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
CELERY_APP = Celery('passbook')
# pylint: disable=unused-argument
@celery.signals.setup_logging.connect
@signals.setup_logging.connect
def config_loggers(*args, **kwags):
"""Apply logging settings from settings.py to celery"""
logging.config.dictConfig(settings.LOGGING)
# 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):
"""Log task_id after it was published"""
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
@celery.signals.task_prerun.connect
@signals.task_prerun.connect
def task_prerun(task_id, task, *args, **kwargs):
"""Log task_id on worker"""
LOGGER.debug('%-40s started (name=%s)', task_id, task.__name__)
# 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):
"""Log task_id on worker"""
LOGGER.debug('%-40s finished (name=%s, state=%s)',
task_id, task.__name__, state)
CELERY_APP = Celery('passbook')
# 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

View File

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

View File

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