Trace Context Propagation for Carrier Webhooks

Step-by-step: propagate W3C trace context from a carrier booking call into its async webhook, so both appear as one trace in Jaeger or Tempo.

Share

A DPD booking call and the webhook that confirms label creation minutes or hours later are, as far as Jaeger or Tempo are concerned, two completely unrelated events. The POST to DPD's shipment service gets a trace ID, a span, maybe a nice flame graph. Then the webhook lands on your ingestion endpoint and starts from zero: new trace ID, no parent, no connection to the booking that caused it. You end up debugging two orphaned fragments instead of one coherent story. This tutorial fixes that specific problem: propagating trace context propagation across the REST-call-then-webhook boundary so both halves show up under one trace ID, using DPD's shipment API as the worked example.

Why carrier integrations produce broken traces

The short answer: the carrier isn't a participant in your tracing system, so it can't carry your context forward, and nothing rehydrates it on the other side unless you build that step yourself.

Normal distributed tracing works because every hop is a service you control or that at least respects your headers. traceparent follows the format {version}-{trace-id}-{parent-id}-{trace-flags}: a 2-character version, a 32-character hex trace ID identifying the entire trace, a 16-character hex parent ID identifying the specific span that made the call, and a 2-character trace-flags bitfield. When Service A calls Service B directly, B reads that header, generates its own span ID, and keeps the same trace ID. Tempo or Jaeger stitch the two spans together because they share that trace ID.

A carrier webhook breaks this chain at the exact point it matters most. DPD's infrastructure processes your booking, maybe routes the parcel through a depot, and pushes parcel tracking events automatically through a webhook endpoint whenever it suits them. That webhook request carries DPD's own payload fields, not your traceparent header. Your webhook receiver sees a fresh inbound POST with no trace context at all, so your OpenTelemetry SDK does the only sensible thing: starts a new trace.

What you need before starting

  • OpenTelemetry SDK instrumented in both the gateway service that calls DPD and the webhook receiver that ingests DPD's callbacks.
  • A trace backend reachable from both services — Jaeger or Grafana Tempo, either works for this pattern.
  • A durable key-value store (Redis or Postgres) to hold trace context against a business key, because the webhook has no HTTP chain back to the original request to lean on.
  • A DPD sandbox account with a webhook URL configured for parcel status events, as described in DPD's Tracking Push documentation.
  • Clarity on which headers you're actually working with: the spec defines two HTTP headers: traceparent, the required, interoperable core, and tracestate, an optional, vendor-extensible companion for additional state. This tutorial only needs traceparent.
FieldSizeExample valueWhat it identifies
version2 hex chars00Format version, currently always 00
trace-id32 hex chars4bf92f3577b34da6a3ce929d0e0e4736The whole trace, end to end
parent-id16 hex chars00f067aa0ba902b7The span ID of the caller
trace-flags2 hex chars01Sampling decision

The parent ID is an 8-byte identifier encoded as 16 lowercase hexadecimal characters, and each service generates a new parent ID for its own span while preserving the trace ID. That's the field you'll be reconstructing manually once DPD's webhook arrives.

Propagating context through the booking and the callback

  1. Generate the root span on booking. When the shipment request hits your gateway, let your OTel SDK start the root span as usual. Capture the resulting traceparent string, something like traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01.
  2. Call DPD's shipment service as normal. POST to the Shipment Service endpoint with your parcel and consignee data. Don't bother injecting the traceparent header into this request — DPD won't read it, store it, or echo it back. There's no point fighting that.
  3. Persist the traceparent against the returned parcel ID. DPD's response includes a 14-digit identifier: a Base64 string on the "parcellabelsPDF" key and a 14 digit parcel number in the "parcelLabelNumber" key. Write a row to Redis or Postgres: parcelLabelNumber → traceparent, with a TTL you'll revisit in the failure-mode section below. This is the same pattern used to carry context across async boundaries generally: generate the context as a W3C traceparent, then hand it off as metadata rather than relying on transport headers to survive the gap.
  4. Configure the webhook endpoint to extract the parcel ID as the lookup key. DPD's tracking push payload carries the parcel identifier on every event — in the Belgian implementation this arrives as parcelNumber in the callback body, mapping to the same parcelLabelNumber value DPD returned at booking time. Pull that field out before anything else.
  5. Fetch the stored traceparent and rehydrate the context manually. Don't expect propagation.extract() to find anything in the inbound HTTP headers — there's nothing there. Instead, read the stored string from your database and feed it through the same extraction code path you'd use for headers, just pointed at a different carrier object:
