Skip to content

Commit 9da8381

Browse files
committed
Update contract tests for runtime
1 parent 52e7da1 commit 9da8381

File tree

3 files changed

+95
-22
lines changed

3 files changed

+95
-22
lines changed

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/misc/RuntimeMetricsTest.java

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20-
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
20+
import io.opentelemetry.proto.metrics.v1.Metric;
2121
import java.util.List;
2222
import java.util.Set;
23-
import org.junit.Assert;
2423
import org.junit.jupiter.api.Nested;
2524
import org.junit.jupiter.api.Test;
2625
import org.junit.jupiter.api.TestInstance;
2726
import org.testcontainers.junit.jupiter.Testcontainers;
2827
import software.amazon.opentelemetry.appsignals.test.base.ContractTestBase;
2928
import software.amazon.opentelemetry.appsignals.test.utils.AppSignalsConstants;
29+
import software.amazon.opentelemetry.appsignals.test.utils.ResourceScopeMetric;
3030

3131
public class RuntimeMetricsTest {
3232
private abstract static class RuntimeMetricsContractTestBase extends ContractTestBase {
@@ -55,7 +55,7 @@ protected void assertRuntimeMetrics() {
5555
var metrics =
5656
mockCollectorClient.getRuntimeMetrics(
5757
Set.of(
58-
AppSignalsConstants.JVM_GC_METRIC,
58+
AppSignalsConstants.JVM_GC_DURATION,
5959
AppSignalsConstants.JVM_GC_COUNT,
6060
AppSignalsConstants.JVM_HEAP_USED,
6161
AppSignalsConstants.JVM_NON_HEAP_USED,
@@ -68,18 +68,88 @@ protected void assertRuntimeMetrics() {
6868
AppSignalsConstants.LATENCY_METRIC,
6969
AppSignalsConstants.ERROR_METRIC,
7070
AppSignalsConstants.FAULT_METRIC));
71-
metrics.forEach(
72-
metric -> {
73-
var dataPoints = metric.getMetric().getGauge().getDataPointsList();
74-
assertNonNegativeValue(dataPoints);
75-
});
71+
72+
testResourceAttributes(metrics);
73+
for (String metricName : List.of(AppSignalsConstants.JVM_POOL_USED)) {
74+
testGaugeMetrics(metrics, metricName, "name");
75+
}
76+
for (String metricName :
77+
List.of(
78+
AppSignalsConstants.JVM_HEAP_USED,
79+
AppSignalsConstants.JVM_NON_HEAP_USED,
80+
AppSignalsConstants.JVM_AFTER_GC,
81+
AppSignalsConstants.JVM_THREAD_COUNT,
82+
AppSignalsConstants.JVM_CLASS_LOADED,
83+
AppSignalsConstants.JVM_CPU_UTILIZATION)) {
84+
testGaugeMetrics(metrics, metricName, "");
85+
}
86+
for (String metricName :
87+
List.of(AppSignalsConstants.JVM_GC_DURATION, AppSignalsConstants.JVM_GC_COUNT)) {
88+
testCounterMetrics(metrics, metricName, "name");
89+
}
90+
for (String metricName : List.of(AppSignalsConstants.JVM_CPU_TIME)) {
91+
testCounterMetrics(metrics, metricName, "");
92+
}
93+
}
94+
95+
private void testGaugeMetrics(
96+
List<ResourceScopeMetric> resourceScopeMetrics, String metricName, String attributeKey) {
97+
for (ResourceScopeMetric rsm : resourceScopeMetrics) {
98+
Metric metric = rsm.getMetric();
99+
if (metricName.equals(metric.getName())) {
100+
assertThat(metric.getGauge().getDataPointsList())
101+
.as(metricName + " is not empty")
102+
.isNotEmpty();
103+
assertThat(metric.getGauge().getDataPointsList())
104+
.as(metricName + " is valid")
105+
.allMatch(
106+
dp -> {
107+
boolean valid = true;
108+
if (!attributeKey.isEmpty()) {
109+
valid =
110+
dp.getAttributesList().stream()
111+
.anyMatch(attribute -> attribute.getKey().equals(attributeKey));
112+
}
113+
return valid && dp.getAsInt() >= 0;
114+
});
115+
}
116+
}
117+
}
118+
119+
private void testCounterMetrics(
120+
List<ResourceScopeMetric> resourceScopeMetrics, String metricName, String attributeKey) {
121+
for (ResourceScopeMetric rsm : resourceScopeMetrics) {
122+
Metric metric = rsm.getMetric();
123+
if (metricName.equals(metric.getName())) {
124+
assertThat(metric.getSum().getDataPointsList())
125+
.as(metricName + " is not empty")
126+
.isNotEmpty();
127+
assertThat(metric.getSum().getDataPointsList())
128+
.as(metricName + " is valid")
129+
.allMatch(
130+
dp -> {
131+
boolean valid = true;
132+
if (!attributeKey.isEmpty()) {
133+
valid =
134+
dp.getAttributesList().stream()
135+
.anyMatch(attribute -> attribute.getKey().equals(attributeKey));
136+
}
137+
return valid && dp.getAsInt() >= 0;
138+
});
139+
}
140+
}
76141
}
77142

78-
private void assertNonNegativeValue(List<NumberDataPoint> dps) {
79-
dps.forEach(
80-
datapoint -> {
81-
Assert.assertTrue(datapoint.getAsInt() >= 0);
82-
});
143+
private void testResourceAttributes(List<ResourceScopeMetric> resourceScopeMetrics) {
144+
for (ResourceScopeMetric rsm : resourceScopeMetrics) {
145+
assertThat(rsm.getResource().getResource().getAttributesList())
146+
.anyMatch(
147+
attr ->
148+
attr.getKey().equals(AppSignalsConstants.AWS_LOCAL_SERVICE)
149+
&& attr.getValue()
150+
.getStringValue()
151+
.equals(getApplicationOtelServiceName()));
152+
}
83153
}
84154
}
85155

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/utils/AppSignalsConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class AppSignalsConstants {
3535
public static final String AWS_REMOTE_DB_USER = "aws.remote.db.user";
3636

3737
// JVM Metrics
38-
public static final String JVM_GC_METRIC = "jvm.gc.collections.elapsed";
38+
public static final String JVM_GC_DURATION = "jvm.gc.collections.elapsed";
3939
public static final String JVM_GC_COUNT = "jvm.gc.collections.count";
4040
public static final String JVM_HEAP_USED = "jvm.memory.heap.used";
4141
public static final String JVM_NON_HEAP_USED = "jvm.memory.nonheap.used";

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/utils/MockCollectorClient.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ public List<ResourceScopeSpan> getTraces() {
134134
}
135135

136136
public List<ResourceScopeMetric> getRuntimeMetrics(Set<String> presentMetrics) {
137-
return fetchMetrics(presentMetrics, true);
137+
return fetchMetrics(presentMetrics, false);
138138
}
139139

140140
public List<ResourceScopeMetric> getMetrics(Set<String> presentMetrics) {
141-
return fetchMetrics(presentMetrics, false);
141+
return fetchMetrics(presentMetrics, true);
142142
}
143143

144144
/**
@@ -147,7 +147,7 @@ public List<ResourceScopeMetric> getMetrics(Set<String> presentMetrics) {
147147
* @return List of `ResourceScopeMetric` which is a flat list containing all metrics and their
148148
* related scope and resources.
149149
*/
150-
private List<ResourceScopeMetric> fetchMetrics(Set<String> presentMetrics, boolean isRuntime) {
150+
private List<ResourceScopeMetric> fetchMetrics(Set<String> presentMetrics, boolean exactMatch) {
151151
List<ExportMetricsServiceRequest> exportedMetrics =
152152
waitForContent(
153153
"/get-metrics",
@@ -160,11 +160,14 @@ private List<ResourceScopeMetric> fetchMetrics(Set<String> presentMetrics, boole
160160
.flatMap(x -> x.getMetricsList().stream())
161161
.map(x -> x.getName())
162162
.collect(Collectors.toSet());
163-
164-
return (isRuntime
165-
? !exported.isEmpty() && receivedMetrics.size() == presentMetrics.size()
166-
: !exported.isEmpty() && current.size() == exported.size())
167-
&& receivedMetrics.containsAll(presentMetrics);
163+
if (!exported.isEmpty() && receivedMetrics.containsAll(presentMetrics)) {
164+
if (exactMatch) {
165+
return current.size() == exported.size();
166+
} else {
167+
return true;
168+
}
169+
}
170+
return false;
168171
});
169172

170173
return exportedMetrics.stream()

0 commit comments

Comments
 (0)