Video in progress
How do you set up distributed tracing in a Spring Boot 3 application, and how does the traceId show up in logs?
Add micrometer-tracing-bridge-otel (or Brave) and an exporter, and set the sampling percentage. After that HTTP and Kafka calls are automatically wrapped in spans, and the traceId and spanId go into the MDC.
📖 Answer
Spring Cloud Sleuth from Spring Boot 2 is gone; in Boot 3 Micrometer Tracing took its place.
The dependencies:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
The configuration:
management:
tracing:
sampling:
probability: 0.1 # 10% of requests are recorded
otlp:
tracing:
endpoint: http://otel-collector:4318/v1/traces
After that, in the default log pattern of Spring Boot 3.2+, a correlation part in the form [traceId-spanId] appears after the application name. In Boot 3.0-3.1 you add %X{traceId} and %X{spanId} to logging.pattern.level yourself.
An important condition: instrumentation works only through builders that Spring creates. If you build with the static method RestClient.builder(), the trace header isn't passed on. Inject the auto-configured RestClient.Builder. For Kafka, observation is off by default: turn it on with spring.kafka.template.observation-enabled=true and spring.kafka.listener.observation-enabled=true.
It's easy to check: start two services, send a request to the first one and see that both logs have the same traceId. If the second one has a different ID, the header got lost along the way.
Sign up to mark studied questions and save your progress.