【问题标题】:Spring Boot 3 Webflux application with Micrometer Tracing not showing traceId and spanId in the console logs带有 Micrometer Tracing 的 Spring Boot 3 Webflux 应用程序未在控制台日志中显示 traceId 和 spanId
【发布时间】:2022-12-22 21:16:28
【问题描述】:

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

我一直在关注this blog post配置一个sample project

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 应用程序的行为似乎发生了变化。还有open issue about this

【问题讨论】:

标签: spring-boot spring-webflux micrometer micrometer-tracing


【解决方案1】:

several ways to achieve it,下面的摘录显示了其中两个,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)
                        }
                }
        }
    }

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2021-08-31
    • 2023-02-26
    • 2023-02-22
    • 2021-04-17
    • 2020-08-21
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多