Distributed tracing helps you follow a request as it flows through multiple microservices. It’s useful for:
- Debugging latency issues
- Understanding service dependencies
- Observing bottlenecks in request paths
Traces are usually composed of spans:
- Trace ID: identifies the full request flow.
- Span ID: identifies a single operation within the trace.
- Parent Span ID: connects spans into a tree.
2. Configure Zipkin
In application.properties
(or application.yml
):
spring.application.name=tracing-demo
spring.sleuth.sampler.probability=1.0
spring.zipkin.base-url=http://localhost:9411/
spring.zipkin.enabled=true
spring.sleuth.sampler.probability=1.0
→ send all traces.spring.zipkin.base-url
→ Zipkin server URL.
3. Example Controller
package com.example.tracingdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot with Zipkin!";
}
}
4. Run Zipkin
The easiest way is via Docker:
docker run -d -p 9411:9411 openzipkin/zipkin
- Access the UI at http://localhost:9411
5. Integrate Jaeger (Optional)
Spring Cloud Sleuth supports Jaeger too.
Add dependency in build.gradle
:
implementation 'io.opentracing.contrib:opentracing-spring-jaeger-cloud-starter:3.2.0'
Configure Jaeger in application.properties
:
spring.application.name=tracing-demo
spring.sleuth.sampler.probability=1.0
# Jaeger settings
JAEGER_SERVICE_NAME=tracing-demo
JAEGER_AGENT_HOST=localhost
JAEGER_AGENT_PORT=6831
Run Jaeger using Docker:
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 9411:9411 \
jaegertracing/all-in-one:1.47
- Access Jaeger UI: http://localhost:16686
6. Test Tracing
- Run your Spring Boot app.
- Hit endpoint:
curl http://localhost:8080/hello
- Check traces in Zipkin or Jaeger UI.
You should see a trace for the /hello
request.
✅ Key Points:
- Spring Cloud Sleuth automatically instruments your app and sends traces.
- Zipkin is lightweight and easy to run.
- Jaeger is more full-featured and integrates well with Kubernetes.