Eyal Ringort
Eyal Ringort

Reputation: 603

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?

Upvotes: 4

Views: 6095

Answers (3)

Gaurao Burghate
Gaurao Burghate

Reputation: 307

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);
  }
}

Upvotes: 1

noob
noob

Reputation: 399

Just do

    public String someMethod(HttpServletRequest request, HttpServletResponse httprespons)
{
 MDC.put(request.getHeader("header_name"););
}

pass HttpServletRequest,HttpServletResponse as parameter.

Upvotes: 0

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11169

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.

Upvotes: 2

Related Questions