Prompt
How do I trace an event-driven workflow across multiple serverless functions?
Latest observation
To trace an event-driven workflow across multiple serverless functions, you want distributed tracing with a shared correlation context that follows the event from one function to the next.
Core idea
Each function should:
- Read the incoming trace context
- Create a child span / continue the trace
- Propagate the context into the next event/message
- Log the trace ID everywhere
That lets you reconstruct the full path across queue triggers, pub/sub, HTTP callbacks, etc.
Recommended approach
1) Use a standard tracing format
Prefer OpenTelemetry and W3C Trace Context:
traceparenttracestate(optional)
These are widely supported and portable across cloud providers and runtimes.
2) Propagate trace context in every event
When Function A emits an event for Function B, include trace metadata in the message envelope or headers.
Examples:
- HTTP: put
traceparentin headers - Queue/pub-sub: include trace headers in message attributes/metadata
- Custom event payloads: include a metadata section
Example event structure:
{
"metadata": {
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00"
},
"data": {
"orderId": "12345"
}
}
3) Instrument every function
In each function:
- Start a span when the function is invoked
- Extract incoming trace context
- Attach logs/metrics to the span
- End the span before return
If you use OpenTelemetry SDKs, this is mostly automatic once configured.
4) Add correlation IDs to logs
Alongside tracing, include:
trace_idspan_idrequest_idorcorrelation_id
This makes it easy to search logs when tracing isn’t enough.
Example log:
{
"message": "Created payment intent",
"trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
"span_id": "00f067aa0ba902b7",
"function": "payment-handler"
}
5) Trace asynchronous hops explicitly
Serverless workflows often cross async boundaries, so you must manually propagate context:
- queue message
- event bus payload
- workflow state
- scheduled callback
If the platform strips headers, store tracing metadata in message attributes or payload fields.
Practical architecture
A typical chain might look like:
-
API Gateway → Function A
- Incoming HTTP request has
traceparent - Function A starts span
A
- Incoming HTTP request has
-
Function A → Queue
- Function A publishes message with same trace context
- Creates child span
publish-to-queue
-
Queue → Function B
- Function B extracts context from message
- Starts span
Bas child of A’s trace
-
Function B → Function C
- Continue propagating trace context
In a tracing UI, you’ll see one trace with spans for A, queue publish, B, C, etc.
Tooling options
Managed services
- AWS X-Ray
- Azure Application Insights
- Google Cloud Trace
Portable standard
- OpenTelemetry + Jaeger / Tempo / Zipkin / Datadog / New Relic
OpenTelemetry is usually the best long-term choice if you want cloud portability.
Common pitfalls
- Not propagating context across async events
- Starting a new trace in each function
- Using ad hoc IDs that aren’t compatible with tracing tools
- Only instrumenting HTTP functions, not queue consumers
- Sampling too aggressively, which can hide the full workflow
Best practice checklist
- Instrument all functions with OpenTelemetry
- Propagate
traceparentthrough events/messages - Log
trace_idandspan_id - Use one trace per business transaction
- Ensure your queue/event broker preserves metadata
- Add span names that reflect business steps
- Export traces to a central backend
If you want, I can show you:
- an AWS Lambda + SQS example,
- an Azure Functions example, or
- a Node.js/Python OpenTelemetry setup.