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:
- Populated in controller's span baggage (i.e.
currentSpan.context().extra()) - 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
- 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)? - Why MDC is not populated with the value of
baggage-behalf-requested-timeheader?