Video in progress
In a distributed system, how do you follow a single request across all services' logs (trace id)?
In Boot 3, Micrometer Tracing gives every incoming request a trace and span id, or continues an incoming traceparent header, puts them into MDC and propagates them to outgoing HTTP/Kafka calls. If you add the trace id to the log pattern, one id shows you the whole path.
📖 Answer
Dependencies: micrometer-tracing-bridge-otel and, for export, opentelemetry-exporter-otlp (or Zipkin).
management:
tracing:
sampling:
probability: 0.1 # 10% of traces are kept in production
otlp:
tracing:
endpoint: http://otel-collector:4318/v1/traces
logging:
pattern:
level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
Incoming HTTP (Spring MVC/WebFlux observation) and outgoing calls through the RestClient.Builder, WebClient.Builder and KafkaTemplate that Boot configures work automatically (for Kafka, if observation is turned on).
Places where the trace id gets lost:
new RestTemplate()or your ownRestClient.create(): inject the builder instead;@Async,CompletableFuture, your own thread pools: useContextPropagatingTaskDecoratororContextSnapshotso the context carries over;- forgetting to turn on
observation-enabled: truefor Kafka.
Even with 10% sampling the trace id is in every log line, so when a complaint comes in you can still find it in the logs. To always keep requests with errors, use tail-based sampling in the collector.
Add an X-Trace-Id header to the response: your support team will thank you. The customer sends you that id and you find it right away.
Sign up to mark studied questions and save your progress.