Distributed Tracing with Spring Cloud Sleuth and Zipkin - AQAP Series
Tags: spring, cloud, sleuth, zipkin
Published: 2020-07-26 14:11:39

There's a lot going on related to cloud services, libraries, providers etc. In fact, everything cloud-related gets a lot of attention right now.

One thing I wanted to try was distribute tracing. Spring has a very interesting project under the [Spring Cloud umbrella](https://spring.io/projects/spring-cloud), called [Spring Cloud Sleuth](https://spring.io/projects/spring-cloud-sleuth), that auto-configures your app to work with distributed tracing tools.

Together with this project you have Spring Cloud Zipkin Client that takes your instrumented log information and sends it to a Zipkin server. The server provides a clean and easy to use interface to understand the relationships between your services.

# The Demo

## Description

To explore the projects I developed 3 "layers" of services that talk to each other. The first layer contain only one service (service-one) and requests data from the second layer, comprised of two services (service-two, service-three).

At this point we can talk about Spring Cloud Load Balancer. This project is used to configure client-side load-balancing on the service-one for the second layer of services. The picture below shows how the services relate to each other.

![Service dependency view](https://dev-to-uploads.s3.amazonaws.com/i/5xnwc1qg4woi79wpxrvg.png)

Any service of the second layer then calls the service from the third layer (service-four). The list of services the request went through is put together and returned to the client by service-one. 

The two possible responses to a call to `service-one/info` are:

```json
//Using service-two on the second layer

{
  "info": {
    "Service 4": "Wow, request arrived this far!",
    "Service 2": "ok",
    "Service 1": "ok"
  }
}
```

```json
//Using service-three on the second layer

{
  "info": {
    "Service 4": "Wow, request arrived this far!",
    "Service 3": "ok",
    "Service 1": "ok"
  }
}
```
Service-one will unevenly load-balance between service-two and service-three because there's a `Thread.sleep(200)` on service-three.

![Uneven balancing](https://dev-to-uploads.s3.amazonaws.com/i/14at6y5qpnt446ildrtd.png)

## Running

To make it easy to run your own tests and see the distributed tracing in action, I've prepared a repository with the four services described above and a `.jar` for the Zipkin server.

{% github brunodrugowick/distributed-tracing-demo no-readme %}

Clone it and just run `/scripts/start-all.sh`. If you have Java 11 and Apache Bench (and a GNU/Linux operating system), everything runs and perform 10 thousand requests to fill your Zipkin server with some data.

## The trace

After waiting for a couple of minutes for the tests to run, the script instructs you to go to `http://localhost:9411`. Let's explore it a bit...

### Zipkin server

After searching for traces by clicking on the search icon, that's what the homepage looks like:

![Zipkin homepage](https://dev-to-uploads.s3.amazonaws.com/i/mec8314g16bbpqabjckb.png)

A very useful feature is the ability to filter traces based on the service name:

![Filter by service name](https://dev-to-uploads.s3.amazonaws.com/i/koifoffbxrgv1bi1dvxl.png)

Inspecting a trace gives you this view, where you can easily see how long the request took in every service:

![Inspecting one trace](https://dev-to-uploads.s3.amazonaws.com/i/62qy4wu44lmlrpxiqq4o.png)

Finally, a very useful feature is understand your services dependencies using the Dependencies Page. Make sure you select a Start and End time and click the magnifier glass icon:

![Dependencies page](https://dev-to-uploads.s3.amazonaws.com/i/kfdv77n00bd4zcsaqsdb.png)

# AQAP Series

As Quickly As Possible (AQAP) is a series of quick posts on something I find interesting. I encourage (and take part on) the discussions on the comments to further explore the technology, library or code quickly explained here.

---

Image by <a href="https://pixabay.com/pt/users/jaykingsta14-4885997/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2358636">Jason King</a> por <a href="https://pixabay.com/pt/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2358636">Pixabay</a>