Skip to content

Commit

Permalink
Adapt WarnThenDebugLogger for 1.10
Browse files Browse the repository at this point in the history
WarnThenDebugLogger is copied into the common module. Changes to it need to be copied over there. Additional places that the supplier variant can be used are updated.
  • Loading branch information
shakuzen committed Sep 22, 2022
2 parents 2e1fb6c + 01734a5 commit a10b1f3
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ private List<Dimension> toDimensions(List<Tag> tags) {

private boolean isAcceptableTag(Tag tag) {
if (StringUtils.isBlank(tag.getValue())) {
warnThenDebugLogger.log("Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
warnThenDebugLogger
.log(() -> "Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private MetricDatum metricDatum(Meter.Id id, @Nullable String suffix, StandardUn

List<Tag> tags = id.getConventionTags(config().namingConvention());
if (tags.size() > MAX_DIMENSIONS_SIZE) {
tooManyTagsLogger.log("Meter " + id.getName() + " has more tags (" + tags.size()
tooManyTagsLogger.log(() -> "Meter " + id.getName() + " has more tags (" + tags.size()
+ ") than the max supported by CloudWatch (" + MAX_DIMENSIONS_SIZE
+ "). Some tags will be dropped.");
}
Expand Down Expand Up @@ -306,7 +306,8 @@ private List<Dimension> toDimensions(List<Tag> tags) {

private boolean isAcceptableTag(Tag tag) {
if (StringUtils.isBlank(tag.getValue())) {
blankTagValueLogger.log("Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
blankTagValueLogger
.log(() -> "Dropping a tag with key '" + tag.getKey() + "' because its value is blank.");
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String sanitizeName(String name) {
}
String sanitized = NAME_CLEANUP_PATTERN.matcher(name).replaceAll("_");
if (LEADING_NUMERIC_PATTERN.matcher(sanitized).find()) {
logger.log("'" + sanitized + "' (original name: '" + name + "') is not a valid meter name. "
logger.log(() -> "'" + sanitized + "' (original name: '" + name + "') is not a valid meter name. "
+ "Dynatrace doesn't allow leading numeric characters after non-alphabets. "
+ "Please rename it to conform to the constraints. "
+ "If it comes from a third party, please use MeterFilter to rename it.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public String tagKey(String key) {
conventionKey = "a" + conventionKey;
}
if (PATTERN_TAG_KEY_DENYLISTED_PREFIX.matcher(conventionKey).matches()) {
logger.log("'" + conventionKey + "' (original name: '" + key + "') is not a valid tag key. "
String finalConventionKey = conventionKey;
logger.log(() -> "'" + finalConventionKey + "' (original name: '" + key + "') is not a valid tag key. "
+ "Must not start with any of these prefixes: aws_, gcp_, or azure_. "
+ "Please rename it to conform to the constraints. "
+ "If it comes from a third party, please use MeterFilter to rename it.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void poll() {
pollableMeter.getValue().poll();
}
catch (RuntimeException e) {
warnThenDebugLogger.log("Failed to poll a meter '" + pollableMeter.getKey().getName() + "'.", e);
warnThenDebugLogger.log(() -> "Failed to poll a meter '" + pollableMeter.getKey().getName() + "'.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.common.util.internal.logging;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

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

public void log(String message, Throwable ex) {
InternalLogLevel level;
String finalMessage;
if (this.warnLogged.compareAndSet(false, true)) {
level = InternalLogLevel.WARN;
finalMessage = message + " Note that subsequent logs will be logged at debug level.";
log(InternalLogLevel.WARN, getWarnMessage(message), ex);
}
else {
level = InternalLogLevel.DEBUG;
finalMessage = message;
log(InternalLogLevel.DEBUG, message, ex);
}
}

private String getWarnMessage(String message) {
return message + " Note that subsequent logs will be logged at debug level.";
}

private void log(InternalLogLevel level, String finalMessage, Throwable ex) {
if (ex != null) {
this.logger.log(level, finalMessage, ex);
}
Expand All @@ -57,4 +61,19 @@ public void log(String message) {
log(message, null);
}

public void log(Supplier<String> messageSupplier, Throwable ex) {
if (this.warnLogged.compareAndSet(false, true)) {
log(InternalLogLevel.WARN, getWarnMessage(messageSupplier.get()), ex);
}
else {
if (this.logger.isDebugEnabled()) {
log(InternalLogLevel.DEBUG, messageSupplier.get(), ex);
}
}
}

public void log(Supplier<String> messageSupplier) {
log(messageSupplier, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Optional<String> findFirst() {
}

private void logWarning(String name) {
WARN_THEN_DEBUG_LOGGER.log(String.format("It seems %s has high cardinality tags (threshold: %d meters).\n"
WARN_THEN_DEBUG_LOGGER.log(() -> String.format("It seems %s has high cardinality tags (threshold: %d meters).\n"
+ "Check your configuration for the instrumentation of %s to find and fix the cause of the high cardinality (see: https://micrometer.io/docs/concepts#_tag_values).\n"
+ "If the cardinality is expected and acceptable, raise the threshold for this %s.", name,
this.threshold, name, getClass().getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ else if (tags.size() == meterTagsWithCommonTags.size())
catch (Exception ex) {
String message = ex.getMessage();
if (message != null && message.contains("Prometheus requires")) {
warnThenDebugLogger.log("Failed to bind meter: " + meterName + " " + tags
warnThenDebugLogger.log(() -> "Failed to bind meter: " + meterName + " " + tags
+ ". However, this could happen and might be restored in the next refresh.");
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, @Nullabl
return valueFunction.applyAsDouble(obj2);
}
catch (Throwable ex) {
logger.log("Failed to apply the value function for the gauge '" + id.getName() + "'.", ex);
logger.log(() -> "Failed to apply the value function for the gauge '" + id.getName() + "'.", ex);
}
}
return nullGaugeValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public double value() {
return value.applyAsDouble(obj);
}
catch (Throwable ex) {
logger.log("Failed to apply the value function for the gauge '" + getId().getName() + "'.", ex);
logger.log(() -> "Failed to apply the value function for the gauge '" + getId().getName() + "'.", ex);
}
}
return Double.NaN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.util.internal.logging;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

/**
* {@link InternalLogger} which logs at warn level at first and then logs at debug level
Expand All @@ -38,16 +39,19 @@ public WarnThenDebugLogger(Class<?> clazz) {
}

public void log(String message, Throwable ex) {
InternalLogLevel level;
String finalMessage;
if (this.warnLogged.compareAndSet(false, true)) {
level = InternalLogLevel.WARN;
finalMessage = message + " Note that subsequent logs will be logged at debug level.";
log(InternalLogLevel.WARN, getWarnMessage(message), ex);
}
else {
level = InternalLogLevel.DEBUG;
finalMessage = message;
log(InternalLogLevel.DEBUG, message, ex);
}
}

private String getWarnMessage(String message) {
return message + " Note that subsequent logs will be logged at debug level.";
}

private void log(InternalLogLevel level, String finalMessage, Throwable ex) {
if (ex != null) {
this.logger.log(level, finalMessage, ex);
}
Expand All @@ -60,4 +64,19 @@ public void log(String message) {
log(message, null);
}

public void log(Supplier<String> messageSupplier, Throwable ex) {
if (this.warnLogged.compareAndSet(false, true)) {
log(InternalLogLevel.WARN, getWarnMessage(messageSupplier.get()), ex);
}
else {
if (this.logger.isDebugEnabled()) {
log(InternalLogLevel.DEBUG, messageSupplier.get(), ex);
}
}
}

public void log(Supplier<String> messageSupplier) {
log(messageSupplier, null);
}

}

0 comments on commit a10b1f3

Please sign in to comment.