如何将请求 headers 保存到 MDC

How to get request headers to be saved to MDC

我有一个 spring 引导 (2.2.5.RELEASE) 项目 spring-cloud-sleuth (Hoxton.SR3)。 我想向包含 header 的控制器发送请求,为此 header 为:

  1. 填充在控制器的跨度行李中(即 currentSpan.context().extra()
  2. 已保存到 MDC

我有一个习惯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();
}

tracingProperties.getAllBaggageKeys return 从我的配置文件中读取行李钥匙列表时。

我也在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

当我(通过 POSTMAN)使用 header 键 baggage-behalf-requested-time 和值 123456 调用服务控制器时,我在 currentSpan.context().extra() 中得到 [=16= 的值](即123456

问题

  1. 我需要在 header 前加上 baggage- 前缀对吗?框架不是应该自己处理吗?或者它只是在我用 Spring 本身发送请求(即 RestTemplate)时执行?
  2. 为什么 MDC 没有填充 baggage-behalf-requested-time header 的值?

您有自己的自定义跟踪机制,这就是为什么您需要处理所有 baggage- 前缀等。如果您查看 Sleuth 的 2.2.2.RELEASE,您会看到我们自己为这些字段添加前缀 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) 但没有 baggage- 前缀。在最新版本 (2.2.3.RELEAE) 中,代码进行了重构,但在概念上是相似的。

随心所欲

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

将 HttpServletRequest、HttpServletResponse 作为参数传递。

你可以写customeFilter来拦截每一个请求,并将请求的数据放入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);
  }
}