// webhook receiver, pseudocode
const stored = await db.get(`trace:${parcelLabelNumber}`);
const carrier = { traceparent: stored.traceparent };
const parentContext = propagator.extract(context.active(), carrier);
  1. Start the webhook-processing span as a child of that rehydrated context. Use context.with(parentContext, () => { ... }) (or your language's equivalent) so the new span's parent-id points at the original booking span. The span will land under the same trace ID as the "create-shipment" span, even though it's running in a completely different process, hours later.
  2. Verify in Jaeger or Tempo. Search by the trace ID you captured in step 1. You should see one trace containing both the "create-shipment" span from your gateway and the "webhook-received" span from your receiver, separated by whatever wall-clock gap DPD actually took to process the parcel.

How you know it worked

Open the trace in Jaeger or Tempo and look for exactly one trace ID spanning two services — your gateway and your webhook receiver — with a realistic time gap between the two spans rather than two unconnected single-span traces that happen to reference the same shipment. If you're still seeing two traces, check that the lookup key used at write time (step 3) matches the key extracted at read time (step 4) byte for byte; a mismatched format, say zero-padding dropped on one side, is the most common reason rehydration silently fails.

Cross-check against your structured logs. Both the booking log line and the webhook log line should carry an identical trace_id field. That's a cheap sanity check you can run before you even open the UI, and it doubles as a correlation ID you can grep on long after the trace itself has aged out of retention.

Failure mode: context expires or gets sampled out before the webhook arrives

Some carriers, consolidators especially, can delay exception-case webhooks by a day or more. If your default sampling decision already dropped the original trace, or your Redis TTL expired before DPD's webhook fired, rehydration at the database layer succeeds but produces an orphaned span with no parent in the UI. Set a TTL on the stored traceparent that matches your longest realistic carrier SLA, not your default cache expiry, and tag any trace that crosses a carrier boundary with an always-sample flag so the sampler doesn't throw it away before the webhook catches up.

A related non-problem worth naming: duplicate webhook deliveries that re-use the same traceparent will create multiple child spans under one parent. That's expected, and it's useful signal, not noise. Don't try to de-duplicate at the tracing layer. De-duplication belongs at the idempotency-key layer, as covered in detail in Encore's write-up on carrier tracking webhook backends, which walks through verifying signatures, deduplicating events, ignoring out-of-order updates, normalising carrier statuses and polling as a fallback. Tracing and idempotency solve different problems; keep them separate.

Extending the pattern beyond a single webhook

If webhook ingestion fans out through SQS or RabbitMQ before processing, carry the rehydrated traceparent as a message attribute rather than embedding it in the JSON body. That keeps it out of your schema and lets consumers extract it the same way regardless of payload shape, matching how most async job systems already carry context as message headers or properties rather than inline fields.

On a multi-tenant platform, namespace the lookup key by tenant plus shipment ID — tenant:1842:parcel:05222835034925 rather than just the parcel number — so one shipper's trace never bleeds into another's. And this is worth saying plainly: whichever connectivity layer sits in front of DPD, GLS or PostNL, whether that's a self-built adapter or a multi-carrier platform such as Cargoson, nShift, EasyPost or ShipEngine, none of them currently forward trace headers from the carrier side. The rehydration step described here is still your responsibility regardless of which layer you're running on top of.

Start with one carrier and one shipment type. Once you can see a single trace ID span the gap between a DPD booking and its webhook, the same database-backed rehydration pattern extends to every other carrier integration on your platform without any new tooling.