Spring Boot: From Basics to Production · Observability with Actuator and Micrometer

Distributed Tracing with Micrometer Tracing and OpenTelemetry

Follow requests across services and correlate logs.

Add Micrometer Tracing with the OpenTelemetry bridge and an OTLP exporter to create spans for incoming requests, RestClient calls, JDBC (with datasource-micrometer) and messaging, propagated via W3C traceparent headers. Trace and span IDs are added to the logging MDC, so logs correlate with traces. Use sampling in production.

@Service
class FulfillmentService {
    private final ObservationRegistry observations;
    FulfillmentService(ObservationRegistry observations) { this.observations = observations; }

    void ship(Order order) {
        Observation.createNotStarted("shop.fulfillment.ship", observations)
                .lowCardinalityKeyValue("carrier", order.carrier())
                .observe(() -> carrierClient.createShipment(order));   // timer + span
    }
}
management:
  tracing:
    sampling:
      probability: 0.1
  otlp:
    tracing:
      endpoint: http://otel-collector:4318/v1/traces

Common pitfalls

  • Creating HTTP clients with new instead of the auto-configured builder (no instrumentation).
  • 100% sampling in high-traffic production.
  • High-cardinality values as low-cardinality keys.

Key points

  • Micrometer Tracing + OTel bridge
  • Use auto-configured builders for instrumentation
  • Trace IDs in logs

Further reading: Spring Boot: Tracing

Practice questions

Why should you build RestClient from the injected RestClient.Builder?

Answer

The auto-configured builder includes observation instrumentation, so outbound calls create spans and propagate trace headers.

How are logs correlated with traces in Spring Boot?

Answer

Micrometer Tracing puts traceId and spanId into the MDC, and the default log pattern (or structured logging) includes them.

Open in the app to mark lessons complete, take notes and track your progress.