Class MetricsRetryListener

java.lang.Object
org.springframework.retry.support.MetricsRetryListener
All Implemented Interfaces:
RetryListener

public class MetricsRetryListener extends Object implements RetryListener
The RetryListener implementation for Micrometer Timers around retry operations.

The Timer.start() is called from the open(RetryContext, RetryCallback) and stopped in the close(RetryContext, RetryCallback, Throwable). This Timer.Sample is associated with the provided RetryContext to make this MetricsRetryListener instance reusable for many retry operation.

The registered "spring.retry" Timer has these tags by default:

  • name - RetryCallback.getLabel()
  • retry.count - the number of attempts - 1; essentially the successful first call means no counts
  • exception - the thrown back to the caller (after all the retry attempts) exception class name

The setCustomTags(Iterable) and setCustomTagsProvider(Function) can be used to further customize tags on the timers.

Since:
2.0.8
Author:
Artem Bilan, Huijin Hong
  • Field Details

  • Constructor Details

    • MetricsRetryListener

      public MetricsRetryListener(io.micrometer.core.instrument.MeterRegistry meterRegistry)
      Construct an instance based on the provided MeterRegistry.
      Parameters:
      meterRegistry - the MeterRegistry to use for timers.
  • Method Details

    • setCustomTags

      public void setCustomTags(@Nullable Iterable<io.micrometer.core.instrument.Tag> customTags)
      Supply tags which are going to be used for all the timers managed by this listener.
      Parameters:
      customTags - the list of additional tags for all the timers.
    • setCustomTagsProvider

      public void setCustomTagsProvider(Function<RetryContext,Iterable<io.micrometer.core.instrument.Tag>> customTagsProvider)
      Supply a Function to build additional tags for all the timers based on the RetryContext.
      Parameters:
      customTagsProvider - the Function for additional tags with a RetryContext scope.
    • open

      public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T,E> callback)
      Description copied from interface: RetryListener
      Called before the first attempt in a retry. For instance, implementers can set up state that is needed by the policies in the RetryOperations. The whole retry can be vetoed by returning false from this method, in which case a TerminatedRetryException will be thrown.
      Specified by:
      open in interface RetryListener
      Type Parameters:
      T - the type of object returned by the callback
      E - the type of exception it declares may be thrown
      Parameters:
      context - the current RetryContext.
      callback - the current RetryCallback.
      Returns:
      true if the retry should proceed.
    • close

      public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T,E> callback, @Nullable Throwable throwable)
      Description copied from interface: RetryListener
      Called after the final attempt (successful or not). Allow the listener to clean up any resource it is holding before control returns to the retry caller.
      Specified by:
      close in interface RetryListener
      Type Parameters:
      T - the return value
      E - the exception type
      Parameters:
      context - the current RetryContext.
      callback - the current RetryCallback.
      throwable - the last exception that was thrown by the callback.