Fix metrics not initialized in Celery prefork worker processes
worker_ready fires in the main process after pool workers are forked, so init_metrics() never ran in the child processes. Add a worker_process_init handler to initialize OTel metrics in each worker.
This commit is contained in:
parent
51e7a7ccc5
commit
f6f0aa8d14
1 changed files with 9 additions and 2 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import sys
|
||||
import time
|
||||
from celery import Celery
|
||||
from celery.signals import worker_ready, task_prerun, task_postrun
|
||||
from celery.signals import worker_ready, worker_process_init, task_prerun, task_postrun
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
|
|
@ -35,9 +35,16 @@ CELERY_METRICS_PORT = int(os.getenv("CELERY_METRICS_PORT", "9090"))
|
|||
_task_start_times: dict[str, float] = {}
|
||||
|
||||
|
||||
@worker_process_init.connect
|
||||
def _init_worker_metrics(**kwargs: object) -> None:
|
||||
"""Initialise OTel metrics in each prefork worker process."""
|
||||
from api.metrics import init_metrics
|
||||
init_metrics(os.getenv("SERVICE_NAME", "celery-worker"))
|
||||
|
||||
|
||||
@worker_ready.connect
|
||||
def _start_metrics_server(**kwargs: object) -> None:
|
||||
"""Start a lightweight Prometheus HTTP server in the worker process."""
|
||||
"""Start a lightweight Prometheus HTTP server in the main worker process."""
|
||||
from api.metrics import init_metrics
|
||||
init_metrics(os.getenv("SERVICE_NAME", "celery-worker"))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue