Class KafkaTraceHeaderCarrier

java.lang.Object
com.fayupable.logged.spring.kafka.KafkaTraceHeaderCarrier

public final class KafkaTraceHeaderCarrier extends Object
Carries a @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 Type
    Method
    Description
    static void
    readAndAdopt(org.apache.kafka.common.header.Headers headers, Runnable work)
    Reads a FlowContext from headers, if present, adopts it as the active context on this thread for the duration of work, and restores this thread's previous context again afterward, regardless of whether work completes normally or throws.
    static void
    writeToHeaders(org.apache.kafka.common.header.Headers headers)
    Writes the current thread's FlowContext, if any is active, into headers as TRACE_ID_HEADER/DEPTH_HEADER entries.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • writeToHeaders

      public static void writeToHeaders(org.apache.kafka.common.header.Headers headers)
      Writes the current thread's FlowContext, if any is active, into headers as TRACE_ID_HEADER/DEPTH_HEADER entries.

      Does nothing if no @Logged call 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 the ProducerRecord about to be sent
    • readAndAdopt

      public static void readAndAdopt(org.apache.kafka.common.header.Headers headers, Runnable work)
      Reads a FlowContext from headers, if present, adopts it as the active context on this thread for the duration of work, and restores this thread's previous context again afterward, regardless of whether work completes normally or throws.

      If headers carries no TRACE_ID_HEADER — for example, a message produced by a service that does not use this library, or one produced from outside any call chain — work is simply run as-is, with no context adopted. Any @Logged call 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 the ConsumerRecord being processed
      work - the message-handling code to run with the message's trace context active