ADR 0009: The W3C trace context crosses the outbox to Kafka
Status
Accepted (2026-10-13, ecosystem gap fix D27). Additive: no migration (the
existing outbox_events.headers jsonb column carries it), optional Kafka
headers only; payloads and the CloudEvents envelope
(ADR 0004) are unchanged.
Point 5 (in-memory outbox without DATABASE_URL) is moot since
ADR 0010: no binary runs
on the memory adapters any more.
Context
Every write of this context runs inside an HTTP request span (otelchi on the
api router) and leaves through the transactional outbox: the use case inserts
the encoded events into outbox_events in its transaction, and the relay
(cmd/api, ADR 0007) later publishes
them from a background goroutine with no span of its own. The outbox stored
only the CloudEvents content-type header, so the trace stopped at the
database: a consumer of warehouse.product-master.events (inventory-storage,
order-management, wes-work-planning, fulfillment-execution) or of the
analytics topic could never link its work to the request that produced the
event.
The fleet decision (gap-fix wave 2026-10-12, D27) is trace continuity across
the outbox, using the storage the fleet's reference implementation
(order-management) already uses: the trace context lives in the outbox row's
headers jsonb column, injected with otel.GetTextMapPropagator().Inject at
publish time, and the relay sets the stored headers as Kafka message headers.
Decision
- Capture at insert.
postgres.OutboxRepo.Insert(the publish point: it receives the use case's context, which carries the request span) injects the trace context with the global propagator thattelemetry.Setupinstalls (W3C trace-context + baggage) into a map carrier and appendstraceparent, andtracestate/baggagewhen present, to each row's headers, after the encoder'scontent-type. A key the encoder already set is never overwritten. Without an active span the propagator injects nothing and the row keeps onlycontent-type. - No schema change.
outbox_events.headersalready exists (jsonb NOT NULL DEFAULT '[]', migration 0001) and the relay already copies every stored header onto the Kafka message (kafka.RelaySink.Send). Rows written before this change relay exactly as before. - Both topics. The integration and the analytics row of one event are
inserted by the same call with the same context, so both carry the same
traceparent. - Consumers. This service's consumers (the legacy classification
importer and the analytics projector) open no spans, so they do not
extract; when one starts a span it must call
Extracton the message headers first. The contract (apis/asyncapi.yaml) documents the headers as optional: consumers may use them and must not require them. - In-memory outbox.
memory.OutboxRepo(noDATABASE_URL, local and unit tests only) does not inject; it is never used with a real broker in the cluster.
Consequences
- A consumer that extracts the headers parents its span on the producing
request's span: one trace from
PUT /products/{sku}/classificationto a sibling's handling ofProductClassified. - Each outbox row grows by one ~55-byte header (more with
tracestateor baggage). - Baggage set by a caller is forwarded to Kafka consumers; this service sets none and the HTTP edge has no auth, so nothing sensitive is expected there.
- Tests (real Postgres and Kafka via testcontainers, in
internal/adapters/outbound/outbox/outbox_trace_integration_test.go):TestOutboxTrace_TraceparentCrossesTheOutboxToKafka(the request span'straceparentis stored in the row, sent by a relay pass that runs on a background context, and extracts to the request's trace and span) andTestOutboxTrace_RowWithoutTraceHeadersRelaysUnchanged(a row without stored headers relays with its key and value unchanged and no headers).