Application Tracing Using Spring Cloud Sleuth and Zipkin

One of the problems developers encounter as their microservice apps grow is tracing requests that propagate from one microservice to the next

Spring Cloud Sleuth is help with this exact problem. It introduces unique IDs to your logging which are consistent between microservice calls which makes it possible to find how a single request travels from one microservice to the next.

Spring Cloud Sleuth adds two types of IDs to your logging, one called a trace ID and the other called a span ID. The span ID represents a basic unit of work, for example sending an HTTP request. The trace ID contains a set of span IDs, forming a tree-like structure. The trace ID will remain the same as one microservice calls the next. span ID difference from one microservice to next.

Spring Cloud Sleuth will send tracing information(all the microservice log) to Zipkin server.  Using Zipkin UI  visualize the how many traced requests went through each application. you can filter or sort all traces based on the application, length of trace, annotation, or timestamp. Once you select a trace, you can see the percentage of the total trace time each span takes which allows you to identify the problem application.

Setting up the Zipkin Server in Spring Boot

  • add these dependencies to pom.xml
-----
<dependency>
 <groupId>io.zipkin.java</groupId>
 <artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
 <groupId>io.zipkin.java</groupId>
 <artifactId>zipkin-autoconfigure-ui</artifactId>
 <scope>runtime</scope>
</dependency>
--------
  • annotate the monitoring application with @EnableZuulServer (e.g. in ZipkinServerApplication.java)
  • Configuring log bean in the All micro service client application
#Add dependency
<dependency>    
<groupId>org.springframework.cloud</groupId>    
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
--------------------
#log format
@Bean
public AlwaysSampler defaultSampler() {   
 return new AlwaysSampler();
}

On pointing your browser to http://localhost:9411, you will see the all the http request trace information.

 

Leave a comment