Beispiel #1
0
    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
Beispiel #2
0
def publish_task_from_worker_or_request(headers: dict, **kwargs) -> None:
    """
    Called when a request or celery worker publishes a task to the worker pool
    by calling task.delay(), task.apply_async() or using another equivalent method.
    This is where we transfer state from a parent process to a child process.
    """
    guid = get_guid()
    logger.info('Setting task request header as %s', guid)
    headers[settings.guid_header_name] = guid

    if settings.integration_settings.celery.log_parent:
        current = celery_current.get()
        if current:
            headers[parent_header] = current
Beispiel #3
0
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]
Beispiel #4
0
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'