Spring Cloud Sleuth and Zipkin

In this tutorial you will learn how to use Spring Cloud Sleuth together with Zipkin to be able to trace HTTP requests across your Microservices. Using Spring Cloud Sleuth and Zipkin you will be able to aggregate in one place the information about HTTP requests that your Microservices sent and the time it took to HTTP Request to complete. This will help you identify a slow responding Microservices, so you can work on it to improve performance.

Spring Cloud Sleuth

Spring Cloud Sleuth will add a couple of additional details to a logging information when it is being logged. These are:

  • Span ID – The basic unit of work. For example, sending an HTTP Request will generate a new span id.  If there will be another Microservice called within the same flow, then new span id will be generated. All of these spans will be grouped under the same trace id.
  • Trace ID – Is used to group a number of spans. The initial HTTP Request will generate a new trace id. If within the same flow, a Microservice will send HTTP request to another Microservice, then Trace Id for both of these HTTP Requests will be the same. HTTP Requests sent by two different users will generate two different trace id.

Enable Spring Cloud Sleuth

Add Sleuth Dependency
To enable Spring Cloud Sleuth in your Spring Boot Microservice add the following dependency to POM.XML file.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

This single dependency will add support for Sleuth which can send information to Zipkin server.

Update application.properties file
Now when we have added spring-cloud-sleuth-zipkin dependency to pom.xml file we need to update application.properties file by adding new configuration properties:

spring.zipkin.service.name=zipkin
spring.zipkin.baseUrl=http://localhost:9411
spring.zipkin.sender.type=web
spring.sleuth.sampler.probability=1.0

where:

  • spring.zipkin.service.name – Is used to provide a name under which Zipkin server is known. (Optional)
  • spring.zipkin.baseUrl – Is used to provide domain and port number of which Zipkin server is running. The default Zipkin server port number is 9411.
  • spring.zipkin.sender.type – Is used to provide how the information to Zipkin will be sent. A value of web will specify that information will be sent over HTTP. Other possible values for example are: rabbit or kafka,
  • spring.sleuth.sampler.probability – Is used to specify how much of information needs to be sent to Zipkin. By default Spring Cloud Sleuth sets all spans to non-exportable. That means that traces appear in logs but not in any remote store. A Value of 1.0 would mean 100% of all times. And a value of 0.1 would mean only 10%. If you want to make sure that all spans are sent to Zipkin, provide a value of 1.0 here.

Note: If you get the following error when your spring boot application starts with Spring Cloud Sleuth enabled:

Error creating bean with name 'rabbitListenerContainerFactory'

then add the two following properties to application.properties file.

spring.rabbitmq.listener.direct.retry.enabled=true
spring.rabbitmq.listener.simple.retry.enabled=true

Sleuth Logging

After you have enabled Sleuth in your Spring Boot application by following the steps above. You can log something to see how Trace and Span Ids are added to your logging information. To enable logging you can use LoggerFactory for example:

Logger logger = LoggerFactory.getLogger(this.getClass());

and then log something before sending HTTP request and after sending HTTP request. For example:

logger.info("Before sending GET Albums HTTP Request...");
List<AlbumResponseModel> albumsList = albumsServiceClient.getAlbums(userId) 
logger.info("Albums web service endpoint was called and recieved " + albumsList.size() + " items");

When you run your application you should see a similar information added to your logging.

[users-ws,88430cb09b5e45ff,48cc20a35c22f1ab,true]

where:

  • users-ws – Name of current Microservice,
  • 88430cb09b5e45ff – Trace Id,
  • 48cc20a35c22f1ab – Span id,
  • true – If the information is sent to Zipkin.

But first we need to start up Zipkin server.

Starting up Zipkin Server

To download Zipkin server run the following command in your terminal window.

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

Run Zipkin with:

java -jar zipkin.jar

Open Zipkin UI 

When you run Zipkin with the above java -jar command, by default, it will start on port 9411. To see Zipkin Dashboard, open the following URL in your browser window:

http://localhost:9411/zipkin/

And you should see something like this:

I hope this tutorial was helpful to you.

To learn more about building Spring Boot Microservices and Spring cloud have a look at my other Spring Cloud tutorials and a list of video courses below.