pydantic/logfire

Celery needs to be instrumented on client side for distributed tracing

Open

#972 创建于 2025年3月31日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)Python (244 fork)github user discovery
documentationgood first issue

仓库指标

Star
 (4,282 star)
PR 合并指标
 (平均合并 7天 8小时) (30 天内合并 35 个 PR)

描述

Demo:

import logfire
from celery import Celery
from celery.signals import worker_init
from fastapi import FastAPI

@worker_init.connect()
def init_worker(*args, **kwargs):
    logfire.configure(service_name="worker")
    logfire.instrument_celery()

app = Celery("tasks", broker="redis://localhost:6379/0")

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

fapp = FastAPI()

logfire.configure()
logfire.instrument_fastapi(fapp)
########### Important:
logfire.instrument_celery()


@fapp.get("/hello")
async def hello(name: str):
    add.delay(42, 50)
    return {"message": f"hello {name}"}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(fapp)

The top-level logfire.instrument_celery() is required for the context propagation, based on https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1002#issuecomment-1275273134

This should be documented at least, maybe fixed in some other way, e.g. in OTel.

贡献者指南