Tracing and correlation ids

Microservices · lesson 12 of 33 · 4 min read

Follow a single user request across every service by propagating one id into logs and spans.

Open this lesson in the learning hub

Key points

  • One click can touch six services. Without a shared id you have six log streams and no way to line them up.
  • A trace id identifies the whole request; a span id identifies one hop. They travel in the W3C traceparent header.
  • Spring Boot 3 uses Micrometer Tracing (Sleuth is retired). Add the bridge and RestClient, WebClient and Kafka are instrumented for you.
  • Boot puts traceId and spanId in the MDC and in the default log pattern, so every log line is already correlated.
  • Sample in production. Tracing 100% of requests is expensive; 1-10% plus "always keep errors" is a normal setting.
  • Export via OTLP to Tempo, Jaeger or Zipkin, then jump from a slow span straight to the logs of that exact hop.

Example

# Dependencies: micrometer-tracing-bridge-otel + opentelemetry-exporter-otlp
spring:
  application:
    name: orders                  # shows up on every span and log line

management:
  tracing:
    sampling:
      probability: 0.1            # 10% in prod, 1.0 while developing
  otlp:
    tracing:
      endpoint: http://tempo:4318/v1/traces

# Resulting log line - the ids are added by Boot's default pattern:
# INFO [orders,3f9a1c7d2b,ab12cd34] c.j.OrderController : placing order o-1042

One trace id per request turns "somewhere it is slow" into "this call, this service".

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Microservices course, and every lesson in it is listed on the Microservices contents page.