[英]TraceId and SpanId are null in WebFilter even when it is registered after TraceWebFilter
I'm currently using 2020.0.0
with Spring Boot 2.4.2
and registered a custom web filter that logs information from the request (Order is webProperties.filterOrder + 1
-- so it should be registered after TraceWebFilter).我目前正在使用带有2020.0.0
Boot 2.4.2
的 2020.0.0 并注册了一个自定义 web 过滤器,该过滤器记录了来自请求的信息(顺序为webProperties.filterOrder + 1
- 所以它应该在 TraceWebFilter 之后注册)。 For some reason, traceId and spanId are not in MDC (and therefore not logging).由于某种原因,traceId 和 spanId 不在 MDC 中(因此不记录)。
WebFilter implementation:网络过滤器实现:
class TraceWebFilter(
private val webProperties: SleuthWebProperties,
private val tracer: Tracer
): WebFilter, Ordered {
override fun getOrder(): Int = webProperties.filterOrder + 1
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
logger.debug { "Building trace web filter" }
return Mono.defer {
MDC.put("Request-Method", exchange.request.method?.toString())
MDC.put("Request-Path", exchange.request.path.toString())
logger.info { tracer.currentSpan() }
logger.info { ">> headers=${exchange.request.headers}" }
logger.info { ">> method=${exchange.request.method}" }
logger.info { ">> path=${exchange.request.path}" }
exchange.response.beforeCommit {
Mono.fromRunnable {
logger.info { "<< headers=${exchange.response.headers}" }
logger.info { "<< status=${exchange.response.rawStatusCode}" }
}
}
chain.filter(exchange)
}
}
}
Logs:日志:
app_1 | 2021-01-28 04:43:24.420 DEBUG [user-service,method=,path=,traceId=,spanId=] 1 --- [p-nio-80-exec-2] o.s.c.s.instrument.web.TraceWebFilter : Received a request to uri [/users]
app_1 | 2021-01-28 04:43:24.566 DEBUG [user-service,method=,path=,traceId=,spanId=] 1 --- [p-nio-80-exec-2] o.s.c.s.instrument.web.TraceWebFilter : Handled receive of span RealSpan(9b7383611e5a3bc2/9b7383611e5a3bc2)
app_1 | 2021-01-28 04:43:24.633 DEBUG [user-service,method=,path=,traceId=,spanId=] 1 --- [p-nio-80-exec-2] c.p.p.components.tracing.TraceWebFilter : Building trace web filter
app_1 | 2021-01-28 04:43:24.638 INFO [user-service,method=POST,path=/users,traceId=,spanId=] 1 --- [p-nio-80-exec-2] c.p.p.components.tracing.TraceWebFilter : null
app_1 | 2021-01-28 04:43:24.694 INFO [user-service,method=POST,path=/users,traceId=,spanId=] 1 --- [p-nio-80-exec-2] c.p.p.components.tracing.TraceWebFilter : >> headers=[content-type:"application/json", user-agent:"PostmanRuntime/7.26.8", accept:"*/*", cache-control:"no-cache", postman-token:"18d99e30-ce7a-4659-a2c1-21020d8bf2b5", host:"localhost:9100", accept-encoding:"gzip, deflate, br", connection:"keep-alive", content-length:"2"]
app_1 | 2021-01-28 04:43:24.699 INFO [user-service,method=POST,path=/users,traceId=,spanId=] 1 --- [p-nio-80-exec-2] c.p.p.components.tracing.TraceWebFilter : >> method=POST
app_1 | 2021-01-28 04:43:24.747 INFO [user-service,method=POST,path=/users,traceId=,spanId=] 1 --- [p-nio-80-exec-2] c.p.p.components.tracing.TraceWebFilter : >> path=/users
Please notice the following:请注意以下事项:
Any clues as to why this is happening?关于为什么会发生这种情况的任何线索?
If you look at TraceWebFilter
, you will see that it does not put anything into the MDC
( MDC
is basically a ThreadLocal
and you are in an async event loop).如果您查看TraceWebFilter
,您会发现它没有将任何内容放入MDC
( MDC
基本上是一个ThreadLocal
并且您处于异步事件循环中)。
But it interacts with the Tracer
that you can also inject into your filter and get the current Span from it: tracer.currentSpan()
and from the TraceContext
of the Span
you can get the traceId
and spanId
.但它与Tracer
交互,您也可以将其注入过滤器并从中获取当前 Span: tracer.currentSpan()
并从Span
的TraceContext
中获取traceId
和spanId
。
Also, it puts the Span
into the Exchange Attributes:此外,它将Span
放入 Exchange 属性中:
this.exchange.getAttributes().put(TRACE_REQUEST_ATTR, span);
I recommend injecting the Tracer
into your filter and getting the current Span
from it.我建议将Tracer
注入您的过滤器并从中获取当前Span
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.