Search code examples
spring-bootspring-webfluxmicrometermicrometer-tracing

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.


Solution

  • Currently there's an even simpler solution placing Hooks.enableAutomaticContextPropagation(); in your main method.

      public static void main(final String[] args) {
        Hooks.enableAutomaticContextPropagation();
        SpringApplication.run(Application.class, args);
      }
    

    With that in place we don't have to do anything else to transparently propagate and log the tracing information in Webflux applications.

    Micrometer doc reference