def filter(self, record: LogRecord) -> bool: """ Sets two record attributes: celery parent and celery current. Celery origin is the tracing ID of the process that spawned the current process, and celery current is the current process' tracing ID. In other words, if a worker sent a task to be executed by the worker pool, that celery worker's `current` tracing ID would become the next worker's `origin` tracing ID. """ record.celery_parent_id: str = celery_parent.get() # type: ignore record.celery_current_id: str = celery_current.get() # type: ignore return True
def test_cleanup(monkeypatch, mocker: MockerFixture): """ Test that cleanup works as expected """ set_guid('123') celery_current.set('123') celery_parent.set('123') mocked_settings = deepcopy(django_settings.DJANGO_GUID) mocked_settings['INTEGRATIONS'] = [CeleryIntegration(log_parent=True)] with override_settings(DJANGO_GUID=mocked_settings): settings = Settings() monkeypatch.setattr('django_guid.integrations.celery.signals.settings', settings) clean_up(task=mocker.Mock()) assert [get_guid(), celery_current.get(), celery_parent.get()] == [None, None, None]
def test_worker_prerun_guid_log_parent_with_origin(monkeypatch, mocker: MockerFixture, mock_uuid_two_unique): """ Tests that depth works when there is an origin """ from django_guid.integrations.celery.signals import parent_header mock_task = mocker.Mock() mock_task.request = { 'Correlation-ID': None, parent_header: '1234' } # No origin mocked_settings = deepcopy(django_settings.DJANGO_GUID) mocked_settings['INTEGRATIONS'] = [CeleryIntegration(log_parent=True)] with override_settings(DJANGO_GUID=mocked_settings): settings = Settings() monkeypatch.setattr('django_guid.integrations.celery.signals.settings', settings) worker_prerun(mock_task) assert get_guid() == '704ae5472cae4f8daa8f2cc5a5a8mock' assert celery_current.get() == 'c494886651cd4baaa8654e4d24a8mock' assert celery_parent.get() == '1234'