Migrating from Spring Boot 2 to Spring Boot 3: Logging Traces and Spans with Micrometer and Sleuth
Ajay Singh
Posted on May 6, 2023
Introduction
When upgrading from Spring Boot 2 to Spring Boot 3, you may encounter issues with logging trace and span information using Spring Cloud Sleuth. This is because, in Spring Boot 3, Sleuth has been merged with Micrometer, a powerful metrics library. In this article, we will explore how to migrate to Micrometer to enable trace and span logging in Spring Boot 3.
Dependencies
To enable trace and span logging with Micrometer in Spring Boot 3, we need to add the following dependencies to our pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
Configuration
After adding the dependencies, we need to configure our application to use Micrometer for trace and span logging. If you have a log pattern configured anywhere such as logback.xml, you can add the following pattern to log trace and span id: %X{traceId}
and %X{spanId}
After adding these it would look similar to this:
%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId},%X{spanId}] [%thread] ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } %logger{36} - %msg%n
Testing
To ensure that the configuration is working as expected, we can test by sending a request to our application and verifying that the trace and span information is logged correctly.
Conclusion
Migrating from Spring Boot 2 to Spring Boot 3 can be a challenging task, but with the right approach, it can be made easier. By following the steps outlined in this article, you can enable trace and span ID logging with Micrometer and ensure that your logs include the necessary information.
Posted on May 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024