I am using spring-boot 3.0.13 and observing that traceId
from currentSpan
is not propagating inside the Mono
. My code is like below
@Component
@AllArgsConstructor
public class Example {
private final Tracer tracer;
void do(int fetchSummaryConcurrency) {
Flux.fromIterable(tasks)
.flatMap(
batch ->
Mono.fromSupplier(
() -> {
System.out.println(getTraceId());
return List.of()
})
.subscribeOn(Schedulers.boundedElastic()),
fetchSummaryConcurrency)
.doOnError(e -> log.error("Exception fetching record", e))
.onErrorResume(e -> Flux.empty())
.toStream()
.flatMap(Collection::stream)
.toList();
}
}
public String traceId() {
if (tracer.currentSpan() == null) {
return Strings.EMPTY;
}
return tracer.currentSpan().context().traceId();
}
getTraceId()
returns empty.
I also tried doing Hooks.enableAutomaticContextPropagation();
as mentioned here. The only way to make this work is to do like below.
val traceContext = tracer.currentTraceContext().context();
Flux.fromIterable(tasks)
.flatMap(
batch ->
Mono.fromSupplier(
() -> {
try (val ignored =
tracerUtils
.getTracer()
.currentTraceContext()
.newScope(traceContext)) {
System.out.println(getTraceId());
return List.of();
}
}
})
.subscribeOn(Schedulers.boundedElastic()),
fetchSummaryConcurrency)
.doOnError(e -> log.error("Exception fetching record", e))
.onErrorResume(e -> Flux.empty())
.toStream()
.flatMap(Collection::stream)
.toList();
Can someone let me know if this is expected or if there were some known issues in spring 3.0.x and 3.1.x which is causing this issue?