Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

Traces show the HTTP request span but the Kafka message sent during the request starts a new trace in the consumer, and spans from @Async work are missing. How do you fix both?

Spring Kafka does not create observations by default, so no trace headers are added to the records and the consumer starts a new trace. Enabling spring.kafka.template.observation-enabled and spring.kafka.listener.observation-enabled makes the producer write traceparent into the record headers and the listener continue the trace. For @Async, the work runs on another thread where the trace context is not set, so we decorate the executor with a ContextPropagatingTaskDecorator. We also check that HTTP clients come from Boot's builders, and verify each fix with a real trace in a test environment.

Why this question is asked

Broken traces are one of the most common complaints after a team introduces tracing. The HTTP part looks perfect, but the moment work leaves the request thread, through a message broker or a thread pool, the trace either splits in two or disappears. During an incident that means we cannot follow an order from the API to the consumer that failed to process it. Interviewers like this question because it tests whether we understand how trace context actually moves: in headers between processes and in thread-local storage inside one. In this answer, we will recap how context is carried, reproduce both problems, fix Kafka, fix @Async, look at the other common gap with HTTP clients, and finish with how to verify the fixes.

1A Quick Recap: How Trace Context Moves

Inside one JVM, the current span is stored in thread-local state, a variable that each thread has its own copy of. Code running on the request thread can see it, which is how a log line or an outgoing call knows which trace it belongs to. Between processes, the context is written into the message itself: HTTP headers for a call, record headers for a Kafka message.

So the context can be lost in two ways. It is not written into the message when the request leaves the process, or it is not copied to the new thread when work moves to another thread. Our two symptoms are one of each.

Step throughWhere the context is lost
  1. HTTP request threadSpan active in thread-local state
  2. Sent to Kafka with observation on?
    Yestraceparent written to record headersNoNo headers: consumer starts a new trace
  3. @Async executor decorated?
    YesContext copied to the worker threadNoWorker thread has no span: work missing
  4. Trace completeOne trace id from API to consumer

4 steps in order. Play them, or step through them one at a time.

One gap is between processes, the other between threads.

2Fixing Kafka: Turn On Observation

Spring Kafka supports observations on both KafkaTemplate and the listener containers, but they are disabled by default. Without them, the producer sends the record with no trace headers, and the consumer has nothing to continue, so it begins a new trace. With Spring Boot's auto-configured Kafka beans we switch them on with two properties.

Java
# application.properties (producer and consumer services)
spring.kafka.template.observation-enabled=true
spring.kafka.listener.observation-enabled=true
// If the template or container factory is created by hand, set it there:
template.setObservationEnabled(true);
factory.getContainerProperties().setObservationEnabled(true);
// Record headers after the fix:
// traceparent = 00-4bf92f3577b34da6a3ce929d0e0e4736-a1b2c3d4e5f60718-01

Now the template creates a producer span and writes its context into the record headers. The listener container reads those headers, creates a consumer span whose parent is the producer span, and makes it current while our @KafkaListener method runs. Notice that both sides need the change: if only the producer is fixed, the headers exist but nobody reads them. A common trap is a service that defines its own KafkaTemplate bean, which Boot then does not configure, so the property has no effect; that is why the programmatic setters matter.

3Reproducing the @Async Problem

@Async tells Spring to run a method on a task executor, a pool of worker threads, and return immediately. The worker thread was created long before our request and has no idea which span is current on the request thread.

Java
@Async
public void sendConfirmation(Order order) {
log.info("sending confirmation"); // traceId= (empty)
emailClient.send(order); // no parent span, new trace or none
}
// Request thread: traceId=4bf92f35... spanId=00f067aa...
// Worker thread : traceId= spanId=

So the email call either appears as a separate trace or, if it is not sampled on its own, does not appear at all, which matches the missing spans in the question. The log line inside the method also loses its trace id, which hurts as much during an incident.

4Fixing @Async: Propagate the Context to the Executor

Spring Framework 6.1 added ContextPropagatingTaskDecorator. A task decorator wraps each submitted task. This one captures the context on the submitting thread, using the Micrometer context-propagation library, and restores it on the worker thread for the duration of the task, then cleans it up. Spring Boot applies a TaskDecorator bean to the executor it auto-configures, so for the default @Async executor one bean is enough.

Java
@Configuration
class AsyncTracingConfig {
@Bean
TaskDecorator contextPropagatingTaskDecorator() {
return new ContextPropagatingTaskDecorator(); // Boot applies it to its task executor
}
// A custom executor must be decorated explicitly
@Bean
ThreadPoolTaskExecutor reportExecutor() {
ThreadPoolTaskExecutor ex = new ThreadPoolTaskExecutor();
ex.setCorePoolSize(4);
ex.setTaskDecorator(new ContextPropagatingTaskDecorator());
return ex;
}
}
// After the fix, worker thread: traceId=4bf92f35... spanId=7d3e... (child of the request span)

The reason the decorator works is that it moves the context at the one moment when both threads are involved: submission. The same applies to CompletableFuture.supplyAsync with our own executor, scheduled tasks and any hand-made thread pool; each one needs a decorated executor or a context-aware wrapper. Boot only decorates executors it creates, so any executor we build ourselves is our responsibility.

5Checking the HTTP Clients Too

When one trace is broken, others often are too. Outgoing HTTP calls only carry traceparent when the client was built from Boot's RestClient.Builder, WebClient.Builder or RestTemplateBuilder, because those carry the observation registry. A client created with new RestTemplate() or RestClient.create() sends no header and creates no span, so the next service starts its own trace. A quick search for those constructors in the code base usually finds the remaining gaps.

6Verifying the Fixes

We verify with a real trace rather than by reading configuration. In a test environment with sampling at 1.0, we send a request with a known traceparent header, then search the tracing backend for that trace id and check that it contains the HTTP span, the Kafka producer span, the consumer span and the async email span as children. We repeat this after each fix, so we know which change closed which gap.

It is also worth an automated check. An integration test with Testcontainers Kafka and a test span exporter, or Micrometer's test tracer, can assert that the consumer's trace id equals the producer's. That turns a one-time fix into a regression test, so the next team that hand-builds a KafkaTemplate finds out in the build, not in an incident.

7Follow-up questions

Why is Kafka observation off by default?

It adds headers to every record and spans for every message, which has a cost on high-throughput topics and changes the record format consumers see. Spring Kafka leaves the choice to us. In most business services the benefit of connected traces is worth it, but for very high-volume pipelines we may enable it with a low sampling rate.

What about reactive code with WebClient and Reactor?

Reactor does not use one thread per request, so thread-local context does not follow the work by itself. With Micrometer's context-propagation library, Hooks.enableAutomaticContextPropagation() lets Reactor restore the context on each operator. Spring Boot can enable this for us in recent versions, so check the property for your version.

Does the consumer span have the producer span as parent, or a link?

Spring Kafka's observation makes the consumer span a child of the producer's context taken from the headers, so the trace continues as one tree. For batch listeners, where one consumer span covers many records from different traces, a single parent is not possible, and tools use span links instead; check how your version handles batch observation.

8Conclusion

In this answer, we fixed two broken traces. Kafka lost the trace because Spring Kafka's observation is off by default, so we enabled it on the template and listener containers, which adds traceparent to record headers and lets the consumer continue the trace. @Async lost it because work moved to a thread without the context, so we decorated the executor with ContextPropagatingTaskDecorator. We also checked that HTTP clients come from Boot's builders, and verified every fix with a real trace and an automated test.

What to say in the interviewAbout 80 seconds aloud

Open with

These are two different gaps. The Kafka one is between processes: Spring Kafka doesn't create observations by default, so KafkaTemplate sends records without a traceparent header and the consumer has nothing to continue, so it starts a new trace.

Then, if they want more

I'd set spring.kafka.template.observation-enabled and spring.kafka.listener.observation-enabled to true on both sides, or call setObservationEnabled if the template or container factory is built by hand, since then Boot's properties don't apply. After that, the producer writes the context into record headers and the listener creates a child span.

The @Async gap is between threads: the span is held in thread-local state on the request thread, and the executor's worker thread doesn't have it. I'd add a ContextPropagatingTaskDecorator bean, which Boot applies to its auto-configured executor, and set it explicitly on any executor we create ourselves. It captures the context at submission and restores it on the worker.

While I'm there, I'd check that HTTP clients are built from Boot's RestClient or WebClient builders, because new RestTemplate breaks traces too. Then I'd verify each fix in a test environment with sampling at one, sending a request and checking the whole tree under one trace id, and add an integration test so it doesn't regress.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
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.
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 "Traces show the HTTP request span but the Kafka message sent during the request starts a...": https://scrivio-interview-library.netlify.app/spring-boot/observability/d48a1def47/