Exemplo n.º 1
0
def worker_prerun(task: Task, **kwargs) -> None:
    """
    Called before a worker starts executing a task.
    Here we make sure to set the appropriate correlation ID for all logs logged
    during the tasks, and on the thread in general. In that regard, this does
    the Celery equivalent to what the django-guid middleware does for a request.
    """
    guid = task.request.get(settings.guid_header_name)
    if guid:
        logger.info('Setting GUID %s', guid)
        set_guid(guid)
    else:
        generated_guid = generate_guid(
            uuid_length=settings.integration_settings.celery.uuid_length)
        logger.info('Generated GUID %s', generated_guid)
        set_guid(generated_guid)

    if settings.integration_settings.celery.log_parent:
        origin = task.request.get(parent_header)
        if origin:
            logger.info('Setting parent ID %s', origin)
            celery_parent.set(origin)
        generated_current_guid = generate_guid(
            uuid_length=settings.integration_settings.celery.uuid_length)
        logger.info('Generated current ID %s', generated_current_guid)
        celery_current.set(generated_current_guid)
Exemplo n.º 2
0
def test_uuid_length():
    """
    Make sure passing uuid_length works.
    """
    for i in range(33):
        guid = generate_guid(uuid_length=i)
        assert len(guid) == i
Exemplo n.º 3
0
def test_uuid_length_setting():
    """
    Make sure that the settings value is used as a default.
    """
    for i in range(33):
        mocked_settings = django_settings.DJANGO_GUID
        mocked_settings['UUID_LENGTH'] = i
        with override_settings(DJANGO_GUID=mocked_settings):
            guid = generate_guid()
            assert len(guid) == i
Exemplo n.º 4
0
def test_dont_set_transaction_id(monkeypatch, caplog):
    """
    Tests that the `configure_scope()` is not executed, given `sentry_integration=False` in CeleryIntegration
    """
    logger = logging.getLogger('django_guid.celery')
    logger.addHandler(caplog.handler)

    mocked_settings = deepcopy(django_settings.DJANGO_GUID)
    mocked_settings['INTEGRATIONS'] = [
        CeleryIntegration(sentry_integration=False)
    ]
    with override_settings(DJANGO_GUID=mocked_settings):
        settings = Settings()
        monkeypatch.setattr('django_guid.integrations.celery.signals.settings',
                            settings)
        guid = generate_guid()
        set_transaction_id(guid)
    logger.removeHandler(caplog.handler)
    assert f'Setting Sentry transaction_id to {guid}' not in [
        record.message for record in caplog.records
    ]
Exemplo n.º 5
0
 def run():
     ppid = os.getppid()
     logger.warning('periodic beat started')
     while True:
         if os.getppid() != ppid:
             # if the parent PID changes, this process has been orphaned
             # via e.g., segfault or sigkill, we should exit too
             pid = os.getpid()
             logger.warning(
                 f'periodic beat exiting gracefully pid:{pid}')
             raise SystemExit()
         try:
             for conn in connections.all():
                 # If the database connection has a hiccup, re-establish a new
                 # connection
                 conn.close_if_unusable_or_obsolete()
             set_guid(generate_guid())
             self.run_pending()
         except Exception:
             logger.exception(
                 'encountered an error while scheduling periodic tasks')
         time.sleep(idle_seconds)
Exemplo n.º 6
0
def test_set_transaction_id(monkeypatch, caplog):
    """
    Tests that the `configure_scope()` is executed, given `sentry_integration=True` in CeleryIntegration
    """
    # https://github.com/eisensheng/pytest-catchlog/issues/44
    logger = logging.getLogger(
        'django_guid.celery'
    )  # Ensure caplog can catch logs with `propagate=False`
    logger.addHandler(caplog.handler)

    mocked_settings = deepcopy(django_settings.DJANGO_GUID)
    mocked_settings['INTEGRATIONS'] = [
        CeleryIntegration(sentry_integration=True)
    ]
    with override_settings(DJANGO_GUID=mocked_settings):
        settings = Settings()
        monkeypatch.setattr('django_guid.integrations.celery.signals.settings',
                            settings)
        guid = generate_guid()
        set_transaction_id(guid)
    logger.removeHandler(caplog.handler)  # Remove handler before test finish
    assert f'Setting Sentry transaction_id to {guid}' in [
        record.message for record in caplog.records
    ]