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:
currentSpan.context().extra())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
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)?baggage-behalf-requested-time header?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.
You can write customeFilter to intercept every request and put data from request into MDC.
@Component
public class CustomFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
final String correlationId = getCorrelationIdFromHeader(req);
MDC.put(HttpConstants.CORRELATION_ID_HEADER_NAME, correlationId);
chain.doFilter(req, res);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With