From 3d8d93ece57a07f8e0ce2b2cbb2f5ae99ef73ffb Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 17 Sep 2021 12:42:42 +0200 Subject: [PATCH] root: log failed celery tasks to event log Signed-off-by: Jens Langhammer --- authentik/lib/sentry.py | 1 + authentik/root/celery.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/authentik/lib/sentry.py b/authentik/lib/sentry.py index 7b847458f..91b017511 100644 --- a/authentik/lib/sentry.py +++ b/authentik/lib/sentry.py @@ -93,6 +93,7 @@ def before_send(event: dict, hint: dict) -> Optional[dict]: if "exc_info" in hint: _, exc_value, _ = hint["exc_info"] if isinstance(exc_value, ignored_classes): + LOGGER.debug("dropping exception", exception=exc_value) return None if "logger" in event: if event["logger"] in [ diff --git a/authentik/root/celery.py b/authentik/root/celery.py index 16f3065a0..21f8b4ffc 100644 --- a/authentik/root/celery.py +++ b/authentik/root/celery.py @@ -3,10 +3,20 @@ import os from logging.config import dictConfig from celery import Celery -from celery.signals import after_task_publish, setup_logging, task_postrun, task_prerun +from celery.signals import ( + after_task_publish, + setup_logging, + task_failure, + task_internal_error, + task_postrun, + task_prerun, +) from django.conf import settings from structlog.stdlib import get_logger +from authentik.lib.sentry import before_send +from authentik.lib.utils.errors import exception_to_string + # set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authentik.root.settings") @@ -43,6 +53,18 @@ def task_postrun_hook(task_id, task, *args, retval=None, state=None, **kwargs): LOGGER.debug("Task finished", task_id=task_id, task_name=task.__name__, state=state) +# pylint: disable=unused-argument +@task_failure.connect +@task_internal_error.connect +def task_error_hook(task_id, exception: Exception, traceback, *args, **kwargs): + """Create system event for failed task""" + from authentik.events.models import Event, EventAction + + LOGGER.warning("Task failure", exception=exception) + if before_send({}, {"exc_info": (None, exception, None)}) is not None: + Event.new(EventAction.SYSTEM_EXCEPTION, message=exception_to_string(exception)).save() + + # 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