Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 72

    Distributed Tracing

    When one checkout hop crosses gateway → Orders → inventory → pricing, logs alone cannot show where time went.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    When one checkout hop crosses gateway → Orders → inventory → pricing, logs alone cannot show where time went. Distributed tracing stitches spans with a shared trace id so you see the critical path.

    Spring Boot 3 uses Micrometer Tracing with bridges such as OpenTelemetry (micrometer-tracing-bridge-otel) and OTLP export to Tempo, Jaeger, or a vendor backend. Sampling controls cost in production.

    Propagate context over HTTP and messaging. Put trace ids in MDC so logs deep-link to traces — completing the observability story started in monitoring.

    This lesson makes TechLearningPRO Orders debuggable as a distributed system.

    Purpose of this lesson

    Enable Micrometer Tracing + OpenTelemetry for Orders and propagate trace context across service calls.

    Understanding the topic

    Trace = tree of spans across process boundaries.

    • Trace id: identifies the whole request journey.
    • Span: one unit of work (HTTP server, client, DB).
    • Propagation: W3C Trace Context headers on outbound calls.
    • Micrometer Tracing: Boot 3 facade over OTel/Brave bridges.
    • Sampling: 1.0 in lab; lower in prod to control volume.

    Step-by-step explanation

    1. Add micrometer-tracing-bridge-otel and an OTLP exporter dependency.
    2. Configure sampling and OTLP endpoint toward Tempo/Jaeger.
    3. Call inventory from Orders with instrumented WebClient/RestClient.
    4. Confirm one trace shows gateway → Orders → inventory spans; log lines share the trace id.

    Informative example

    Boot 3 Micrometer Tracing with OTLP.

    yaml
    // Dependencies (Maven excerpt)
    // micrometer-tracing-bridge-otel
    // opentelemetry-exporter-otlp
    # application.yml
    management:
    tracing:
    sampling:
    probability: 1.0 # use ~0.05–0.1 in production
    otlp:
    tracing:
    endpoint: http://tempo:4318/v1/traces
    logging:
    pattern:
    level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
    # Outbound calls must use instrumented clients so headers propagate
    bean WebClient.Builder → WebClient.create() // prefer builder from Boot

    Real-world use

    TechLearningPRO's p99 checkout spike looked like 'Orders is slow'. A single trace showed 80% of time in inventory DNS timeouts after a bad kube service rename. Without tracing, teams blamed Postgres for half a day. With tracing, DNS was fixed in twenty minutes.

    Security implications

    Traces can leak sensitive data.

    • Do not put tokens, passwords, or card data in span attributes.
    • Restrict who can query the tracing backend.
    • Keep exporter endpoints on private networks.

    Best practices

    • Always correlate logs via MDC trace ids.
    • Trace the gateway and each Boot service with the same propagator standard.
    • Alert from metrics; use traces to explain the alert.

    Common mistakes

    • Sampling 100% in huge prod clusters without a budget — bill shock.
    • Manual HTTP clients that drop propagation headers.
    • High-cardinality span attributes (raw PII or unbounded ids) leaking into backends.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat question does tracing answer?+

    Answer

    Where time and errors occurred across services for a single request.
    2IntermediateQuestionWhat does Boot 3 use for tracing?+

    Answer

    Micrometer Tracing, commonly bridged to OpenTelemetry and exported via OTLP.
    3IntermediateQuestionWhy is sampling important?+

    Answer

    Full traces for every request can be expensive; sampling keeps signal while controlling cost.

    Hands-on exercise

    Lab. Enable OTel bridge on Orders and a fake inventory service. Generate one checkout and open the trace in Jaeger/Tempo. Break propagation deliberately (raw JDK HttpClient) and observe a broken trace — then restore the instrumented client.

    Summary

    Distributed tracing completes Orders observability across microservices. With limits, metrics, tests, and resilience in place, you can operate a Boot 3 estate with eyes open.

    Ready to mark this lesson complete?Track your journey across the entire course.