Skip to content

Commit eaf2377

Browse files
authored
Fix types when combining measurements (#113686)
1 parent 2acb880 commit eaf2377

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

test/framework/src/main/java/org/elasticsearch/telemetry/Measurement.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public static List<Measurement> combine(List<Measurement> measurements) {
5252
if (m.isDouble != isDouble) {
5353
throw new IllegalArgumentException("cannot combine measurements of different types");
5454
}
55-
byAttr.compute(
56-
m.attributes,
57-
(k, v) -> (v == null) ? m.value : isDouble ? v.doubleValue() + m.getDouble() : v.longValue() + m.getLong()
58-
);
55+
byAttr.compute(m.attributes, (k, v) -> {
56+
if (v == null) return m.value;
57+
if (isDouble) {
58+
return v.doubleValue() + m.getDouble();
59+
} else {
60+
return v.longValue() + m.getLong();
61+
}
62+
});
5963
});
6064
return byAttr.entrySet()
6165
.stream()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.telemetry;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
14+
import java.util.Collections;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import static org.hamcrest.Matchers.containsInAnyOrder;
19+
20+
public class MeasurementTests extends ESTestCase {
21+
22+
public void testCombineLongs() {
23+
Measurement m1 = new Measurement(1L, Collections.emptyMap(), false);
24+
Measurement m2 = new Measurement(2L, Collections.emptyMap(), false);
25+
Measurement m3 = new Measurement(3L, Map.of("k", "v"), false);
26+
Measurement m4 = new Measurement(4L, Map.of("k", "v"), false);
27+
28+
List<Measurement> combined = Measurement.combine(List.of(m1, m2, m3, m4));
29+
assertThat(
30+
combined,
31+
containsInAnyOrder(new Measurement(3L, Collections.emptyMap(), false), new Measurement(7L, Map.of("k", "v"), false))
32+
);
33+
}
34+
35+
public void testCombineDoubles() {
36+
Measurement m1 = new Measurement(1.0, Collections.emptyMap(), true);
37+
Measurement m2 = new Measurement(2.0, Collections.emptyMap(), true);
38+
Measurement m3 = new Measurement(3.0, Map.of("k", "v"), true);
39+
Measurement m4 = new Measurement(4.0, Map.of("k", "v"), true);
40+
41+
List<Measurement> combined = Measurement.combine(List.of(m1, m2, m3, m4));
42+
assertThat(
43+
combined,
44+
containsInAnyOrder(new Measurement(3.0, Collections.emptyMap(), true), new Measurement(7.0, Map.of("k", "v"), true))
45+
);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)