springspring-bootspring-cloud-sleuthmicrometer-tracing

Use both B3 and W3C trace formats in trace propagation between Spring Boot 2 & 3 applications


I am migrating from Spring Boot 2 to Spring boot 3, and switched from spring cloud sleuth to io.micrometer:micrometer-tracing-bridge-brave in the migration process. I noticed that Spring Boot 2 services use b3 headers to propagate span & trace IDs, while the new Spring Boot 3 projects use a w3c traceparent header.

To still be able to trace messages through multiple Spring Boot 2 and Spring Boot 3 services in our own domain, I configured every Spring Boot 3 service in our domain to keep using the "old" B3 format using this stackoverflow post:

@Bean
public Tracing braveTracing() {
    return Tracing.newBuilder()
            .propagationFactory(B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE).build())
            .build();
}

Which works, however I'd rather have the Spring Boot 3 applications recognize both formats and (if possible) propagate both formats as well. The reason is that applications outside our domain expect the W3C header instead of the B3 header. Would anyone know if this is possible?


Solution

  • For anyone interested, I managed to use multiple tracing header types by removing the Tracing bean I configured in my original post, and by setting the consume and produce properties in the application.yml:

    management:
      tracing:
        propagation:
          consume: [b3, w3c]
          produce: [b3, w3c]
          # type: [b3, w3c] if consume and produce are the same, type can be used as well
    

    Three propagation types can be used:

    More info regarding the trace headers can be found here.

    Be sure to add the @AutoConfigureObservability annotation to SpringBootTests if you plan to test the tracing setup in your application. Also, ensure the RestTemplate bean is constructed using an injected RestTemplateBuilder instead of using new RestTemplate(), as the builder adds tracing configuration by default.