diff --git a/java-util/src/main/java/org/apache/druid/java/util/emitter/core/ConcurrentTimeCounter.java b/java-util/src/main/java/org/apache/druid/java/util/emitter/core/ConcurrentTimeCounter.java index d4c5435236d..a71b2fc4ed6 100644 --- a/java-util/src/main/java/org/apache/druid/java/util/emitter/core/ConcurrentTimeCounter.java +++ b/java-util/src/main/java/org/apache/druid/java/util/emitter/core/ConcurrentTimeCounter.java @@ -21,6 +21,7 @@ package org.apache.druid.java.util.emitter.core; import com.google.common.primitives.UnsignedInts; +import javax.annotation.Nullable; import java.util.concurrent.atomic.AtomicLong; /** @@ -70,18 +71,34 @@ public class ConcurrentTimeCounter return timeSumAndCount.getAndSet(0L); } - public int getAndResetMaxTime() + /** + * Returns the max time {@link #add}ed since the previous call to this method or since the creation of this object, + * or null if no times were added. + */ + @Nullable + public Integer getAndResetMaxTime() { long max = this.max.getAndSet(-1); - // If max < 0, means no times added yet, then return 0 - return max >= 0 ? (int) max : 0; + if (max >= 0) { + return (int) max; + } else { + return null; + } } - public int getAndResetMinTime() + /** + * Returns the min time {@link #add}ed since the previous call to this method or since the creation of this object, + * or null if no times were added. + */ + @Nullable + public Integer getAndResetMinTime() { long min = this.min.getAndSet(-1); - // If min < 0, means no times added yet, then return 0 - return min >= 0 ? (int) min : 0; + if (min >= 0) { + return (int) min; + } else { + return null; + } } public static int timeSum(long timeSumAndCount) diff --git a/java-util/src/main/java/org/apache/druid/java/util/metrics/HttpPostEmitterMonitor.java b/java-util/src/main/java/org/apache/druid/java/util/metrics/HttpPostEmitterMonitor.java index de40f0ec652..a8fd802194d 100644 --- a/java-util/src/main/java/org/apache/druid/java/util/metrics/HttpPostEmitterMonitor.java +++ b/java-util/src/main/java/org/apache/druid/java/util/metrics/HttpPostEmitterMonitor.java @@ -75,10 +75,20 @@ public class HttpPostEmitterMonitor extends FeedDefiningMonitor private void emitTimeCounterMetrics(ServiceEmitter emitter, ConcurrentTimeCounter timeCounter, String metricNameBase) { long timeSumAndCount = timeCounter.getTimeSumAndCountAndReset(); - emitter.emit(builder.build(metricNameBase + "timeMsSum", ConcurrentTimeCounter.timeSum(timeSumAndCount))); - emitter.emit(builder.build(metricNameBase + "count", ConcurrentTimeCounter.count(timeSumAndCount))); - emitter.emit(builder.build(metricNameBase + "maxTimeMs", timeCounter.getAndResetMaxTime())); - emitter.emit(builder.build(metricNameBase + "minTimeMs", timeCounter.getAndResetMinTime())); + int timeSum = ConcurrentTimeCounter.timeSum(timeSumAndCount); + int count = ConcurrentTimeCounter.count(timeSumAndCount); + if (count != 0) { + emitter.emit(builder.build(metricNameBase + "timeMsSum", timeSum)); + emitter.emit(builder.build(metricNameBase + "count", count)); + } + Integer maxTime = timeCounter.getAndResetMaxTime(); + if (maxTime != null) { + emitter.emit(builder.build(metricNameBase + "maxTimeMs", maxTime)); + } + Integer minTime = timeCounter.getAndResetMinTime(); + if (minTime != null) { + emitter.emit(builder.build(metricNameBase + "minTimeMs", minTime)); + } } @Override