Search code examples
webclienttracemicrometerpropagationspring-boot-3

Spring brave creates another traceId with WebClient


I am creating a spring-boot3 service that needs to interact with a spring-boot2 sleuth service.

I configured the spring-boot3 app to propagate B3 trace-ids using WebClient. The problem is that WebClient is creating another trace-id.

This is the endpoint that I execute http://localhost:8080 which in turns calls to http://localhost:9090

@RestController
@SpringBootApplication
public class MyApplication {

  private static final Log logger = LogFactory.getLog(MyApplication.class);
  @Autowired WebClient.Builder builder;

  @RequestMapping("/")
  String home() {
    logger.info("Testing sending request to client");
    String data = builder
        .baseUrl("http://localhost:9090")
        .exchangeStrategies(ExchangeStrategies.builder().codecs(c ->
            c.defaultCodecs().enableLoggingRequestDetails(true)).build()
        )
        .build()
        .get()
        .exchangeToMono(res -> res.bodyToMono(String.class))
        .block();
    return data;
  }
}

A Config to use B3 style traces

@Configuration
public class Config {
  @Bean
  public Tracing braveTracing() {
    return Tracing.newBuilder()
        .propagationFactory(
            B3Propagation.newFactoryBuilder()
                .injectFormat(B3Propagation.Format.MULTI)
                .build())
        .build();
  }
}

properties

spring.application.name=demo
logging.level.org.springframework.web=TRACE
#spring.mvc.log-request-details=true
#spring.http.log-request-details=true

spring.codec.log-request-details=true
management.tracing.propagation.type=B3

logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=TRACE

management.tracing.enabled = true
management.tracing.sampling.probability = 1.0

Dependencies in build.gradle

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

Finally the logs I got:

2024-04-16T15:10:27.010-04:00 TRACE 34890 --- [demo] [nio-8080-exec-1] [121d49ff22764a67                -121d49ff22764a67] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.demo.MyApplication#home()
2024-04-16T15:10:27.027-04:00 TRACE 34890 --- [demo] [nio-8080-exec-1] [121d49ff22764a67                -121d49ff22764a67] o.s.web.method.HandlerMethod             : Arguments: []
2024-04-16T15:10:27.028-04:00  INFO 34890 --- [demo] [nio-8080-exec-1] [121d49ff22764a67                -121d49ff22764a67] com.example.demo.MyApplication           : Testing sending request to client
2024-04-16T15:10:27.138-04:00 TRACE 34890 --- [demo] [nio-8080-exec-1] [121d49ff22764a67                -121d49ff22764a67] o.s.w.r.f.client.ExchangeFunctions       : [2f40d44] HTTP GET http://localhost:9090, headers=[X-B3-TraceId:"8089985fbcccddfa", X-B3-SpanId:"8089985fbcccddfa", X-B3-Sampled:"1"]

The trace-id in the spring-boot3 app is 121d49ff22764a67 and the one that's propagated is 8089985fbcccddfa. What am I missing to propagate the first trace-id 121d49ff22764a67 ?


Solution

  • Answering my own question.

    No need to define this

    @Configuration
    public class Config {
      @Bean
      public Tracing braveTracing() {
        return Tracing.newBuilder()
            .propagationFactory(
                B3Propagation.newFactoryBuilder()
                    .injectFormat(B3Propagation.Format.MULTI)
                    .build())
            .build();
      }
    }
    

    To use B3 style recognizable by spring boot 2 sleuth just need to define configuration management.tracing.propagation.type=B3_MULTI