Prompt
Need to trace async jobs and HTTP requests together
Latest observation
Oct 28, 2026
OpenAI APIWeb search: off
To trace async jobs and HTTP requests together, you want one shared trace context that is started on the incoming HTTP request and then propagated into background jobs.
Core idea
- HTTP request starts trace/span
- When you enqueue an async job, capture the trace context and attach it to the job payload/headers
- When the worker picks up the job, resume the trace and create a child span
- Emit both to the same observability backend so they appear as one distributed trace
What to propagate
At minimum:
- trace_id
- span_id / parent_span_id
- trace flags / sampling decision
- Optionally baggage/metadata if needed
If you’re using standard distributed tracing, use:
- W3C Trace Context:
traceparent/tracestate - Optional:
baggage
Recommended setup
1) Instrument HTTP server
Create a root span for each inbound request:
- Request middleware starts span
- Add request attributes: route, method, status, user/session (careful with PII)
2) Propagate context into the job queue
When producing a job:
- Serialize current trace context into job metadata
- Examples:
- Queue message attributes
- Job headers
- Payload fields like
traceparent
3) Resume context in worker
When consuming the job:
- Extract trace context from job metadata
- Start a worker span as a child of the HTTP request span
- Add job-specific attributes: queue name, attempt count, duration, error status
4) Link retries and fan-out
For:
- Retries: keep the same trace or link spans to the original job span
- Fan-out: create child spans/jobs with the same parent or use span links if supported
- Scheduled jobs: preserve context at enqueue time, not execution time
Example flow
- User hits
POST /checkout - Server creates span
HTTP POST /checkout - App enqueues job
send_receipt - Job includes
traceparent - Worker receives job and creates span
send_receipt - Backend shows:
- request span
- enqueue span
- worker span
- downstream DB/API spans
If you use OpenTelemetry
OpenTelemetry is the easiest path:
- Use OTel SDK in web app and workers
- Use propagators for context injection/extraction
- Ensure your queue library supports custom metadata
- Export to Jaeger, Tempo, Datadog, Honeycomb, etc.
Pseudo-example:
# producer
carrier = {}
inject(carrier) # adds trace context
job = {"payload": data, "trace": carrier}
# consumer
ctx = extract(job["trace"])
with tracer.start_as_current_span("process_job", context=ctx):
do_work()
Common pitfalls
- Starting a new trace in the worker instead of continuing the original one
- Losing context because the queue payload doesn’t preserve metadata
- Forgetting to instrument retries and downstream calls
- Sampling too aggressively so request/job spans don’t both exist
- Using async frameworks without context propagation support
Best practice
Use:
- OpenTelemetry
- W3C trace context
- A queue that can carry metadata
- A consistent span naming scheme
If you want, I can also give you:
- a language-specific example (Python/Node/Java/Go), or
- a design for a specific queue like Celery, SQS, Kafka, RabbitMQ, Sidekiq, or BullMQ.