Distributed Tracing with Spring Cloud Sleuth And Zipkin

Photo by Agence Olloweb on Unsplash

Introduction

What is Distributed Tracing?

Key concepts of Distributed Tracing.

  • Trace Id
  • Span Id

Spring Boot Example With Spring Cloud Sleuth

public class Controller {

private static final Logger logger = LoggerFactory.getLogger(Controller.class);
private RestTemplate restTemplate;

@Value("${spring.application.name}")
private String applicationName;

public Controller(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@GetMapping("/path1")
public ResponseEntity path1() {

logger.info("Request at {} for request /path1 ", applicationName);
String response = restTemplate.getForObject("http://localhost:8090/service/path2", String.class); return ResponseEntity.ok("response from /path1 + "+ response);
}

@GetMapping("/path2")
public ResponseEntity path2(){
logger.info("Request at {} at /path2", applicationName); return ResponseEntity.ok("response from /path2 ");
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
java -jar \
target/Distributed-Service-0.0.1-SNAPSHOT.jar \
--spring.application.name=Service-1 \
--server.port=8080
java -jar \
target/Distributed-Service-0.0.1-SNAPSHOT.jar \
--spring.application.name=Service-2 \
--server.port=8090
curl -i http://localhost:8080/service/path1
INFO [Service-1,222f3b00a283c75c,222f3b00a283c75c] 41114 --- [nio-8080-exec-1] c.a.p.distributedservice.Controller      : Incoming request at Service-1 for request /path1
INFO [Service-2,222f3b00a283c75c,13194db963293a22] 41052 --- [nio-8090-exec-1] c.a.p.distributedservice.Controller      : Incoming request at Service-2 at /path2
x-b3-traceid:"222f3b00a283c75c", 
x-b3-spanid:"13194db963293a22",
x-b3-parentspanid:"222f3b00a283c75c

Visualizing Traces with Zipkin

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
version: "3.1"
services:
zipkin:
image: openzipkin/zipkin:2
ports:
- "9411:9411"
spring:
zipkin:
baseUrl: http://localhost:9411
  • The first point refers to when the client from “Service 1” started the request
  • The second point is when “Service 2” started processing the request.
  • The third point is when the client on “Server 1” finished receiving the response.
  • And finally, the last point when “Server 2” finished.

Conclusion

--

--

--

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Meet Our Team: Sergey Lesninsky, Infomatix CTO

The Serverless Pharmacy

Install Minikube

Ready, Set, Hack! — Junction Tokyo 2018

System Design: Web-Crawler

Notes from APUE(2) — File and I/O

Python Tuple In Operation And Method

WMB Continuous Integration

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amrut Prabhu

Amrut Prabhu

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles

More from Medium

Circuit Breaker And Retry with Spring Cloud Resiliance4j

Keycloak Configuration for Java Application Authentication

Spring Boot Cache with Redis

REDIS TRANSACTION IN JAVA

Redis transaction commands