Distributed Tracing With Spring Cloud Jaeger

In this article, we will explore how to implement distributed tracing using Jaeger and visualize the traces using Jaeger UI.


Jaeger is an open-source distributed tracing mechanism that helps to trace requests in distributed systems. It is based on opentracing specification and is a part of the Cloud Native Computing Foundation (CNCF).

I have explained some key concepts of tracing in my previous article “Distributed Tracing With Spring Cloud Sleuth”.

With this, let’s look at some code.

Implementing Jaeger Tracing

Let’s create an application from with only a single dependency “Spring Web”.

Once you generate and download the code, we will add the following Jaeger dependency to the pom file which will help to generate and propagate the traces between the services.


With this, let’s add a controller with some paths.

public class Controller {  
    private static final Logger _logger_ = LoggerFactory._getLogger_(Controller.class);  
    private RestTemplate restTemplate;  
    private String applicationName;  
    public Controller(RestTemplate restTemplate) {  
        this.restTemplate = restTemplate;  
    public ResponseEntity path1() {  
 "Incoming request at {} for request /path1 ", applicationName);  
        String response = restTemplate.getForObject("http://localhost:8090/service/path2", String.class);  
        return ResponseEntity._ok_("response from /path1 + " + response);  
    public ResponseEntity path2() { "Incoming request at {} at /path2", applicationName);  
        return ResponseEntity._ok_("response from /path2 ");  

Here, we have two endpoints /path1 and /path2 . The idea here is to use two instances of the same application such that /path1 calls /path2 of another service at a fixed port 8090.

For the spans to get connected to the same trace id, We need to create a RestTemplate bean to allow Jaeger to include an interceptor. This then helps to add traces to the outgoing request which will help to trace the entire request.

   public RestTemplate restTemplate(RestTemplateBuilder builder) {  

With this done, Let’s start a Jaeger Server locally using docker. For this, I have created a docker-compose file with the port mappings.

version: "3.3"  
    image: jaegertracing/all-in-one:1.25  
      - 6831:6831/udp  
      - 6832:6832/udp  
      - 16686:16686  
      - 14268:14268

We can communicate with Jaeger using either via UDP or TCP. After starting the docker image using docker-compose up , we can access the UI using the URL http://localhost:16686/

Now, let’s add some properties to allow the application to send the traces to the Jaeger server. We will communicate via TCP, so make sure that we send the traces to the other TCP port. i.e 14268

      url: http://localhost:14268/api/traces

Let’s start “Server 1” with the below command.

java -jar \  
target/Distributed-Service-0.0.1-SNAPSHOT.jar \ \  

Then on a different terminal, run a new instance of the same application as “Service 2” as follows

java -jar \  
target/Distributed-Service-0.0.1-SNAPSHOT.jar \ \  

Once the application starts, call “Service 1” at /path1 as follows

curl -i http://localhost:8080/service/path1

Let’s look at the logs of “Service 1”.

INFO 69938 --- [nio-8080-exec-1] i.j.internal.reporters.LoggingReporter   : Span reported: ed70bbaa2bd5b42f:c7c94163fc95fc1e:ed70bbaa2bd5b42f:1 - GET

The tracing is of the format [Root Span Id, Current Span Id, Parent Span Id]. In this case, since “Service 1” is the originating service, the parent span Id “ed70bbaa2bd5b42f” is also the root span id.

Now, let’s look at the logs of “Service 2”.

INFO 69885 --- [nio-8090-exec-1] i.j.internal.reporters.LoggingReporter   : Span reported: ed70bbaa2bd5b42f:e9060cb1d5336c55:c7c94163fc95fc1e:1 - path2

Here we see that the middle value is the current span id and the parent span id (ie. the third value “c7c94163fc95fc1e”) is the span id of “Service 1”.

Now, If you open the UI you will see the following.

When we dig deeper, we see more details on each of the spans.

Here, the root span id “ed70bbaa2bd5b42f” spans across the entire request. The other two span ids refer to the individual services.


Today we explored how we can integrate Jaeger which is based on OpenTracing with a spring boot application. You can always read more in-depth about the specification of OpenTracing here. Also, the library documentation for using spring cloud Jaeger is here.

I have uploaded the code on GitHub.

You can read about Distributed tracing using Zipkin my previous article here.

I keep exploring and learning new things. If you want to know the latest trends and improve your software development skills, then subscribe to my newsletter below and also follow me on Twitter.