Spring Boot 3 Observability in Production: Distributed Tracing with OpenTelemetry, Micrometer, Prometheus & Grafana
Why `System.out.println` and scattered log files fail across 15 microservices—and how to wire `traceId`, `spanId`, RED metrics, and Spring AI token telemetry.

- When a customer reports a 9-second checkout delay across 8 microservices, grepping unindexed text logs on 20 containers is impossible without Distributed Tracing (`traceId` + `spanId`).
- Spring Boot 3 replaced legacy Spring Cloud Sleuth with the unified **Micrometer Observation API** (`ObservationRegistry`) and **OpenTelemetry (OTel)** exporter—allowing you to write instrumentation once and emit both Prometheus metrics and Tempo/Jaeger traces automatically.
- Modern Java + Gen AI services also track LLM-specific observability: prompt/completion token counts (`gen_ai.usage.input_tokens`), vector retrieval latency, and model fallback rates.
1. The 3 Pillars of Microservice Observability: Metrics, Logs & Traces
In production engineering, Metrics tell you *that* something is broken (e.g., P99 latency spiked above 3s), Distributed Traces tell you *where* it broke (e.g., `InventoryService -> PostgreSQL query` took 2.8s of the 3s), and Structured Correlated Logs tell you *why* it broke (e.g., `DeadlockDetectedException` for `sku_id=9941`).
| Observability Pillar | Spring Boot 3 / Java 21 API | Collection & Storage Backend | What You Alert & Debug On |
|---|---|---|---|
| 1. Metrics (RED + JVM) | Micrometer + `/actuator/prometheus` | Prometheus + Grafana Dashboards | Request Rate, Error % (5xx), P95/P99 Latency Histograms, HikariCP pool exhaustion, JVM GC pauses |
| 2. Distributed Traces | `micrometer-tracing-bridge-otel` + W3C `traceparent` HTTP/Kafka headers | OpenTelemetry Collector → Grafana Tempo / Jaeger / Zipkin | End-to-end waterfall timeline across API Gateway → Spring Boot → Kafka → Postgres → Spring AI |
| 3. Structured JSON Logs | SLF4J + Logback JSON encoder with automatic MDC `[traceId, spanId]` injection | Grafana Loki / ELK (Elasticsearch, Logstash, Kibana) | Instant filtering of all log lines across 10 microservices for a single failing `traceId` |
2. How W3C `traceparent` Context Propagation Works Across HTTP & Kafka
When you use Spring Boot 3's auto-configured `RestClient.Builder`, `WebClient.Builder`, and `KafkaTemplate`, Micrometer Tracing automatically injects the W3C standard `traceparent` header into every outbound call:
[React Client] ──► [API Gateway: Generates traceId = 4bf92f3577b34da6...]
├─► [OrderService (spanId=a1): 12ms] -> Logs JSON with { traceId: '4bf9...', spanId: 'a1' }
├─► [PaymentService (spanId=b2): 85ms] -> Calls PostgreSQL (spanId=b3: 18ms)
└─► [Spring AI Assistant (spanId=c4): 640ms]
├─► [pgvector Similarity Search (spanId=c5): 22ms]
└─► [LLM ChatModel Call (spanId=c6): 610ms | input_tokens=1420, output_tokens=185]3. Bonus Differentiator: Monitoring Spring AI & Vector Store Telemetry
Spring AI natively integrates with Micrometer's `ObservationRegistry`! Out of the box, every `ChatClient` and `VectorStore` call records high-value metrics including model name, temperature, input/output token consumption, and vector search latency—letting you build a real-time Grafana dashboard showing token cost per endpoint alongside standard JVM and HTTP metrics.
Frequently Asked Questions
Why did Spring Boot 3 replace Spring Cloud Sleuth with Micrometer Tracing?
Previously, developers had to write separate code for metrics (Micrometer) and tracing (Sleuth). Spring Boot 3 unified both into the Micrometer Observation API (`@Observed`), where a single observation automatically starts a timer metric AND creates an OpenTelemetry trace span.
Why must you instantiate `RestClient` via injected `RestClient.Builder` for tracing?
If you call `RestClient.create()` manually with `new`, Spring Boot cannot attach its Micrometer observation interceptors, breaking `traceId` propagation to downstream microservices. Always inject the Spring-managed `RestClient.Builder`.
Naitik Tailor & Uncorp Engineering Faculty
Founder & Principal Engineering Mentors (12+ Yrs Enterprise Experience) · VIT CSE '21 · Symbiosis MBA '24 · Ex-Delhivery & Ex-Aditya Birla Capital
Continue Reading in This Topic Cluster
All 73 Guides
Fault-Tolerant Microservices in Spring Boot 3: Resilience4j Circuit Breakers, Rate Limiters, Retry & Bulkhead Patterns
How a single slow downstream service or OpenAI API spike can crash your entire JVM cluster—and how to isolate failures using Resilience4j in Java 21.

Hibernate, Spring Data JPA & Redis Performance Tuning: Fixing the N+1 Query Problem, Connection Pools & Cache Stampedes
The ultimate database and caching optimization guide for Java Backend developers: eliminate N+1 queries, size HikariCP connection pools, and architect Redis Cache-Aside in Spring Boot 3.

How to Build Production MCP Servers & Tool-Calling AI Agents in Java 21 with Spring AI
A practical enterprise blueprint for Java backend developers: expose Spring Boot microservices, PostgreSQL queries, and internal APIs safely to LLMs using Spring AI and Model Context Protocol (MCP).

