简体   繁体   中英

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

I'm replacing Spring Cloud Sleuth to generate log correlation with the new Micrometer Tracing for Spring Boot 3.

I've been following this blog post to configure a sample project

The traceId/spanId don't seem to be automatically generated per request:

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

currentSpan is null and the log shows empty strings:

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

This is my current config:

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

And the dependencies:

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")
}

Why isn't it working?

EDIT:

WebMVC applications aren't affected by this problem, and log the correlation information after upgrading.

There seems to be a change of behaviour for Webflux applications though. There's and open issue about this .

There are several ways to achieve it , the following excerpt shows two of them, ContextSnapshot.setThreadLocalsFrom and the handle() operator

    @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)
                        }
                }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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