diff options
-rw-r--r-- | simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java | 16 | ||||
-rw-r--r-- | simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java | 29 |
2 files changed, 41 insertions, 4 deletions
diff --git a/simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java b/simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java index 3a9f71b932c..f6762434275 100644 --- a/simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java +++ b/simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java @@ -74,12 +74,20 @@ public class UntypedMetric { if (outputFormat != other.outputFormat) { throw new IllegalArgumentException("Mismatching output formats: " + outputFormat + " and " + other.outputFormat + "."); } - count += other.count; - if (otherIsNewer) { + if (count > 0) { + if (other.count > 0) { + max = Math.max(other.max, max); + min = Math.min(other.min, min); + if (otherIsNewer) { + current = other.current; + } + } + } else { + max = other.max; + min = other.min; current = other.current; } - max = Math.max(other.max, max); - min = Math.min(other.min, min); + count += other.count; sum += other.sum; if (histogram != null) { // some config scenarios may lead to differing histogram settings, diff --git a/simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java b/simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java index 927fea9ea0a..b33da4bd531 100644 --- a/simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java +++ b/simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java @@ -137,6 +137,35 @@ public class BucketTest { assertEquals(3, entry.getValue().getCount()); } + @Test + public final void testMergeDifferentMetrics() { + bucket.put(new Sample(new Measurement(2), new Identifier("nalle", null), AssumedType.GAUGE)); + Bucket otherNew = new Bucket(); + otherNew.put(new Sample(new Measurement(3), new Identifier("other", null), AssumedType.GAUGE)); + bucket.merge(otherNew, true); + Set<Entry<Identifier, UntypedMetric>> entries = bucket.entrySet(); + assertEquals(2, entries.size()); + + Collection<Map.Entry<Point, UntypedMetric>> nalle_values = bucket.getValuesForMetric("nalle"); + assertEquals(1, nalle_values.size()); + Collection<Map.Entry<Point, UntypedMetric>> other_values = bucket.getValuesForMetric("other"); + assertEquals(1, other_values.size()); + + UntypedMetric nalle_v = nalle_values.iterator().next().getValue(); + assertEquals(1, nalle_v.getCount()); + assertEquals(2, nalle_v.getSum(), 0.0); + assertEquals(2, nalle_v.getLast(), 0.0); + assertEquals(2, nalle_v.getMin(), 0.0); + assertEquals(2, nalle_v.getMax(), 0.0); + + UntypedMetric other_v = other_values.iterator().next().getValue(); + assertEquals(1, other_v.getCount()); + assertEquals(3, other_v.getSum(), 0.0); + assertEquals(3, other_v.getLast(), 0.0); + assertEquals(3, other_v.getMax(), 0.0); + assertEquals(3, other_v.getMin(), 0.0); + } + private static class CheckThatItWasLogged extends Handler { final boolean[] loggingMarker; |