Search code examples
javaspring-bootspring-cloud-sleuthopen-telemetrydistributed-tracing

Headers not getting propagated with open-telemetry and spring-cloud-sleuth


I have integrated open-telemetry collector with spring-cloud-sleuth and trying to test distributed tracing with it. My basic requirement is if app1 receives a header for traceId, it should use the same for logging and tracing. If not, it can generate it's own traceId

I have configured spring.sleuth.baggage.remoteFields/correlationFields as traceId.

spring:
  application:
    name: spring-cloud-sleuth-otel-slf4j
  sleuth:
    otel:
      config:
        trace-id-ratio-based: 1.0
      exporter:
        otlp:
          endpoint: http://otel-collector:4317
    baggage:
     remoteFields: requestId,serviceName,trace_id
     correlationFields: requestId,serviceName,traceId
     tagFields: requestId,serviceName,traceId

I am calling an API in app1 from postman with header X-B3-TraceId. This header gets used in the logs and traces emitted by app1. Now, I call an app2 API from app1 using RestTemplate. In the app2, I am seeing an header named b3 instead of X-B3-TraceId.

However, if I change header name to traceId instead of X-B3-TraceId when calling app1 from postman, it gets propagated to app2. However, in this case, the generated trace by app1 does not use this header traceId but creates a new one. Do I need to add any additional configuration for these headers to get propagated with correct names?


Solution

  • I was able to get it working. By default, the propagation style is set to use a single header which needs to be changed to use multiple headers. This can be done by following steps:- Using brave:-

    @Bean
    PropagationFactorySupplier myPropagationFactorySupplier() {
      return () -> B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
    }
    

    Without using brave:-

        @Bean
        public B3Propagator b3Propagator() {
            return B3Propagator.injectingMultiHeaders();
        }