简体   繁体   English

TraceId 和 SpanId 在 WebFilter 中是 null,即使它是在 TraceWebFilter 之后注册的

[英]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:请注意以下事项:

  • traceId and spanId in the logs are null日志中的traceId和spanId为null
  • logged tracer.currentSpan in logs is null日志中记录的 tracer.currentSpan 为 null

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 ,您会发现它没有将任何内容放入MDCMDC基本上是一个ThreadLocal并且您处于异步事件循环中)。

  1. 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()并从SpanTraceContext中获取traceIdspanId

  2. 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.

相关问题 迁移到 Spring Boot 3 后 TraceId 和 SpanId 不可用 - TraceId and SpanId not available after migrating to Spring Boot 3 traceId 和 spanId 在整个记录器中都是一样的 - traceId and spanId are coming as same throughout the loggers Sleuth traceId 和 spanId 不会传播到 parallelStream 工作线程 - Sleuth traceId and spanId are not propagated to parallelStream worker threads 在 Sleuth 的日志中看不到 traceId 和 spanId - Can't see traceId and spanId in log for Sleuth 哪个记录 X-B3-SpanId 或 SpanId? X-B3-TraceId 还是 TraceId? (春天的侦探) - Which to log X-B3-SpanId or SpanId? X-B3-TraceId or TraceId? (Spring sleuth) Spring 云侦探 3.0.1 使用 logback 在日志中生成 traceid 和 spanid - Spring cloud sleuth 3.0.1 generate traceid & spanid in logs using logback Sleuth 不会在 Spring Boot 应用程序中打印 spanId、traceId - Sleuth doesnt print spanId,traceId in spring boot application FirebaseUser实例不为null。 即使清除数据后,是否仍在同一设备上检索第一个注册用户? - FirebaseUser instance not null. Is it retrieving the first registered user on the same device even after clearing data? 如何获取 log4j2 中的 trace id 和 span id,格式为 [traceId, spanId ]? - How to get trace id and span id in the log4j2, in the format [traceId, spanId ]? 在使用带有新关键字的 resttemplate 进行 API 调用时,无法将 Sleuth traceid 和 spanid 传递给下游服务 - Not able to pass Sleuth traceid and spanid to downstream services while making API calls using resttemplate with new keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM