Skip to content

Commit acd9ed3

Browse files
thonrashakuzen
andauthored
Enhance logging of negative amount in AbstractTimer.record() by printing stack trace (#6530)
In order to help identify the code that is resulting in calling record with a negative amount, include the stacktrace in the log. Nothing will be logged after the first time if debug level logging is not enabled. We can avoid the cost of instantiating the exception in that case. --------- Signed-off-by: thonra <[email protected]> Co-authored-by: Tommy Ludwig <[email protected]>
1 parent ac5ede2 commit acd9ed3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public WarnThenDebugLogger(String name) {
4646
this.logger = InternalLoggerFactory.getInstance(name);
4747
}
4848

49+
/**
50+
* This may be useful to have different behavior before/after logging once.
51+
* @return whether the warn-level log has been made or not
52+
* @since 1.16.0
53+
*/
54+
public boolean isWarnLogged() {
55+
return warnLogged.get();
56+
}
57+
4958
public void log(String message, @Nullable Throwable ex) {
5059
if (this.warnLogged.compareAndSet(false, true)) {
5160
log(InternalLogLevel.WARN, getWarnMessage(message), ex);

micrometer-core/src/main/java/io/micrometer/core/instrument/AbstractTimer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.micrometer.core.instrument;
1717

18+
import io.micrometer.common.util.internal.logging.InternalLogger;
19+
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
1820
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger;
1921
import io.micrometer.core.instrument.distribution.*;
2022
import io.micrometer.core.instrument.distribution.pause.ClockDriftPauseDetector;
@@ -37,7 +39,9 @@
3739

3840
public abstract class AbstractTimer extends AbstractMeter implements Timer {
3941

40-
private static final WarnThenDebugLogger log = new WarnThenDebugLogger(AbstractTimer.class);
42+
private static final WarnThenDebugLogger warnThenDebugLogger = new WarnThenDebugLogger(AbstractTimer.class);
43+
44+
private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(AbstractTimer.class);
4145

4246
private static final Map<PauseDetector, Object> pauseDetectorCache = new ConcurrentHashMap<>();
4347

@@ -264,7 +268,10 @@ public final void record(long amount, TimeUnit unit) {
264268
}
265269
}
266270
else {
267-
log.log(() -> "'amount' should not be negative but was: " + amount);
271+
if (!warnThenDebugLogger.isWarnLogged() || LOGGER.isDebugEnabled()) {
272+
warnThenDebugLogger.log(() -> "'amount' should not be negative but was: " + amount,
273+
new IllegalArgumentException("Timer measurements cannot be negative"));
274+
}
268275
}
269276
}
270277

0 commit comments

Comments
 (0)