'Spring Interceptor has different traceId for afterCompletion method with Spring sleuth 3.x.x

I am currently working on a Spring boot project. Recently I upgraded the version of my Spring boot from 2.3.3 to 2.6.6. In Spring boot 2.3.3 I was using Spring cloud sleuth 2.x.x which now has been upgraded to 3.x.x due to the Spring cloud dependency management BOM. The POM is as follows

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
    </dependencies>

I have an interceptor as follows

@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {

@Autowired
private Tracer tracer;

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {
    log.info("Execution completed with traceId: {}", tracer.currentSpan().context().traceIdString());
}

Earlier this code was giving me the same traceId for all the logs for that particular request. But with Spring sleuth 3.x.x I'm getting a different traceId in afterCompletion() of the Spring Interceptor. Can someone please help

Note: I know this is a deliberate example but I have a similar code in my project



Solution 1:[1]

That's a bug in Sleuth it seems or some behaviour has changed in Reactor / WebClient - https://github.com/spring-cloud/spring-cloud-sleuth/issues/2075

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin Grzejszczak