2

I have a method in EventService that calls an API and handle errors if there is any.

private Mono<ApiResponse> send(String accountId) {
    accountIdBaggageField.updateValue(accountId);
    logger.info("making the call");
    Mono<ApiResponse> res = apiClient.dispatchEvent(accountId);
    return res.doOnError(e -> {
        logger.error("Could not dispatch batch for events");
    });
}

Here is the SleuthConfiguration class that defines accountIdBaggageField bean:

@Configuration
public class SleuthConfiguration {

    @Bean
    public BaggageField accountIdBaggageField() {
        return BaggageField.create(LoggingContextVariables.MDC_ACCOUNT_ID);
    }

    @Bean
    public BaggagePropagationCustomizer baggagePropagationCustomizer(BaggageField accountIdBaggageField) {
        return factoryBuilder -> {
            factoryBuilder.add(remote(accountIdBaggageField));
        };
    }

    @Bean
    public CorrelationScopeCustomizer correlationScopeCustomizer(BaggageField accountIdBaggageField) {
        return builder -> {
            builder.add(createCorrelationScopeConfig(accountIdBaggageField));
        };
    }

    private CorrelationScopeConfig createCorrelationScopeConfig(BaggageField field) {
        return CorrelationScopeConfig.SingleCorrelationField.newBuilder(field)
                .flushOnUpdate()
                .build();
    }
}

Here is the ApiClient's dispatchEvents method:

public Mono<ApiResponse> dispatchEvent(String accountId) {
        return webClient
                .post()
                .uri(properties.getEndpoints().getDispatchEvent(), Map.of("accountId", accountId))
                .retrieve()
                .onStatus(HttpStatus::isError, this::constructException)
                .bodyToMono(ApiResponse.class)
                .onErrorMap(WebClientRequestException.class, e -> new CGWException("Error during dispatching event to Connector Gateway", e));
    }

Here is how I call the send method:

eventService.send("account1");
eventService.send("account2");

The problem here is that the accountIdBaggageField is first set to "account1" and then the io process is started when apiClient.dispatchEvents is called. Before the end of the io process (before getting a response from the api), the second call takes place and the accountIdBaggageField is set to "account2".

Then, when the response of the first request is returned, the error log in doOnError adds the accountId to the log as "account2" but needs to add it as "account1".

Here are the logs:

2023-01-09 11:50:56.791  INFO [account1] [Thread-1] c.t.e.s.impl.EventServiceImpl    making the call

2023-01-09 11:50:56.812  INFO [account2] [Thread-1] c.t.e.s.impl.EventServiceImpl    making the call

2023-01-09 11:50:58.241  INFO [account2] [reactor-http-nio-4] c.t.e.s.impl.EventServiceImpl    Could not dispatch batch for events

2023-01-09 11:50:58.281  INFO [account2] [reactor-http-nio-6] c.t.e.s.impl.EventServiceImpl    Could not dispatch batch for events

As can be seen in the logs, the log on line 3 should have been accountId1 instead of accountId2. How can I fix this situation?

0 Answers0