Add Distributed Tracing to Spring Cloud Microservices with Sleuth

This guide shows how to enable Spring Cloud Sleuth in a Spring Boot microservice setup, configure Eureka discovery, add tracing dependencies, implement two simple REST services, set up application properties, run the services, and interpret the generated trace and span IDs from the console logs.

Programmer DD
Programmer DD
Programmer DD
Add Distributed Tracing to Spring Cloud Microservices with Sleuth

Overview

Distributed tracing is needed to follow a request that passes through multiple Spring Cloud microservices. Spring Cloud Sleuth automatically creates and propagates trace and span identifiers and adds them to log output.

Quick start example

The example consists of two Spring Boot services registered with Eureka: trace-1 (calls trace-2) and trace-2 (returns a static string).

Dependencies

Add the following Maven dependencies to each service:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

Application code

trace‑1

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class TraceApplication {

    private final Logger logger = Logger.getLogger(getClass());

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping(value = "/trace-1", method = RequestMethod.GET)
    public String trace() {
        logger.info("===call trace-1===");
        return restTemplate()
                .getForEntity("http://trace-2/trace-2", String.class)
                .getBody();
    }

    public static void main(String[] args) {
        SpringApplication.run(TraceApplication.class, args);
    }
}

trace‑2

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class TraceApplication {

    private final Logger logger = Logger.getLogger(getClass());

    @RequestMapping(value = "/trace-2", method = RequestMethod.GET)
    public String trace() {
        logger.info("===call trace-2===");
        return "Trace";
    }

    public static void main(String[] args) {
        SpringApplication.run(TraceApplication.class, args);
    }
}

Configuration

In application.properties set the service name, port and Eureka address for each instance:

# trace-1
spring.application.name=trace-1
server.port=9101
eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/

# trace-2
spring.application.name=trace-2
server.port=9102
eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/

Running the example

Start the Eureka server, then start both services. A request to http://localhost:9101/trace-1 returns Trace. The console logs contain entries such as:

INFO 25272 --- [nio-9101-exec-2] ... :===call trace-1===
INFO 7136 --- [nio-9102-exec-1] ... :===call trace-2===

Each log line is prefixed with a bracketed list added by Sleuth, for example:

[trace-1, f410ab57afd5c145, a9f2118fa2019684, false]

Application name – value of spring.application.name.

Trace ID – a unique identifier that is shared by all services handling the same request.

Span ID – identifier of the current operation (e.g., an HTTP call).

Export flag – false means the data is not sent to an external tracing system such as Zipkin.

Because both services log the same Trace ID, Sleuth successfully propagates the identifier across the service boundary.

Key points

Adding spring-cloud-starter-sleuth and the Spring Cloud dependencies is sufficient to instrument a Spring Boot microservice.

Sleuth automatically creates traceId and spanId for each request and injects them into log statements.

The identifiers are also propagated via HTTP headers, so downstream services receive the same Trace ID.

Export to Zipkin or other back‑ends can be enabled by adding the corresponding starter (e.g., spring-cloud-starter-zipkin).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

eurekaresttemplate
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.