Michiel
Michiel

Reputation: 3500

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?

Upvotes: 5

Views: 13506

Answers (3)

Jonathan
Jonathan

Reputation: 21

Spring Boot 3.0 doesn't let dev to add multiple propagation type. Since 3.0.8, this bug is fixed and now, you can specify multiple values.

Release note : https://github.com/spring-projects/spring-boot/releases/tag/v3.0.8

Fixed in : https://github.com/spring-projects/spring-boot/pull/35611

Upvotes: 2

Michiel
Michiel

Reputation: 3500

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:

  • b3 to use the concatenated b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} header
  • b3_multi to use separate headers such as X-B3-SpanId and X-B3-TraceId
  • w3c to use the traceparent header

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.

Upvotes: 11

dpittori
dpittori

Reputation: 61

Documentation says it is not possible :

Since the default propagation format is w3c and in Spring Boot 3.0 we don't support multiple propagation types, you should set in your Sleuth & Boot 2.x application spring.sleuth.propagation.type=w3c,b3 so that you publish the headers in 2 formats. One for the current Boot 2.x applications (b3) and one for the new Boot 3.x applications (w3c).

Spring-Cloud-Sleuth-3.1-Migration-Guide

In our case, we added a custom interceptor over our OkHttpClient to propagate both formats.

Upvotes: 2

Related Questions