Prompt
I’m building tracing for a system with REST services and async jobs
Latest observation
A solid tracing setup for REST services plus async jobs usually comes down to one rule:
carry the same trace context everywhere, and create new spans for each meaningful unit of work.
Here’s a practical blueprint.
1) Use a standard context format
Adopt W3C Trace Context if possible:
traceparenttracestateoptional
This works well across HTTP, queues, workers, and background jobs.
2) Model the flow
For a request that hits a REST service and enqueues async work:
- Incoming HTTP request → create a server span
- Any downstream HTTP calls → create client spans
- When publishing a job/message → create a producer span
- When a worker consumes the job/message → create a consumer span
- If the worker does more internal work → child spans under that consumer span
This gives you one trace spanning sync + async steps.
3) Propagate trace context through jobs
When you enqueue a job, include trace context in the job payload or message headers, for example:
traceparenttracestate- optionally baggage items if you use them
Then on the worker side:
- extract the context
- make the consumer span a child of that context
If the job may be retried later, preserve the original trace context so retries stay in the same trace.
4) Use the right span kinds
Typical span kinds:
- SERVER for REST handlers
- CLIENT for outbound REST calls
- PRODUCER for enqueueing async jobs/messages
- CONSUMER for processing async jobs/messages
- INTERNAL for local operations
5) Include important attributes
Good baseline attributes:
service.namedeployment.environment- HTTP:
http.methodhttp.routehttp.status_code
- Messaging/job:
- queue/topic name
- job type
- message id
- attempt/retry count
- Error info:
- exception type/message/stack
- span status = error
6) Keep span boundaries meaningful
Don’t create spans for every tiny function. Good span candidates:
- request handler
- database query
- external API call
- enqueue job
- process job
- important business steps
7) Be careful with async context propagation
Common pitfalls:
- losing context when handing off to a thread pool or event loop
- accidentally starting a new trace for retries
- forgetting to propagate headers into job payloads
- mixing logs and traces without correlation IDs
8) Correlate logs with traces
Add trace IDs to logs:
trace_idspan_id
That makes debugging much easier when a job fails asynchronously.
9) Handle fan-out and fan-in
If one request enqueues many jobs:
- each job should usually be a child of the original trace
- if a job triggers many subtasks, use child spans
- if you aggregate results later, you can continue the same trace or create a new trace depending on how long the workflow is
For very long-lived workflows, some teams prefer separate traces linked by identifiers, but the simplest starting point is to keep one trace per user/workflow request.
10) OpenTelemetry is the usual choice
If you’re starting fresh, OpenTelemetry is the safest default:
- standard instrumentation for HTTP, queues, DBs
- propagators for trace context
- exporters to Jaeger, Tempo, Honeycomb, Datadog, New Relic, etc.
Recommended architecture
REST service
- receive request
- extract trace context from headers
- create server span
- do work
- when enqueueing async job, inject trace context into job metadata
- finish span
Worker
- receive job
- extract trace context from job metadata
- create consumer span
- process job
- emit child spans as needed
- finish span
If you want, I can also give you:
- a language-specific implementation example (Java, Go, Python, Node, .NET), or
- a diagram and payload format for propagating trace context through your jobs.