OpenTracing using Spring Cloud Sleuth
By: Venkat Pandeti
Tags: Microservices, Opentracing, Distributed Tracing, Spring Boot, Sleuth

Introduction

In the world of microservices, a distributed transaction moves across multiple services. OpenTracing would be very helpful to reduce Mean Time To Detect (MTTD) an issue in a distributed transaction.

Trace: Description of a distributed transaction that moves across multiple services.

Span: Timed operation within a distributed transaction. A group of spans form a trace

Span Context: As the distributed transaction moves from one service to another service, the span context needs to be propagated as well. Span Context can be propagated using B3 propagation, a specification for the header “B3” and those that start with “X-B3-”. Below are the“X-B3-” headers propagated through the network call.

X-B3-TraceId

X-B3-ParentSpanId

X-B3-SpanId

X-B3-Sampled

Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data by Trace Id.

Run the below commands to run Zipkin server in local machine

curl -sSL https://zipkin.io/quickstart.sh | bash -s

java -jar zipkin.jar

Spring Cloud Sleuth provides Spring Boot auto-configuration for OpenTracing. Sleuth instruments common ingress and egress points (RestTemplate, Spring Kafka, etc.) with OpenTracing

Dependencies

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-sleuth-zipkin</artifactId>

</dependency> <dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-sleuth</artifactId>

</dependency>

RestTemplate must be a Spring Component, otherwise, http calls won’t be intercepted by tracing

I have created a sample service using Spring Boot Cloud Sleuth. Please follow the below steps to run it

1. Clone the repo

2. In the terminal, cd to the code base

3. Run mvn clean install

4. As shown in the below image, open 4 terminal windows (one for Zipkin server, two for MainService, three for UserService and four for BookService)

java -jar target/sampleservice-0.0.1-SNAPSHOT.jar --server.port=8080 --spring.application.name=MainService java -jar target/sampleservice-0.0.1-SNAPSHOT.jar --server.port=8081 --spring.application.name=UserService java -jar target/sampleservice-0.0.1-SNAPSHOT.jar --server.port=8082 --spring.application.name=BookService

5. Once Zipkin is started and MainService, UserService and BookServices are started, open the following url http://localhost:8080/users/4890-12-29379490/details/

6. Trace and Span data should be displayed as shown below

6. Trace and Span data should be displayed as shown below

7. Open Zipkin server using the url http://localhost:9411/ and enter the Trace Id in the search box (Search by trace ID)

8. It displays the distributed transaction involving the calls to UserService and BookService, made by MainService.

Zipkin Server showing the distributed transaction

Happy coding :)

https://github.com/vpandeti/opentracing

References:

https://opentracing.io/docs/overview/

https://zipkin.io/

https://spring.io/projects/spring-cloud-sleuth