Upgrading Sleuth for Spring Boot 3.0

08-01-2023 – by Gustavo Aquino · 2 min read

#java#spring#observability

Context

My team and I were recently working on library upgrades for APIs built on Spring Boot. These upgrades were happening in projects running the framework on version 2.7.X, and we wanted to take the opportunity to move up to version 3.0.1, made available as Generally Available in late November 2022.

The upgrade guide available in the framework's official repository went smoothly, with the build and automated tests running without issues.

However, when we ran the project and looked at the logs, we noticed the Trace Id and Span Id were missing from the records, which are normally enriched through the Spring Cloud Sleuth library.

2023-01-06T20:06:46.830-03:00  INFO [my-test-app,<TRACE_ID_HERE_BUT_EMPTY>,<SPAN_ID_HERE_BUT_EMPTY>] 29324 --- [nio-8080-exec-6] c.a.Loren : Loren ipsun

Problem

A quick read of the Sleuth documentation tells us this project was absorbed by Micrometer Tracing starting with Spring Boot 3.0, and therefore appears to be deprecated starting with version 3.1. This is also confirmed on Micrometer's official GitHub, which provides a migration guide.

However, the migration guide doesn't seem complete, since following it didn't result in trace data being printed correctly.

So the goal of the walkthrough below is to help you with this migration.

Solution

  1. Check that the project is using Spring Boot version 3.0;
plugins {
    id("org.springframework.boot") version "3.0.1"
}
  1. Remove the Sleuth dependency;
implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
  1. Add the Micrometer Tracing dependency along with the bridge to Brave, which is responsible for creating the tracers;
implementation("io.micrometer:micrometer-tracing-bridge-brave")
  1. If you don't already have it, add the Spring Actuator dependency. It's required for Micrometer's autoconfiguration;
implementation("org.springframework.boot:spring-boot-starter-actuator")
  1. Finally, following Micrometer's migration guide, add the log pattern property in your .properties or .yml file so that the Trace Id and Span Id get printed;
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

When you run the project again, the Trace and Span Ids will start working correctly.

2023-01-06T20:16:46.830-03:00  INFO [my-test-app,63b8a986c713b025bfcb74bfc7c64a77,bfcb74bfc7c64a77] 29324 --- [nio-8080-exec-6] c.a.Loren : Loren ipsun

If you have any questions, feel free to reach out through my contacts.

Happy coding, everyone. 😎

References

  1. Spring Boot 3.0 GA release
  2. Spring Boot 3.0 migration guide
  3. Spring Cloud Sleuth
  4. Micrometer Tracing documentation
  5. Sleuth 3.1 to Micrometer migration guide

Profile picture

Written by Gustavo Aquino, Master of Science, Software Engineer, and founder of Decodifique.com. You can find me on LinkedIn.