|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.metrics; |
18 | 18 |
|
| 19 | +import java.util.Set; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
19 | 21 | import java.util.concurrent.atomic.AtomicBoolean; |
20 | 22 | import java.util.function.Supplier; |
21 | 23 |
|
|
28 | 30 | import org.springframework.util.Assert; |
29 | 31 |
|
30 | 32 | /** |
31 | | - * {@link MeterFilter} to log only once a warning message and deny a {@link Id Meter.Id}. |
| 33 | + * {@link MeterFilter} to log a single warning message and deny a {@link Id Meter.Id} |
| 34 | + * after a number of attempts for a given tag. |
32 | 35 | * |
33 | 36 | * @author Jon Schneider |
34 | 37 | * @author Dmytro Nosan |
| 38 | + * @author Phillip Webb |
35 | 39 | * @since 2.0.5 |
36 | 40 | */ |
37 | 41 | public final class OnlyOnceLoggingDenyMeterFilter implements MeterFilter { |
38 | 42 |
|
39 | | - private static final Log logger = LogFactory.getLog(OnlyOnceLoggingDenyMeterFilter.class); |
| 43 | + private final Log logger; |
40 | 44 |
|
41 | 45 | private final AtomicBoolean alreadyWarned = new AtomicBoolean(); |
42 | 46 |
|
| 47 | + private final String meterNamePrefix; |
| 48 | + |
| 49 | + private final int maximumTagValues; |
| 50 | + |
| 51 | + private final String tagKey; |
| 52 | + |
43 | 53 | private final Supplier<String> message; |
44 | 54 |
|
| 55 | + private final Set<String> observedTagValues = ConcurrentHashMap.newKeySet(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a new {@link OnlyOnceLoggingDenyMeterFilter} instance. |
| 59 | + * @param message the message to log |
| 60 | + * @since 2.0.5 |
| 61 | + * @deprecated since 3.4.12 in favor of |
| 62 | + * {@link #OnlyOnceLoggingDenyMeterFilter(String, String, int, String)} |
| 63 | + */ |
| 64 | + @Deprecated(since = "3.4.12", forRemoval = true) |
45 | 65 | public OnlyOnceLoggingDenyMeterFilter(Supplier<String> message) { |
| 66 | + this(null, null, null, 0, message); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create a new {@link OnlyOnceLoggingDenyMeterFilter} with an upper bound on the |
| 71 | + * number of tags produced by matching metrics. |
| 72 | + * @param meterNamePrefix the prefix of the meter name to apply the filter to |
| 73 | + * @param tagKey the tag to place an upper bound on |
| 74 | + * @param maximumTagValues the total number of tag values that are allowable |
| 75 | + * @since 3.4.12 |
| 76 | + */ |
| 77 | + public OnlyOnceLoggingDenyMeterFilter(String meterNamePrefix, String tagKey, int maximumTagValues) { |
| 78 | + this(meterNamePrefix, tagKey, maximumTagValues, (String) null); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Create a new {@link OnlyOnceLoggingDenyMeterFilter} with an upper bound on the |
| 83 | + * number of tags produced by matching metrics. |
| 84 | + * @param meterNamePrefix the prefix of the meter name to apply the filter to |
| 85 | + * @param tagKey the tag to place an upper bound on |
| 86 | + * @param maximumTagValues the total number of tag values that are allowable |
| 87 | + * @param hint an additional hint to add to the logged message or {@code null} |
| 88 | + * @since 3.4.12 |
| 89 | + */ |
| 90 | + public OnlyOnceLoggingDenyMeterFilter(String meterNamePrefix, String tagKey, int maximumTagValues, String hint) { |
| 91 | + this(null, meterNamePrefix, tagKey, maximumTagValues, |
| 92 | + () -> String.format("Reached the maximum number of '%s' tags for '%s'.%s", tagKey, meterNamePrefix, |
| 93 | + (hint != null) ? " " + hint : "")); |
| 94 | + } |
| 95 | + |
| 96 | + private OnlyOnceLoggingDenyMeterFilter(Log logger, String meterNamePrefix, String tagKey, int maximumTagValues, |
| 97 | + Supplier<String> message) { |
46 | 98 | Assert.notNull(message, "'message' must not be null"); |
| 99 | + Assert.isTrue(maximumTagValues >= 0, "'maximumTagValues' must be positive"); |
| 100 | + this.logger = (logger != null) ? logger : LogFactory.getLog(OnlyOnceLoggingDenyMeterFilter.class); |
| 101 | + this.meterNamePrefix = meterNamePrefix; |
| 102 | + this.maximumTagValues = maximumTagValues; |
| 103 | + this.tagKey = tagKey; |
47 | 104 | this.message = message; |
48 | 105 | } |
49 | 106 |
|
50 | 107 | @Override |
51 | 108 | public MeterFilterReply accept(Id id) { |
52 | | - if (logger.isWarnEnabled() && this.alreadyWarned.compareAndSet(false, true)) { |
53 | | - logger.warn(this.message.get()); |
| 109 | + if (this.meterNamePrefix == null) { |
| 110 | + return logAndDeny(); |
| 111 | + } |
| 112 | + String tagValue = id.getName().startsWith(this.meterNamePrefix) ? id.getTag(this.tagKey) : null; |
| 113 | + if (tagValue != null && !this.observedTagValues.contains(tagValue)) { |
| 114 | + if (this.observedTagValues.size() >= this.maximumTagValues) { |
| 115 | + return logAndDeny(); |
| 116 | + } |
| 117 | + this.observedTagValues.add(tagValue); |
| 118 | + } |
| 119 | + return MeterFilterReply.NEUTRAL; |
| 120 | + } |
| 121 | + |
| 122 | + private MeterFilterReply logAndDeny() { |
| 123 | + if (this.logger.isWarnEnabled() && this.alreadyWarned.compareAndSet(false, true)) { |
| 124 | + this.logger.warn(this.message.get()); |
54 | 125 | } |
55 | 126 | return MeterFilterReply.DENY; |
56 | 127 | } |
|
0 commit comments