Search code examples
javaspringspring-bootspring-cloudspring-cloud-sleuth

How to get request headers to be saved to MDC


I have a spring boot (2.2.5.RELEASE) project with spring-cloud-sleuth (Hoxton.SR3). I want to send a request to a controller containing a header and for this header to be:

  1. Populated in controller's span baggage (i.e. currentSpan.context().extra())
  2. Saved to MDC

I have a custom TracingConfiguration

@Bean
public Tracing tracing(@Value("${spring.application.name}") String serviceName, TracingProperties tracingProperties,
                       CurrentTraceContext currentTraceContext) {

    String profile = String.join(",", env.getActiveProfiles());

    log.info("Enable tracing for service {}", serviceName + ":" + profile);
    return Tracing.newBuilder()
            .localServiceName(serviceName + ":" + profile)
            .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
                    .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs
                    .build()
            )
            .sampler(Sampler.NEVER_SAMPLE)
            .propagationFactory(
                    ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
                            .addPrefixedFields(TracingConstant.BAGGAGE_HEADER_PREFIX, tracingProperties.getAllBaggageKeys())
                            .build())
            .build();
}

When tracingProperties.getAllBaggageKeys return a list of baggage keys read from my configuration file.

I also defined in application.yml:

spring:
  sleuth:
    log:
      slf4j:
        whitelisted-mdc-keys:
          - behalf-flow-id
          - behalf-requested-time
          - x-behalf-ach-file-name
          - behalf-principal-id
          - x-http-request-id
          - behalf-principal
          - requestid
    baggage-keys:
      - behalf-flow-id
      - behalf-requested-time
      - x-behalf-ach-file-name
      - behalf-principal-id
      - x-http-request-id
      - behalf-principal
      - requestid

When I call (through POSTMAN) the service controller with header with key baggage-behalf-requested-time and value 123456 I get in currentSpan.context().extra() the value of baggage-behalf-requested-time (i.e. 123456)

Questions

  1. Is it right that I need to prefix my header with baggage-? Isn't the framework suppose to handle it by itself? Or it just do it when I send a request with Spring itself (i.e. RestTemplate)?
  2. Why MDC is not populated with the value of baggage-behalf-requested-time header?

Solution

  • You have your custom tracing mechanism, that's why you need to take care of all the baggage- prefixing etc. If you check out the 2.2.2.RELEASE of Sleuth, you'll see that we do prefix those fields ourselves https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.2.2.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java#L168-L174 Then, they get also populated to the MDC (https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.2.2.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java) but without the baggage- prefix. In the latest release (2.2.3.RELEAE) the code got refactored but conceptually it's similar.