springboot 3.1.6 webflux trace lost

417 Views Asked by At

I constructed the web client using the following method.

 @Bean
    public XXXApiService xxxApiClient(HttpClient httpClient, WebClient.Builder builder) {
        WebClient webClient = builder
                .baseUrl("XX")
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .defaultStatusHandler(
                        httpStatusCode -> HttpStatus.NOT_FOUND == httpStatusCode,
                        response -> Mono.empty())
                .defaultStatusHandler(
                        HttpStatusCode::is5xxServerError,
                        response -> Mono.error(new RuntimeException(response.statusCode().toString())))
                .build();
        return HttpServiceProxyFactory
                .builder(WebClientAdapter.forClient(webClient))
                .build()
                .createClient(XXXApiService.class);
    }

I found that when entering from webflux and directly calling webclient, the trace can be successfully connected.

However, if it is first done in the form of Flux.fromIterable(xx).flatMap(xx -> webClient.xxx), for example. There is no way to connect the entire trace.

So, may I ask how should I go about solving this problem?

1

There are 1 best solutions below

0
SeverityOne On BEST ANSWER

See if the first comment to this question helps you. It solved the problem for me.

Micrometer tracing context propagration is lost in webclient flatMap function

When I upgraded to Project Reactor 3.6.0, the tracing was propagated from one WebClient call to the next.

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.6.0</version>
</dependency>

With thanks to user @Trind for providing the solution.