Instrument Celery: trace every task your workers run
See every background task your Celery workers run: the task name, how long it took, and whether it failed, as a span (one unit of work with a name, a start, and a duration) in Logfire. Spans link together into a trace (the full journey of one request, made of nested spans). Enable tracing in the sender and the worker to see the task in the trace that started it. You can also trace tasks that Celery beat starts on a schedule.
- Each task run as a span, with its duration and status
- The task name and the arguments it ran with
- Failed tasks, with the error
- Tasks fired on a schedule by Celery beat
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
Install logfire with the celery extra:
pip install 'logfire[celery]'
uv add 'logfire[celery]'
The example uses Celery with Redis support. Install it in the same environment as Logfire.
pip install 'celery[redis]'
Call logfire.configure(), then logfire.instrument_celery() to
record every task. Call both inside Celery’s worker_process_init signal so they run once in each
worker process, after the prefork pool forks it.
The example below uses Redis as the broker. You can start one with Docker:
docker run --rm -d -p 127.0.0.1:6379:6379 redis
Save the task and the worker setup in tasks.py.
from celery import Celery
from celery.signals import worker_process_init
import logfire
@worker_process_init.connect(weak=False) # (1)
def init_worker(*args, **kwargs):
logfire.configure(service_name='worker', distributed_tracing=True) # (2)
logfire.instrument_celery()
app = Celery('tasks', broker='redis://localhost:6379/0')
@app.task
def add(x: int, y: int):
return x + y Celery emits signals at points in the
application lifecycle. worker_process_init fires once inside each worker process, after Celery
forks it from the parent. Initializing in worker_init (which runs in the parent, before the fork)
would build the exporter's background thread in the wrong process, so spans wouldn't export.
Set a service name so you can find the worker in Logfire. With distributed_tracing=True, the
worker reads incoming trace context without a warning. Trace context is the information that
connects the task to the trace that started it. Use this setting when you expect that information
from senders you trust. See incoming trace context.
Keep the worker running in its own terminal. Start it from the directory that contains tasks.py.
celery -A tasks worker --loglevel=info
Enable Celery tracking in the sender process too. Here the sender is a script. In a web app, the sender is the server that adds the task to the queue, such as FastAPI or Django. The integration in the sender puts the trace context in the task message. The integration in the worker reads it.
Save submit_task.py beside tasks.py. The Submit addition span starts a trace before the script
sends the task. If your web app already records requests in Logfire, the task can join the request’s
trace.
from tasks import add
import logfire
logfire.configure(service_name='sender')
logfire.instrument_celery()
with logfire.span('Submit addition'):
add.delay(42, 50)
Open a second terminal in the same directory. Run the sender there.
python submit_task.py
The worker terminal shows that tasks.add succeeded with the result 92. Open the
Live view and expand Submit addition. You should see these three
spans in one trace.
Submit addition [sender]
└── apply_async/tasks.add [sender]
└── run/tasks.add [worker]
Select run/tasks.add to see the task duration, status, and arguments.
Not seeing your tasks? Check that logfire.configure() ran before instrument_celery(), that your
write token is set (install uv
if needed, then run uvx logfire-cli auth if needed, then
uvx logfire-cli init use from your application’s directory, or set the LOGFIRE_TOKEN
environment variable in production; see Getting Started), and that you called
instrument_celery() exactly once per worker process.
If run/tasks.add appears in a separate trace, check that the sender calls
logfire.instrument_celery() before it sends the task. Check the worker configuration too.
distributed_tracing=False makes the worker start a new trace for the task.
If you schedule periodic tasks with Celery beat, instrument the beat process too, in its own
beat_init signal. Building on the example above:
from celery import Celery
from celery.signals import beat_init, worker_process_init
import logfire
@worker_process_init.connect(weak=False)
def init_worker(*args, **kwargs):
logfire.configure(service_name='worker')
logfire.instrument_celery()
@beat_init.connect(weak=False) # (1)
def init_beat(*args, **kwargs):
logfire.configure(service_name='beat') # (2)
logfire.instrument_celery()
app = Celery('tasks', broker='redis://localhost:6379/0')
app.conf.beat_schedule = { # (3)
'add-every-30-seconds': {
'task': 'tasks.add',
'schedule': 30.0,
'args': (16, 16),
},
}
@app.task
def add(x: int, y: int):
return x + y The beat_init signal fires once, as the beat process starts.
Use a different service_name so beat's spans are easy to tell apart.
Add a task to the beat schedule. See the beat schedule docs for the full format.
This schedules add to run every 30 seconds with the arguments 16 and 16. Run the beat process
with:
celery -A tasks beat --loglevel=info
The keyword arguments of logfire.instrument_celery() are passed
straight to the CeleryInstrumentor().instrument()
method. See the OpenTelemetry Celery instrumentation docs for the full option
list.
- API reference:
logfire.instrument_celery() - Underlying OpenTelemetry package: Celery instrumentation