Skip to content

Commit 9425188

Browse files
authored
Support message supplier in WarnThenDebugLogger (#3424)
This commit also updates to use its supplier variants where beneficial.
1 parent cfba5ad commit 9425188

File tree

9 files changed

+36
-14
lines changed

9 files changed

+36
-14
lines changed

implementations/micrometer-registry-cloudwatch/src/main/java/io/micrometer/cloudwatch/CloudWatchMeterRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ private List<Dimension> toDimensions(List<Tag> tags) {
301301

302302
private boolean isAcceptableTag(Tag tag) {
303303
if (StringUtils.isBlank(tag.getValue())) {
304-
warnThenDebugLogger.log("Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
304+
warnThenDebugLogger
305+
.log(() -> "Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
305306
return false;
306307
}
307308
return true;

implementations/micrometer-registry-cloudwatch2/src/main/java/io/micrometer/cloudwatch2/CloudWatchMeterRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ private List<Dimension> toDimensions(List<Tag> tags) {
295295

296296
private boolean isAcceptableTag(Tag tag) {
297297
if (StringUtils.isBlank(tag.getValue())) {
298-
warnThenDebugLogger.log("Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
298+
warnThenDebugLogger
299+
.log(() -> "Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
299300
return false;
300301
}
301302
return true;

implementations/micrometer-registry-dynatrace/src/main/java/io/micrometer/dynatrace/v1/DynatraceNamingConventionV1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private String sanitizeName(String name) {
6464
}
6565
String sanitized = NAME_CLEANUP_PATTERN.matcher(name).replaceAll("_");
6666
if (LEADING_NUMERIC_PATTERN.matcher(sanitized).find()) {
67-
logger.log("'" + sanitized + "' (original name: '" + name + "') is not a valid meter name. "
67+
logger.log(() -> "'" + sanitized + "' (original name: '" + name + "') is not a valid meter name. "
6868
+ "Dynatrace doesn't allow leading numeric characters after non-alphabets. "
6969
+ "Please rename it to conform to the constraints. "
7070
+ "If it comes from a third party, please use MeterFilter to rename it.");

implementations/micrometer-registry-signalfx/src/main/java/io/micrometer/signalfx/SignalFxNamingConvention.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public String tagKey(String key) {
8989
conventionKey = "a" + conventionKey;
9090
}
9191
if (PATTERN_TAG_KEY_DENYLISTED_PREFIX.matcher(conventionKey).matches()) {
92-
logger.log("'" + conventionKey + "' (original name: '" + key + "') is not a valid tag key. "
92+
String finalConventionKey = conventionKey;
93+
logger.log(() -> "'" + finalConventionKey + "' (original name: '" + key + "') is not a valid tag key. "
9394
+ "Must not start with any of these prefixes: aws_, gcp_, or azure_. "
9495
+ "Please rename it to conform to the constraints. "
9596
+ "If it comes from a third party, please use MeterFilter to rename it.");

implementations/micrometer-registry-statsd/src/main/java/io/micrometer/statsd/StatsdMeterRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void poll() {
182182
pollableMeter.getValue().poll();
183183
}
184184
catch (RuntimeException e) {
185-
warnThenDebugLogger.log("Failed to poll a meter '" + pollableMeter.getKey().getName() + "'.", e);
185+
warnThenDebugLogger.log(() -> "Failed to poll a meter '" + pollableMeter.getKey().getName() + "'.", e);
186186
}
187187
}
188188
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ else if (tags.size() == meterTagsWithCommonTags.size())
227227
catch (Exception ex) {
228228
String message = ex.getMessage();
229229
if (message != null && message.contains("Prometheus requires")) {
230-
warnThenDebugLogger.log("Failed to bind meter: " + meterName + " " + tags
230+
warnThenDebugLogger.log(() -> "Failed to bind meter: " + meterName + " " + tags
231231
+ ". However, this could happen and might be restored in the next refresh.");
232232
}
233233
else {

micrometer-core/src/main/java/io/micrometer/core/instrument/dropwizard/DropwizardMeterRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, @Nullabl
9595
return valueFunction.applyAsDouble(obj2);
9696
}
9797
catch (Throwable ex) {
98-
logger.log("Failed to apply the value function for the gauge '" + id.getName() + "'.", ex);
98+
logger.log(() -> "Failed to apply the value function for the gauge '" + id.getName() + "'.", ex);
9999
}
100100
}
101101
return nullGaugeValue();

micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultGauge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public double value() {
5353
return value.applyAsDouble(obj);
5454
}
5555
catch (Throwable ex) {
56-
logger.log("Failed to apply the value function for the gauge '" + getId().getName() + "'.", ex);
56+
logger.log(() -> "Failed to apply the value function for the gauge '" + getId().getName() + "'.", ex);
5757
}
5858
}
5959
return Double.NaN;

micrometer-core/src/main/java/io/micrometer/core/util/internal/logging/WarnThenDebugLogger.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.micrometer.core.util.internal.logging;
1717

1818
import java.util.concurrent.atomic.AtomicBoolean;
19+
import java.util.function.Supplier;
1920

2021
/**
2122
* {@link InternalLogger} which logs at warn level at first and then logs at debug level
@@ -35,16 +36,19 @@ public WarnThenDebugLogger(Class<?> clazz) {
3536
}
3637

3738
public void log(String message, Throwable ex) {
38-
InternalLogLevel level;
39-
String finalMessage;
4039
if (this.warnLogged.compareAndSet(false, true)) {
41-
level = InternalLogLevel.WARN;
42-
finalMessage = message + " Note that subsequent logs will be logged at debug level.";
40+
log(InternalLogLevel.WARN, getWarnMessage(message), ex);
4341
}
4442
else {
45-
level = InternalLogLevel.DEBUG;
46-
finalMessage = message;
43+
log(InternalLogLevel.DEBUG, message, ex);
4744
}
45+
}
46+
47+
private String getWarnMessage(String message) {
48+
return message + " Note that subsequent logs will be logged at debug level.";
49+
}
50+
51+
private void log(InternalLogLevel level, String finalMessage, Throwable ex) {
4852
if (ex != null) {
4953
this.logger.log(level, finalMessage, ex);
5054
}
@@ -57,4 +61,19 @@ public void log(String message) {
5761
log(message, null);
5862
}
5963

64+
public void log(Supplier<String> messageSupplier, Throwable ex) {
65+
if (this.warnLogged.compareAndSet(false, true)) {
66+
log(InternalLogLevel.WARN, getWarnMessage(messageSupplier.get()), ex);
67+
}
68+
else {
69+
if (this.logger.isDebugEnabled()) {
70+
log(InternalLogLevel.DEBUG, messageSupplier.get(), ex);
71+
}
72+
}
73+
}
74+
75+
public void log(Supplier<String> messageSupplier) {
76+
log(messageSupplier, null);
77+
}
78+
6079
}

0 commit comments

Comments
 (0)