|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.retry.support; |
| 18 | + |
| 19 | +import java.util.IdentityHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import io.micrometer.core.instrument.Meter; |
| 24 | +import io.micrometer.core.instrument.MeterRegistry; |
| 25 | +import io.micrometer.core.instrument.Tag; |
| 26 | +import io.micrometer.core.instrument.Tags; |
| 27 | +import io.micrometer.core.instrument.Timer; |
| 28 | + |
| 29 | +import org.springframework.lang.Nullable; |
| 30 | +import org.springframework.retry.RetryCallback; |
| 31 | +import org.springframework.retry.RetryContext; |
| 32 | +import org.springframework.retry.RetryListener; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * The {@link RetryListener} implementation for Micrometer {@link Timer}s around retry |
| 37 | + * operations. |
| 38 | + * <p> |
| 39 | + * The {@link Timer#start} is called from the {@link #open(RetryContext, RetryCallback)} |
| 40 | + * and stopped in the {@link #close(RetryContext, RetryCallback, Throwable)}. This |
| 41 | + * {@link Timer.Sample} is associated with the provided {@link RetryContext} to make this |
| 42 | + * {@link MetricsRetryListener} instance reusable for many retry operation. |
| 43 | + * <p> |
| 44 | + * The registered {@value #TIMER_NAME} {@link Timer} has these tags by default: |
| 45 | + * <ul> |
| 46 | + * <li>{@code name} - {@link RetryCallback#getLabel()}</li> |
| 47 | + * <li>{@code retry.count} - the number of attempts - 1; essentially the successful first |
| 48 | + * call means no counts</li> |
| 49 | + * <li>{@code exception} - the thrown back to the caller (after all the retry attempts) |
| 50 | + * exception class name</li> |
| 51 | + * </ul> |
| 52 | + * <p> |
| 53 | + * The {@link #setCustomTags(Iterable)} and {@link #setCustomTagsProvider(Function)} can |
| 54 | + * be used to further customize tags on the timers. |
| 55 | + * |
| 56 | + * @author Artem Bilan |
| 57 | + * @since 2.0.8 |
| 58 | + */ |
| 59 | +public class MetricsRetryListener implements RetryListener { |
| 60 | + |
| 61 | + public static final String TIMER_NAME = "spring.retry"; |
| 62 | + |
| 63 | + private final MeterRegistry meterRegistry; |
| 64 | + |
| 65 | + private final Map<RetryContext, Timer.Sample> retryContextToSample = new IdentityHashMap<>(); |
| 66 | + |
| 67 | + private final Meter.MeterProvider<Timer> retryMeterProvider; |
| 68 | + |
| 69 | + private Tags customTags = Tags.empty(); |
| 70 | + |
| 71 | + private Function<RetryContext, Iterable<Tag>> customTagsProvider = retryContext -> Tags.empty(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Construct an instance based on the provided {@link MeterRegistry}. |
| 75 | + * @param meterRegistry the {@link MeterRegistry} to use for timers. |
| 76 | + */ |
| 77 | + public MetricsRetryListener(MeterRegistry meterRegistry) { |
| 78 | + Assert.notNull(meterRegistry, "'meterRegistry' must not be null"); |
| 79 | + this.meterRegistry = meterRegistry; |
| 80 | + this.retryMeterProvider = Timer.builder(TIMER_NAME) |
| 81 | + .description("Metrics for Spring RetryTemplate") |
| 82 | + .withRegistry(this.meterRegistry); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Supply tags which are going to be used for all the timers managed by this listener. |
| 87 | + * @param customTags the list of additional tags for all the timers. |
| 88 | + */ |
| 89 | + public void setCustomTags(@Nullable Iterable<Tag> customTags) { |
| 90 | + this.customTags = this.customTags.and(customTags); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Supply a {@link Function} to build additional tags for all the timers based on the |
| 95 | + * {@link RetryContext}. |
| 96 | + * @param customTagsProvider the {@link Function} for additional tags with a |
| 97 | + * {@link RetryContext} scope. |
| 98 | + */ |
| 99 | + public void setCustomTagsProvider(Function<RetryContext, Iterable<Tag>> customTagsProvider) { |
| 100 | + Assert.notNull(customTagsProvider, "'customTagsProvider' must not be null"); |
| 101 | + this.customTagsProvider = customTagsProvider; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) { |
| 106 | + this.retryContextToSample.put(context, Timer.start(this.meterRegistry)); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, |
| 112 | + @Nullable Throwable throwable) { |
| 113 | + |
| 114 | + Timer.Sample sample = this.retryContextToSample.remove(context); |
| 115 | + |
| 116 | + Assert.state(sample != null, |
| 117 | + () -> String.format("No 'Timer.Sample' registered for '%s'. Was the 'open()' called?", context)); |
| 118 | + |
| 119 | + Tags retryTags = Tags.of("name", callback.getLabel()) |
| 120 | + .and("retry.count", "" + context.getRetryCount()) |
| 121 | + .and(this.customTags) |
| 122 | + .and(this.customTagsProvider.apply(context)) |
| 123 | + .and("exception", throwable != null ? throwable.getClass().getSimpleName() : "none"); |
| 124 | + |
| 125 | + sample.stop(this.retryMeterProvider.withTags(retryTags)); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments