【发布时间】:2020-10-04 18:25:56
【问题描述】:
我有一个带有 spring-cloud-sleuth (Hoxton.SR3) 的 spring boot (2.2.5.RELEASE) 项目。 我想向包含标头的控制器发送请求,并且此标头为:
- 填充在控制器的跨距行李中(即
currentSpan.context().extra()) - 已保存到 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 返回从我的配置文件中读取的行李密钥列表时。
我也在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)调用带有键 baggage-behalf-requested-time 和值 123456 的标头的服务控制器时,我得到 currentSpan.context().extra() 的值 baggage-behalf-requested-time(即 123456)
问题
- 是否需要在标题前加上
baggage-前缀?框架不是应该自己处理吗?或者它只是在我使用 Spring 本身发送请求时才这样做(即RestTemplate)? - 为什么 MDC 没有填充
baggage-behalf-requested-time标头的值?
【问题讨论】:
标签: java spring spring-boot spring-cloud spring-cloud-sleuth