Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

When we build a microservice architecture and the number of services keeps growing, we face the problem of debugging and tracing requests through our entire system. What happened when a user got a 500 error on his request? What service incorrectly processed his request? All these questions can be solved by the Distributed Tracing System. Let's take Spring Cloud Sleuth as an example.

How Spring Cloud Sleuth Works

To trace a request through a distributed system, the concepts TraceID and SpanID are introduced. TraceID is generated when a request enters our system and remains unchanged throughout its path. SpanID changes as the request passes from one service to another. If necessary, it is possible to generate new spans within one service to distinguish business processes.

Distributed Tracing With Spring-cloud-Sleuth Kafka and Zipkin

Distributed Tracing introduction

As we are all living in the microservices world now, it is necessary and important to have a nice and easy way to trace the invocation from one service to another directly or via a message broker like Kafka.

So in this tutorial, you will see how to use Spring Cloud Sleuth to record distributed tracing between Spring Boot microservices and Kafka. Zipkin will be used as a tool to collect and visualize distributed tracing across services.

Distributed Tracing With Spring Cloud Sleuth and Zipkin

In the case of a single giant application that does everything, which we usually refer to as a monolith, tracing the incoming request within the application is easy. We can follow the logs and then figure out how the request is being handled. There is nothing else we have to look at but the application logs themselves. 

Over time, monoliths have become difficult to scale, to serve a large number of requests as well as delivering new features to the customer with the growing size of the codebase. This leads to breaking down the monolith into microservice, which helps in scaling individual components and also helps to deliver faster.