Class KafkaTraceHeaderCarrier
@Logged call chain's FlowContext across a Kafka
message, by writing it into the message's headers when producing and
reading it back when consuming.
The traceId and depth are written as plain Kafka
headers (Headers), never into the message's own key or value.
Kafka headers exist specifically for metadata like this: they travel with
the message but are entirely separate from whatever serialization format
(JSON, Avro, protobuf, or anything else) the message's actual payload
uses, so this never touches or constrains that payload's schema.
This class depends only on org.apache.kafka:kafka-clients'
Headers interface, not spring-kafka: every Kafka producer
and consumer, whether used directly or through Spring's own
KafkaTemplate/@KafkaListener, exposes this same interface,
so this works regardless of which of those a consuming application uses.
Like every propagation class in this library, nothing here is wired in
automatically. A consuming application calls writeToHeaders(org.apache.kafka.common.header.Headers) when
producing a message and readAndAdopt(org.apache.kafka.common.header.Headers, java.lang.Runnable) when consuming one, exactly
where it already produces or consumes Kafka messages.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidreadAndAdopt(org.apache.kafka.common.header.Headers headers, Runnable work) Reads aFlowContextfromheaders, if present, adopts it as the active context on this thread for the duration ofwork, and restores this thread's previous context again afterward, regardless of whetherworkcompletes normally or throws.static voidwriteToHeaders(org.apache.kafka.common.header.Headers headers) Writes the current thread'sFlowContext, if any is active, intoheadersasTRACE_ID_HEADER/DEPTH_HEADERentries.
-
Method Details
-
writeToHeaders
public static void writeToHeaders(org.apache.kafka.common.header.Headers headers) Writes the current thread'sFlowContext, if any is active, intoheadersasTRACE_ID_HEADER/DEPTH_HEADERentries.Does nothing if no
@Loggedcall is currently active on this thread: a message produced from outside any call chain simply carries no trace headers, exactly as if this method had never been called.- Parameters:
headers- the headers of theProducerRecordabout to be sent
-
readAndAdopt
Reads aFlowContextfromheaders, if present, adopts it as the active context on this thread for the duration ofwork, and restores this thread's previous context again afterward, regardless of whetherworkcompletes normally or throws.If
headerscarries noTRACE_ID_HEADER— for example, a message produced by a service that does not use this library, or one produced from outside any call chain —workis simply run as-is, with no context adopted. Any@Loggedcall made from within it then starts a new chain of its own, exactly as it would without this class involved at all.- Parameters:
headers- the headers of theConsumerRecordbeing processedwork- the message-handling code to run with the message's trace context active
-