我可以使用 Spring Cloud Sleuth 根据消息标头值向消息传递范围添加标签吗?

问题描述

当 Spring Boot 应用程序发送消息(到 RabbitMQ)时,我正在使用 Spring Cloud Sleuth 将跨度发送到 zipkin。

我想自定义发送到 zipkin 的信息,以包含一些从传出消息的某些标头填充的额外标签,例如下面的myCustomTag

"traceId": "9fc1f35070446800","parentId": "033b24f12d5fe4e8","id": "fea24862c3e1f374","kind": "PRODUCER","name": "send","timestamp": 1609943208647127,"duration": 1407,"localEndpoint": {
  "serviceName": "foo-service","ipv4": "10.204.71.17"
},"remoteEndpoint": {
  "serviceName": "broker"
},"tags": {
  "channel": "fooEventsChannel","myCustomTag": <some value extracted from the "Bar" header of the outgoing message>
}

是否可以使用 Sleuth/brave 来做到这一点?感觉就像注册(例如)类型为 brave.http.HttpRequestParser 的 bean 的消息传递等效,但我看不到明显的前进方向。

解决方法

您可以使用 TagValueResolverMessageSpanCustomizer,有关详细信息,请参阅文档。