繁体   English   中英

带有 Micrometer Tracing 的 Spring Boot 3 Webflux 应用程序未在控制台日志中显示 traceId 和 spanId

[英]Spring Boot 3 Webflux application with Micrometer Tracing not showing traceId and spanId in the console logs

我正在替换 Spring Cloud Sleuth,以使用新的 Micrometer Tracing for Spring Boot 3 生成日志相关性。

我一直在关注这篇博文来配置示例项目

traceId/spanId 似乎不会根据请求自动生成:

    @GetMapping("/hello")
    fun hello(): String {
        val currentSpan: Span? = tracer.currentSpan()
        logger.info("Hello!")
        return "hello"
    }

currentSpan为 null,日志显示空字符串:

2022-11-28T14:53:05.335+01:00  INFO [server,,] 9176 --- [ctor-http-nio-2] d.DemotracingApplication$$SpringCGLIB$$0 : Hello!

这是我当前的配置:

logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

以及依赖项:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-aop")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.micrometer:micrometer-tracing-bridge-brave")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("io.micrometer:micrometer-registry-prometheus")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

为什么它不起作用?

编辑:

WebMVC 应用不受此问题影响,升级后会记录关联信息。

不过,Webflux 应用程序的行为似乎发生了变化。 关于这个有一个未解决的问题

几种方法可以实现它,下面的摘录显示了其中的两种方法, ContextSnapshot.setThreadLocalsFromhandle()运算符

    @GetMapping("/hello")
    fun hello(): Mono<String> {
        return Mono.deferContextual { contextView: ContextView ->
            ContextSnapshot.setThreadLocalsFrom(contextView, ObservationThreadLocalAccessor.KEY)
                .use { scope: ContextSnapshot.Scope ->
                    val traceId = tracer.currentSpan()!!.context().traceId()
                    logger.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello!", traceId)
                    webClient.get().uri("http://localhost:7654/helloWc")
                        .retrieve()
                        .bodyToMono(String::class.java)
                        .handle { t: String, u: SynchronousSink<String> ->
                            logger.info("Retrieved helloWc {}", t)
                            u.next(t)
                        }
                }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM