Video in progress
The trace ID is passed on HTTP calls, but the trace breaks after a Kafka message or an `@Async` method. Why?
The trace context is bound to the thread and passes automatically only through instrumented places. A new thread (@Async, your own ExecutorService) or a Kafka producer/consumer without observation enabled doesn't carry the context over, so a new trace begins.
📖 Answer
Micrometer Tracing keeps the current span in a thread-local. The RestClient.Builder and WebClient.Builder configured by Spring Boot write it into an HTTP header (traceparent). But:
1. @Async and a hand-made executor. The work runs on another thread, where the thread-local is empty. In Spring 6.1+ ContextPropagatingTaskDecorator carries the context over:
@Bean
ThreadPoolTaskExecutor asyncExecutor() {
var ex = new ThreadPoolTaskExecutor();
ex.setTaskDecorator(new ContextPropagatingTaskDecorator());
return ex;
}
If you use Executors.newFixedThreadPool(...) directly, you have to wrap the context yourself with ContextSnapshot.
2. Kafka. In Spring Kafka observation is off by default. Until you turn it on, no trace is added to the header and the consumer starts a new trace:
spring.kafka.template.observation-enabled=true
spring.kafka.listener.observation-enabled=true
3. Hand-made clients. RestClient.create() or new RestTemplate() doesn't pick up Boot's observation setup; use the builder Boot provides.
It's easy to check: send one request, then open the trace in Zipkin/Tempo. If the Kafka consumer span doesn't show up inside that trace, the context got lost along the way.
Sign up to mark studied questions and save your progress.