Reputation: 24442
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.
Upvotes: 2
Views: 9012
Reputation: 4911
The config spring.reactor.context-propagation: AUTO
worked for me in spring boot 3.2+, which is equivalent to calling Hooks.enableAutomaticContextPropagation()
from the main
method, as mentioned in this issue
Upvotes: 0
Reputation: 69
Feel free to check my demo project spring-cloud-gateway-upgrade with branch 'spring-boot-3.x-upgrade' (based on my question). It shows the additional setup needed to make tracing work for all wanted loggers (e.g. access-log)
Upvotes: 1
Reputation: 24442
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.
Upvotes: 2
Reputation: 24442
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)
}
}
}
}
Upvotes: 2