4

Is it possible to log retries attempts on client side with resilience4j please?

Maybe via some kind of configuration, or settings.

Currently, I am using resilience4j with Spring boot Webflux annotation based.

It is working great, the project is amazing.

While we put server logs on server side, to see that a same http call has been made due to a retry (we log time, client IP, request ID, etc...) Would I be possible to have client side logs?

I was expecting to see something like "Resilience4j - client side: 1st attempt failed because of someException, retying with attend number 2. 2nd attempt failed because of someException, retying with attend number 3. 3rd attempt successful!"

Something like that. Is there a property, some config, some setup, that can help to do this easily please? Without adding too much boiler code.

@RestController
public class TestController {

    private final WebClient webClient;

    public TestController(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8443/serviceBgreeting").build();
    }

    @GetMapping("/greeting")
    public Mono<String> greeting() {
        System.out.println("Greeting method is invoked ");
        return someRestCall();
    }

    @Retry(name = "greetingRetry")
    public Mono<String> someRestCall() {
        return this.webClient.get().retrieve().bodyToMono(String.class);
    }

}

Thank you

PatPanda
  • 3,644
  • 9
  • 58
  • 154

3 Answers3

6

Fortunately (or unfortunately) there is an undocumented feature :)

You can add a RegistryEventConsumer Bean in order to add event consumers to any Retry instance.

    @Bean
    public RegistryEventConsumer<Retry> myRetryRegistryEventConsumer() {

        return new RegistryEventConsumer<Retry>() {
            @Override
            public void onEntryAddedEvent(EntryAddedEvent<Retry> entryAddedEvent) {
                entryAddedEvent.getAddedEntry().getEventPublisher()
                   .onEvent(event -> LOG.info(event.toString()));
            }

            @Override
            public void onEntryRemovedEvent(EntryRemovedEvent<Retry> entryRemoveEvent) {

            }

            @Override
            public void onEntryReplacedEvent(EntryReplacedEvent<Retry> entryReplacedEvent) {

            }
        };
    }

Log entry look as follows:

2020-10-26T13:00:19.807034700+01:00[Europe/Berlin]: Retry 'backendA', waiting PT0.1S until attempt '1'. Last attempt failed with exception 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.

2020-10-26T13:00:19.912028800+01:00[Europe/Berlin]: Retry 'backendA', waiting PT0.1S until attempt '2'. Last attempt failed with exception 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.

2020-10-26T13:00:20.023250+01:00[Europe/Berlin]: Retry 'backendA' recorded a failed retry attempt. Number of retry attempts: '3'. Giving up. Last exception was: 'org.springframework.web.client.HttpServerErrorException: 500 This is a remote exception'.
Robert Winkler
  • 1,734
  • 9
  • 8
4

There seems to be a lot of information about this on the web if you Google for "resilience4j retry example logging". I found this as a potential solution:

RetryConfig config = RetryConfig.ofDefaults();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("flightSearchService", config);

...

Retry.EventPublisher publisher = retry.getEventPublisher();
publisher.onRetry(event -> System.out.println(event.toString()));

where you can register a callback to get an event whenever a Retry occurs. This. came from "https://reflectoring.io/retry-with-resilience4j".

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

Configured with application.properties, and using the @Retry annotation, I managed to get some output with

resilience4j.retry.instances.myRetry.maxAttempts=3
resilience4j.retry.instances.myRetry.waitDuration=1s
resilience4j.retry.instances.myRetry.enableExponentialBackoff=true
resilience4j.retry.instances.myRetry.exponentialBackoffMultiplier=2
resilience4j.retry.instances.myRetry.retryExceptions[0]=java.lang.Exception
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import io.github.resilience4j.retry.RetryRegistry;
import io.github.resilience4j.retry.annotation.Retry;

@Service
public class MyService {
    private static final Logger LOG = LoggerFactory.getLogger(MyService.class);

    public MyService(RetryRegistry retryRegistry) {
        // all
        retryRegistry.getAllRetries()
          .forEach(retry -> retry
            .getEventPublisher()
            .onRetry(event -> LOG.info("{}", event))
        );

       // or single
       retryRegistry
            .retry("myRetry")
            .getEventPublisher()
            .onRetry(event -> LOG.info("{}", event));
    }

    @Retry(name = "myRetry")
    public void doSomething() {
        throw new RuntimeException("It failed");
    }
}

eg.

2021-03-31T07:42:23 [http-nio-8083-exec-1] INFO  [myService] - 2021-03-31T07:42:23.228892500Z[UTC]: Retry 'myRetry', waiting PT1S until attempt '1'. Last attempt failed with exception 'java.lang.RuntimeException: It failed'.
2021-03-31T07:42:24 [http-nio-8083-exec-1] INFO  [myService] - 2021-03-31T07:42:24.231504600Z[UTC]: Retry 'myRetry', waiting PT2S until attempt '2'. Last attempt failed with exception 'java.lang.RuntimeException: It failed'.
Julien
  • 1,765
  • 20
  • 26