Quick revision
The short answer
Add Micrometer Tracing with a bridge, for example micrometer-tracing-bridge-otel with an OTLP exporter, so Spring Boot provides an Observation-based tracer, and switch on observations for Kafka with spring.kafka.template.observation-enabled=true in A and spring.kafka.listener.observation-enabled=true in B. The template then writes the W3C traceparent into the record headers, and the listener reads it and starts a consumer span whose parent is the producer span, so B's call to C stays in the same trace.
What to remember
In this answer, we saw that Kafka tracing in Spring Boot needs a tracing bridge and exporter plus the two observation properties, one for the template in A and one for the listener in B. The template injects the W3C traceparent into record headers, the listener extracts it and starts a consumer span under the producer span, and instrumented HTTP clients carry it on to C. Custom templates, hand-built producers, thread hops and batch listeners are the places where the chain breaks and need explicit handling.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Add Micrometer Tracing with a bridge, for example micrometer-tracing-bridge-otel with an OTLP exporter, so Spring Boot provides an Observation-based tracer, and switch on observations for Kafka with spring.kafka.template.observation-enabled=true in A and spring.kafka.listener.observation-enabled=true in B. The template then writes the W3C traceparent into the record headers, and the listener reads it and starts a consumer span whose parent is the producer span, so B's call to C stays in the same trace.
Why this question is asked
HTTP tracing works out of the box in Spring Boot, so teams are surprised when a trace stops the moment an event goes through Kafka. The reason is that Kafka observation is off by default, and the context has to travel inside the record. Interviewers ask this because it shows whether we understand how trace context propagates rather than just which dependency to add. In this answer, we will recap trace context, add the dependencies and properties, see how the context travels through headers, look at the cases that break it, and finish with batch listeners.
1A Quick Recap of Trace Context
A trace is the record of one request across services. It is made of spans, each a timed piece of work with a parent. For a child span in another process to join the trace, the caller must send the trace id and its own span id along with the request. Over HTTP this travels in the traceparent header defined by the W3C Trace Context standard, which looks like 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01: version, trace id, parent span id and flags.
Kafka has no request headers of its own, but records have headers, so the same traceparent can ride on the record. Something must write it when producing and read it when consuming, and in Spring that something is the Micrometer Observation support in KafkaTemplate and the listener container.
2The Configuration in A and B
Both services need Actuator, a tracing bridge and an exporter. With the OpenTelemetry bridge, Spring Boot auto-configures an Observation-based tracer and a W3C propagator.
Notice the sampling probability: Spring Boot samples 10 percent of traces by default, which is often why a test request seems to vanish. Starter names and property keys have shifted a little between Spring Boot 3.5 and 4.0, so check your version. In B, the call to C needs no extra work as long as it uses a RestClient or WebClient built from the auto-configured builder, because those are already instrumented.
3How the Context Travels
When A's controller handles the request, the HTTP server observation opens a span. Inside it, kafkaTemplate.send() starts a producer observation, which creates a child span and asks the propagator to inject the context into the record. The record leaves A with a traceparent header containing the trace id and the producer span's id.
In B, the listener container sees the record, extracts traceparent, and starts a consumer span whose parent is the producer span. Our @KafkaListener method runs with that span current, so the RestClient call to C creates a client span under it, and C's server span continues the same trace.
7 messages in order. Play them, or step through them one at a time.
Every span shares one trace id; the Kafka record is just another carrier for traceparent.
4Where the Chain Breaks
The observation lives in Spring's KafkaTemplate and in containers made by the auto-configured listener container factory. If a team builds its own KafkaTemplate or factory bean, the property no longer applies and they must call setObservationEnabled(true) themselves. A plain KafkaProducer created by hand bypasses Spring entirely and writes no header.
The other common break is a thread hop. If the listener hands work to its own executor, the current span does not follow automatically. Wrapping the executor with Micrometer's context propagation, for example ContextExecutorService.wrap(...), carries it across.
5Batch Listeners
A batch listener receives, say, 500 records at once, and each may come from a different trace. There is no single parent for the method call, so per-record parent-child spans do not fit. Teams usually either create one batch span and attach each record's context as a span link, which tracing backends show as related traces, or loop over the records and open a span per record with the extracted context. Spring Kafka's built-in observation targets record listeners, so check what your version supports for batches.
6Follow-up questions
Why is the consumer span a child and not a separate trace?
Because the listener extracts the producer's span id from traceparent and uses it as the parent. Some teams prefer span links for messaging, since a consumer may run hours later; both are valid, but parent-child gives the continuous trace the question asks for.
Does this work if B is written in Go and uses OpenTelemetry?
Yes, if both sides use W3C traceparent. The header is a standard, so any OpenTelemetry Kafka instrumentation can extract what Spring wrote. Problems appear only when one side uses a different propagation format, such as the older B3 headers.
How do I check that the header is really there?
Read a record with the console consumer and print headers (kafka-console-consumer with --property print.headers=true), or log record.headers() in B. If traceparent is missing, the producer side is the problem.
7Conclusion
In this answer, we saw that Kafka tracing in Spring Boot needs a tracing bridge and exporter plus the two observation properties, one for the template in A and one for the listener in B. The template injects the W3C traceparent into record headers, the listener extracts it and starts a consumer span under the producer span, and instrumented HTTP clients carry it on to C. Custom templates, hand-built producers, thread hops and batch listeners are the places where the chain breaks and need explicit handling.
Open with
Spring Boot traces HTTP automatically, but Kafka observation is off by default, which is why the trace stops at A. In both services I add Actuator, micrometer-tracing-bridge-otel and the OTLP exporter, so Boot provides an Observation-based tracer with the W3C propagator, and while testing I set the sampling probability to 1.0, because the default samples only ten percent.
Then, if they want more
Then in A I set spring.kafka.template.observation-enabled to true and in B spring.kafka.listener.observation-enabled to true. With that, KafkaTemplate creates a producer span inside the HTTP span and injects the traceparent header, with the trace id and the producer span id, into the record.
B's listener container extracts it and starts a consumer span whose parent is the producer span, so my listener runs inside the trace, and the call to C through the auto-configured RestClient continues it.
The things I watch for are custom KafkaTemplate or factory beans, where I call setObservationEnabled myself, hand-built producers that bypass Spring, thread hops that need context propagation, and batch listeners, where there is no single parent, so I either link each record's context to a batch span or trace records one by one.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Spring Boot 3.5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show the Spring Boot configuration needed for the trace to continue through Kafka, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/818e7efc37/