Skip to content

celery

logfire.instrument_celery() Method creates a span for each task performed by the Celery worker.

install

Install logfire using 额外的芹菜:

{{ install_logfire(extras=['芹菜']) }}

usage

You need a message broker to run Celery. In this example, we will run RabbitMQ on the docker container. You can run it as follows:

docker run -d --hostname my-rabbit \
    --name some-rabbit \
    # -e RABBITMQ_DEFAULT_USER=user \
    # -e RABBITMQ_DEFAULT_PASS=password \
    rabbitmq:3-management

Below we have a minimal example of using celery. You can run it with celery -A tasks worker --loglevel=info:

logfire.configure()

@worker_process_init.connect(weak=False) def init_celery_tracing(args, *kwargs): logfire.instrument_celery()

app = Celery("tasks", broker="pyamqp://localhost//") # (1)!

@app.task def add(x, y): return x + y

add.delay(42, 50) ```

  1. 使用 pip install pyamqp 安装 pyamqp。

logfire.instrument_celery() 传递给 CeleryInstrumentor().instrument() 方法。


本文总阅读量次