Skip to content

Commit

Permalink
add GELF handler for Celery logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aperrin66 committed Oct 9, 2020
1 parent 7dbcabd commit 0c5f447
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FROM ${BASE_IMAGE} as base
RUN pip install --no-cache-dir \
celery==4.4 \
django-celery-results==1.2 \
graypy==2.1.0 \
paramiko==2.7.1 \
redis==3.5 \
scp==0.13.2
Expand Down
13 changes: 13 additions & 0 deletions geospaas_processing/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import shutil

import celery
import celery.utils.log
import celery.signals
import graypy.handler
import scp
from django.db import connection

Expand All @@ -30,6 +33,16 @@
app.config_from_object('django.conf:settings', namespace='CELERY')


@celery.signals.after_setup_logger.connect
def setup_logger(logger, *args, **kwargs): # pylint: disable=unused-argument
"""Set up a GELF handler for Celery tasks if the necessary environment variables are set"""
logging_host = os.getenv('GEOSPAAS_PROCESSING_LOGGING_HOST')
logging_port = os.getenv('GEOSPAAS_PROCESSING_LOGGING_PORT')
if logging_host and logging_port:
gelf_handler = graypy.handler.GELFTCPHandler(logging_host, logging_port, facility=__name__)
logger.addHandler(gelf_handler)


class FaultTolerantTask(celery.Task): # pylint: disable=abstract-method
"""
Workaround for https://github.com/celery/django-celery/issues/121.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest.mock as mock

import celery
import graypy.handler
import scp

import geospaas_processing.downloaders as downloaders
Expand Down Expand Up @@ -54,6 +55,24 @@ def test_retry_if_locked(self):
mock_task.retry.assert_called()


class SignalsTestCase(unittest.TestCase):
"""Unit tests for Celery signal functions"""

def test_setup_logger(self):
"""
The setup_logger() functions must add a GELF handler if the
right environment variables are defined
"""
with mock.patch('os.getenv', return_value='test'):
logger = logging.Logger('test_logger')
tasks.setup_logger(logger)
handler = logger.handlers[0]
self.assertIsInstance(handler, graypy.handler.GELFTCPHandler)
self.assertEqual(handler.host, 'test')
self.assertEqual(handler.port, 'test')
self.assertEqual(handler.facility, tasks.__name__)


class DownloadTestCase(unittest.TestCase):
"""Tests for the download() task"""

Expand Down

0 comments on commit 0c5f447

Please sign in to comment.