summaryrefslogtreecommitdiffstats
path: root/simplemetrics
diff options
context:
space:
mode:
authorArne Juul <arnej@yahoo-inc.com>2019-05-16 09:10:18 +0000
committerArne Juul <arnej@yahoo-inc.com>2019-05-16 09:20:36 +0000
commit9688d52721ac2e9244ef9c1ac8a5cf8fb99e45b2 (patch)
tree6a00e2144b33b68a17dc246dc36b44ae3db3567f /simplemetrics
parent8c88782c16e527ab79a6b39857a96db96f209211 (diff)
fix merge bug
* avoid using empty metric's value as input to max() and min()
Diffstat (limited to 'simplemetrics')
-rw-r--r--simplemetrics/src/main/java/com/yahoo/metrics/simple/UntypedMetric.java16
-rw-r--r--simplemetrics/src/test/java/com/yahoo/metrics/simple/BucketTest.java29
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;