0041. The W3C trace context crosses the outbox in outbox_events.headers (D27 reference)
Status
Accepted — recorded by the change that introduces this record (fleet gap-fix wave 2026-10-12, decision D27). This repo is the reference the other contexts copy in wave 7.
Builds on ADR 0022 (the outbox) and ADR 0009 (telemetry setup and the W3C propagator).
Context
Fleet decision D27 asks that a trace started by a request continue through
the transactional outbox: the outbox stores the W3C traceparent and
tracestate of the transaction that wrote the row (the addendum names an
additive nullable column trace_headers jsonb), the publisher fills it from
ctx through the global propagator, the relay sets those values as Kafka
message headers, rows without a value relay unchanged, and every consumer
that starts a span extracts the context from the headers.
Verifying that against this repo showed the mechanism already exists, under a different column name:
migrations/0007_outbox.up.sqlcreatedoutbox_events.headers JSONB NOT NULL DEFAULT '[]', documented as "the W3C trace-context headers captured at Encode time".- Both encoders build the headers from
ctx:kafka.Publisher.Encode(internal/adapters/outbound/kafka/publisher.go,otel.GetTextMapPropagator().Injectnext to the CloudEventscontent-typeheader) andkafka.AnalyticsPublisher.Encode(analytics_publisher.go). postgres.OutboxPublisher.Publishstores them in that column inside the use case's transaction (outbox_publisher.go,marshalHeaders), andpostgres.OutboxRelayreads them back (unmarshalHeaders) and hands them tokafka.RelaySink.Send, which writes them as the Kafka message headers.- Every inbound consumer that starts a span extracts first:
re-promise, planned-capacity, product-classification and the analytics
consumer (
otel.GetTextMapPropagator().Extractininternal/adapters/inbound/kafka). The three replay caches start no span and need no extraction.
What was missing was proof: the only trace test covered the direct publish
path (TestPublisher_InjectsTraceContextIntoHeaders), not the outbox path
that every Postgres-backed deployment takes.
Decision
- The trace store is
outbox_events.headers. Notrace_headerscolumn is added here. The column already holds exactly the headers the message will carry (content-type,traceparent,tracestatewhen present), and a second column would hold a second copy of the sametraceparentwith no rule for which one wins. - Capture point. The encoder injects from the
ctxof the use case (the HTTP server span from otelchi, or the consumer span of the message that triggered the use case), inside the same transaction that writes the aggregate. The relay never injects: it has no span, and the stored context is the only correct parent. - Rows without trace context relay unchanged. A row written with no
active span stores only
content-type; a raw row with[]relays with no headers at all. Neither carries an all-zerotraceparent. - Consumers. A consumer that starts a span calls
otel.GetTextMapPropagator().Extracton the message headers first. A consumer that starts no span (the replay caches) is left alone. - The reference tests (testcontainers Postgres and Kafka, in
internal/adapters/outbound/postgres/outbox_trace_integration_test.go):TestOutboxTrace_TraceparentCrossesTheOutboxToKafka: a use case runs in a request span; the outbox row stores that span'straceparent; a relay pass on a context WITHOUT the span sends it to a real broker; extracting the consumed message's headers yields the request's trace id and span id as a remote parent.TestOutboxTrace_RowWithoutTraceHeadersRelaysUnchanged: a row with no stored headers reaches Kafka with the same key and value and no headers.
How wave 7 copies this
For each repo with an outbox:
- Check whether the outbox already stores the propagator output (a
headerscolumn filled by an encoder that callsInjecton the use case'sctx). If it does, add this ADR's two tests, adapted, and record the existing column the same way. If it does not, add the additive nullabletrace_headers jsonbcolumn the addendum names, fill it fromctxin the outbox publisher, merge it into the message headers in the relay, and leave rows withNULLunchanged. - Check each inbound consumer that starts a span for an
ExtractbeforeStart. - Add the ADR (next free number), the index and sidebar entries, and the observability and testing page lines.
Consequences
- A trace in Tempo/Jaeger runs from the HTTP request (or the consumed message) through the downstream consumer's span, even though the Kafka write happens later in the relay goroutine. There is still no publish span on the outbox path.
- The relay keeps a row's original parent even when it sends the row much
later (after a broker outage or an
OUTBOX_MAX_ATTEMPTSre-arm, ADR 0040): the consumer span then starts long after its parent ended, which is expected for asynchronous messaging. - No migration, no new configuration, no behaviour change in this repo: the change is the record and the two tests that keep the behaviour from regressing.
Alternatives considered
- Add
trace_headers jsonbanyway, as the addendum words it. Rejected for this repo: it would duplicate thetraceparentalready inheadersand force a precedence rule. The addendum's goal (continuity, nullable safety, unchanged legacy rows) is met by the existing column, and the tests prove it. - Start a publish span in the relay and inject it. Rejected: the relay span would become the consumer's parent and cut the link to the request that caused the event.