Problem
When the API publishes a message to NATS and the Worker picks it up, the "trace" normally breaks. The Worker starts a new trace, making it impossible to see that the Worker's action was caused by a specific API call. This is known as a "Context Gap."
Proposed Solution
Use OpenTelemetry's TextMapPropagator to inject and extract trace headers.
- Producer (API): Before publishing to NATS, "inject" the current span context into the NATS message headers.
- Consumer (Worker): Upon receiving a message, "extract" the context from the headers and use it as the parent for the Worker's new span.
Alternatives Considered
Passing IDs in JSON payload: Rejected as it litters the business logic with telemetry data. Using NATS headers keeps the payload clean.
Additional Context
None
Problem
When the API publishes a message to NATS and the Worker picks it up, the "trace" normally breaks. The Worker starts a new trace, making it impossible to see that the Worker's action was caused by a specific API call. This is known as a "Context Gap."
Proposed Solution
Use OpenTelemetry's TextMapPropagator to inject and extract trace headers.
Alternatives Considered
Passing IDs in JSON payload: Rejected as it litters the business logic with telemetry data. Using NATS headers keeps the payload clean.
Additional Context
None