Distributed Tracing in Spring Boot using OpenTelemetry + Jaeger (2025 Guide)

 

Distributed Tracing in Spring Boot using OpenTelemetry + Jaeger (2025 Guide)

This guide helps you understand:

✅ What is distributed tracing
✅ How to enable OpenTelemetry in Spring Boot
✅ How Jaeger receives spans
✅ What is the difference between OTEL vs Prometheus
✅ Complete ready-to-run example with Docker Compose
✅ Architecture diagrams
✅ README for your project


1. What is Distributed Tracing?

Modern microservices often involve multiple services communicating with each other:

Client → API Gateway → Auth Service → Order Service → Inventory Service

Debugging such systems with only logs is extremely difficult.

Distributed Tracing solves this by:

✔ Tracking a request end-to-end
✔ Showing how long each service took
✔ Identifying bottlenecks
✔ Detecting where a failure occurs

OpenTelemetry (OTEL) is the standard for collecting such traces.


2. What is OpenTelemetry (OTEL)?

OpenTelemetry is an open-source CNCF standard for:

  • Traces

  • Metrics

  • Logs

In your Spring Boot app, OTEL:

  1. Creates spans

  2. Stores them briefly in memory

  3. Exports them through OTLP protocol

  4. Sends to a backend like Jaeger, Tempo, Zipkin, etc.

✔ OTEL uses a push model

This means:

Your application sends data OUT to Jaeger, like this:

Spring Boot (OTEL SDK) ↓ (push) Jaeger Collector

3. What is Jaeger?

Jaeger is an open-source distributed tracing system.

In local mode (all-in-one):

  • Collector

  • Query

  • UI

  • Storage

are all in one container.

Jaeger stores data:

  • In memory (in local mode)

  • Elastic / Cassandra / ClickHouse / etc. (in production)


4. Architecture Diagram (Spring Boot → OTEL → Jaeger)

Client | v Spring Boot App | | creates spans v OTel SDK | | batches spans (memory buffer) v OTLP Exporter | | pushes spans v Jaeger Collector (4317/4318) | v Jaeger Storage (Memory) | v Jaeger UI (16686)

5. How Does OTEL Export Data? (Important)

👉 OTEL does not store long-term data
👉 OTEL only batches spans in memory for milliseconds
👉 OTEL pushes spans to Jaeger via OTLP

There is no polling.

✔ Jaeger never calls your app
✔ Your app always pushes the data


6. What is Prometheus?

Prometheus is used for:

  • Metrics (CPU, memory, request_count, latency, etc.)

  • Alerting

  • Time-series storage

Prometheus follows a pull model:

Prometheus scrapes /actuator/prometheus endpoint

✔ Prometheus calls your Spring Boot endpoint

✔ Your app does NOT push data

✔ Prometheus stores metrics on disk


7. OTEL vs Prometheus (VERY IMPORTANT)

FeatureOpenTelemetryPrometheus
PurposeTracing (request flow)Metrics (performance)
Data TypeSpans, traces, logsTime-series metrics
Collection ModelPush (App → Jaeger)Pull (Prometheus → App)
How It WorksOTEL sends trace data instantlyPrometheus periodically scrapes
What It ShowsEnd-to-end request timingCPU, memory, errors, latency
StorageJaeger (memory/DB)Prometheus TSDB
UIJaeger UIGrafana
Ideal UseMicroservice debuggingMonitoring & alerting

✔ Both used together in production

You use:

OTEL + Jaeger for tracing
Prometheus + Grafana for metrics


8. Spring Boot Example (Tracing /hello endpoint)

Your controller:

@GetMapping("/hello") public String hello() { Span span = tracer.spanBuilder("custom-span").startSpan(); span.addEvent("processing-hello"); try { Thread.sleep(50); } catch(Exception ignored) {} span.end(); return "Hello with tracing!"; }

Every request generates a trace visible in Jaeger.


9. Docker Compose — Spring Boot + Jaeger

version: "3.8" services: spring-app: build: . image: springboot-tracing-demo:latest container_name: springboot-tracing environment: - OTEL_SERVICE_NAME=springboot-tracing-demo - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318 - OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf - OTEL_TRACES_EXPORTER=otlp ports: - "8080:8080" depends_on: - jaeger jaeger: image: jaegertracing/all-in-one:1.57 container_name: jaeger ports: - "16686:16686" - "4317:4317" - "4318:4318"

10. Source code

https://github.com/kkvinodkumaran/springboot-tracing-demo-complete

No comments:

Post a Comment

Distributed Tracing in Spring Boot using OpenTelemetry + Jaeger (2025 Guide)

  Distributed Tracing in Spring Boot using OpenTelemetry + Jaeger (2025 Guide) This guide helps you understand: ✅ What is distributed trac...

Featured Posts