Skip to content

Commit 21dda9d

Browse files
committed
test for single metric attribute aggregation
1 parent 9b35aa2 commit 21dda9d

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

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

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
package io.opentelemetry.instrumentation.jmx.engine;
77

88
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
9+
import static org.assertj.core.groups.Tuple.tuple;
910

11+
import io.opentelemetry.api.common.AttributeKey;
12+
import io.opentelemetry.api.common.Attributes;
1013
import io.opentelemetry.sdk.OpenTelemetrySdk;
1114
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
1215
import io.opentelemetry.sdk.metrics.data.LongPointData;
1316
import io.opentelemetry.sdk.metrics.data.MetricData;
17+
import io.opentelemetry.sdk.metrics.data.PointData;
1418
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
1519
import java.util.ArrayList;
1620
import java.util.Collection;
@@ -23,7 +27,6 @@
2327
import javax.management.MBeanServerFactory;
2428
import javax.management.MalformedObjectNameException;
2529
import javax.management.ObjectName;
26-
import org.assertj.core.api.Assertions;
2730
import org.junit.jupiter.api.AfterAll;
2831
import org.junit.jupiter.api.AfterEach;
2932
import org.junit.jupiter.api.BeforeAll;
@@ -76,17 +79,18 @@ void before() {
7679

7780
@AfterEach
7881
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-
);
82+
ObjectName objectName =
83+
new ObjectName("otel.jmx.test:type=" + Hello.class.getSimpleName() + ",*");
84+
theServer
85+
.queryMBeans(objectName, null)
86+
.forEach(
87+
instance -> {
88+
try {
89+
theServer.unregisterMBean(instance.getObjectName());
90+
} catch (InstanceNotFoundException | MBeanRegistrationException e) {
91+
throw new RuntimeException(e);
92+
}
93+
});
9094

9195
if (sdk != null) {
9296
sdk.getSdkMeterProvider().close();
@@ -152,28 +156,23 @@ void partialAggregateMultipleParams() throws Exception {
152156

153157
String bean = getObjectName("*", "*").toString();
154158

155-
List<MetricAttribute> attributes = Collections.singletonList(
156-
new MetricAttribute("test.metric.param",
157-
MetricAttributeExtractor.fromObjectNameParameter("b")));
159+
List<MetricAttribute> attributes =
160+
Collections.singletonList(
161+
new MetricAttribute(
162+
"test.metric.param", MetricAttributeExtractor.fromObjectNameParameter("b")));
158163
Collection<MetricData> data = testMetric(bean, attributes);
159164

160165
assertThat(data)
161166
.isNotEmpty()
162-
.satisfiesExactlyInAnyOrder(
167+
.satisfiesExactly(
163168
metric -> {
164169
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);
170+
AttributeKey<String> metricAttribute = AttributeKey.stringKey("test.metric.param");
171+
assertThat(metric.getLongSumData().getPoints())
172+
.extracting(LongPointData::getValue, PointData::getAttributes)
173+
.containsExactlyInAnyOrder(
174+
tuple(6L, Attributes.of(metricAttribute, "y")),
175+
tuple(4L, Attributes.of(metricAttribute, "x")));
177176
});
178177
}
179178

@@ -197,17 +196,16 @@ private Collection<MetricData> testMetric(String mbean, List<MetricAttribute> at
197196
return waitMetricsReceived();
198197
}
199198

200-
private static void checkSingleValue(Collection<MetricData> data, int expectedValue) {
199+
private static void checkSingleValue(Collection<MetricData> data, long expectedValue) {
201200
assertThat(data)
202201
.isNotEmpty()
203202
.satisfiesExactlyInAnyOrder(
204203
metric -> {
205204
assertThat(metric.getName()).isEqualTo("test.metric");
206-
Collection<LongPointData> points = metric.getLongSumData().getPoints();
207-
assertThat(points).hasSize(1);
208-
LongPointData pointData = points.stream().findFirst().get();
209-
assertThat(pointData.getValue()).isEqualTo(expectedValue);
210-
assertThat(pointData.getAttributes()).isEmpty();
205+
assertThat(metric.getLongSumData().getPoints())
206+
.extracting(LongPointData::getValue, PointData::getAttributes)
207+
.containsExactlyInAnyOrder(
208+
tuple(expectedValue, Attributes.empty()));
211209
});
212210
}
213211

0 commit comments

Comments
 (0)