Skip to content

Commit 9b35aa2

Browse files
committed
add testcase to reproduce issue
1 parent 2279e00 commit 9b35aa2

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/MetricAggregationTest.java

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
import java.util.Collections;
1818
import java.util.List;
1919
import javax.annotation.Nullable;
20+
import javax.management.InstanceNotFoundException;
21+
import javax.management.MBeanRegistrationException;
2022
import javax.management.MBeanServer;
2123
import javax.management.MBeanServerFactory;
2224
import javax.management.MalformedObjectNameException;
2325
import javax.management.ObjectName;
26+
import org.assertj.core.api.Assertions;
2427
import org.junit.jupiter.api.AfterAll;
2528
import org.junit.jupiter.api.AfterEach;
2629
import org.junit.jupiter.api.BeforeAll;
@@ -72,7 +75,19 @@ void before() {
7275
}
7376

7477
@AfterEach
75-
void after() {
78+
void after() throws Exception {
79+
ObjectName objectName = new ObjectName(
80+
"otel.jmx.test:type=" + Hello.class.getSimpleName() + ",*");
81+
theServer.queryMBeans(objectName, null).forEach(
82+
instance -> {
83+
try {
84+
theServer.unregisterMBean(instance.getObjectName());
85+
} catch (InstanceNotFoundException | MBeanRegistrationException e) {
86+
throw new RuntimeException(e);
87+
}
88+
}
89+
);
90+
7691
if (sdk != null) {
7792
sdk.getSdkMeterProvider().close();
7893
}
@@ -102,42 +117,76 @@ void singleInstance() throws Exception {
102117
ObjectName bean = getObjectName(null, null);
103118
theServer.registerMBean(new Hello(42), bean);
104119

105-
Collection<MetricData> data = testMetric(bean.toString());
120+
Collection<MetricData> data = testMetric(bean.toString(), Collections.emptyList());
106121
checkSingleValue(data, 42);
107122
}
108123

109124
@Test
110-
void multipleInstancesAggregated_twoInstances() throws Exception {
125+
void aggregateOneParam() throws Exception {
111126
theServer.registerMBean(new Hello(42), getObjectName("value1", null));
112127
theServer.registerMBean(new Hello(37), getObjectName("value2", null));
113128

114129
String bean = getObjectName("*", null).toString();
115-
Collection<MetricData> data = testMetric(bean);
130+
Collection<MetricData> data = testMetric(bean, Collections.emptyList());
116131
checkSingleValue(data, 79);
117132
}
118133

119134
@Test
120-
void multipleInstancesAggregated_fourInstances() throws Exception {
121-
theServer.registerMBean(new Hello(1), getObjectName("1", "a"));
122-
theServer.registerMBean(new Hello(2), getObjectName("2", "b"));
123-
theServer.registerMBean(new Hello(3), getObjectName("3", "a"));
124-
theServer.registerMBean(new Hello(5), getObjectName("4", "b"));
135+
void aggregateMultipleParams() throws Exception {
136+
theServer.registerMBean(new Hello(1), getObjectName("1", "x"));
137+
theServer.registerMBean(new Hello(2), getObjectName("2", "y"));
138+
theServer.registerMBean(new Hello(3), getObjectName("3", "x"));
139+
theServer.registerMBean(new Hello(4), getObjectName("4", "y"));
140+
141+
String bean = getObjectName("*", "*").toString();
142+
Collection<MetricData> data = testMetric(bean, Collections.emptyList());
143+
checkSingleValue(data, 10);
144+
}
145+
146+
@Test
147+
void partialAggregateMultipleParams() throws Exception {
148+
theServer.registerMBean(new Hello(1), getObjectName("1", "x"));
149+
theServer.registerMBean(new Hello(2), getObjectName("2", "y"));
150+
theServer.registerMBean(new Hello(3), getObjectName("3", "x"));
151+
theServer.registerMBean(new Hello(4), getObjectName("4", "y"));
125152

126153
String bean = getObjectName("*", "*").toString();
127-
Collection<MetricData> data = testMetric(bean);
128-
checkSingleValue(data, 11);
154+
155+
List<MetricAttribute> attributes = Collections.singletonList(
156+
new MetricAttribute("test.metric.param",
157+
MetricAttributeExtractor.fromObjectNameParameter("b")));
158+
Collection<MetricData> data = testMetric(bean, attributes);
159+
160+
assertThat(data)
161+
.isNotEmpty()
162+
.satisfiesExactlyInAnyOrder(
163+
metric -> {
164+
assertThat(metric.getName()).isEqualTo("test.metric");
165+
Collection<LongPointData> points = metric.getLongSumData().getPoints();
166+
assertThat(points).hasSize(1);
167+
LongPointData pointData = points.stream().findFirst().get();
168+
assertThat(pointData.getAttributes()).containsEntry("b", "y");
169+
assertThat(pointData.getValue()).isEqualTo(6);
170+
}, metric -> {
171+
Assertions.assertThat(metric.getName()).isEqualTo("test.metric");
172+
Collection<LongPointData> points = metric.getLongSumData().getPoints();
173+
assertThat(points).hasSize(1);
174+
LongPointData pointData = points.stream().findFirst().get();
175+
assertThat(pointData.getAttributes()).containsEntry("b", "x");
176+
assertThat(pointData.getValue()).isEqualTo(4);
177+
});
129178
}
130179

131-
private Collection<MetricData> testMetric(String mbean) throws MalformedObjectNameException {
180+
private Collection<MetricData> testMetric(String mbean, List<MetricAttribute> attributes)
181+
throws MalformedObjectNameException {
132182
JmxMetricInsight metricInsight = JmxMetricInsight.createService(sdk, 0);
133183
MetricConfiguration metricConfiguration = new MetricConfiguration();
134184
List<MetricExtractor> extractors = new ArrayList<>();
135185

136186
MetricInfo metricInfo =
137187
new MetricInfo("test.metric", "description", null, "1", MetricInfo.Type.COUNTER);
138188
BeanAttributeExtractor beanExtractor = BeanAttributeExtractor.fromName("Value");
139-
MetricExtractor extractor =
140-
new MetricExtractor(beanExtractor, metricInfo, Collections.emptyList());
189+
MetricExtractor extractor = new MetricExtractor(beanExtractor, metricInfo, attributes);
141190
extractors.add(extractor);
142191
MetricDef metricDef =
143192
new MetricDef(BeanGroup.forBeans(Collections.singletonList(mbean)), extractors);

0 commit comments

Comments
 (0)