Skip to content

Celery needs to be instrumented on client side for distributed tracing #972

@alexmojaki

Description

@alexmojaki

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 open-telemetry/opentelemetry-python-contrib#1002 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions