Skip to content

Commit dc06079

Browse files
committed
Add and fix tests
1 parent 013bb74 commit dc06079

File tree

9 files changed

+88
-76
lines changed

9 files changed

+88
-76
lines changed

example-definitions/json/metric-definition.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"aggregationTemporality": "delta",
1919
"valueFunction": "arithmeticSequence(1, 0, \"\")",
2020
"isMonotonic": true,
21-
"reportingResources": [
22-
"pod"
23-
]
21+
"filteredReportingResources": {
22+
"pod": ["k8s.cluster.name=cluster-skyrim"]
23+
}
2424
},
2525
{
2626
"name": "cpu.allocatable",
@@ -47,9 +47,9 @@
4747
0,
4848
1
4949
],
50-
"reportingResources": [
51-
"pod"
52-
]
50+
"filteredReportingResources": {
51+
"pod": ["k8s.pod.status=Running", "k8s.cluster.name=cluster-skyrim"]
52+
}
5353
},
5454
{
5555
"name": "system.filesystem.mountpoint.limit",
@@ -117,9 +117,11 @@
117117
1
118118
],
119119
"reportingResources": [
120-
"managed_replicaset",
121-
"unmanaged_replicaset"
122-
]
120+
"managed_replicaset"
121+
],
122+
"filteredReportingResources": {
123+
"unmanaged_replicaset": ["k8s.workload.kind=unmanaged_replicaset"]
124+
}
123125
},
124126
{
125127
"name": "calls_min",

example-definitions/json/resource-definition.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"node": "distribution(5, 2, 10)"
121121
},
122122
"attributes": {
123-
"k8s.cluster.name": "\"cluster-\".concat(alphanumericSequence(\"dppteam\"))"
123+
"k8s.cluster.name": "\"cluster-\".concat(alphanumericSequence(\"skyrim\"))"
124124
}
125125
},
126126
{
@@ -296,7 +296,7 @@
296296
"name": "network",
297297
"count": 120,
298298
"attributes": {
299-
"clou.network.id": "alphanumericSequence(\"IDfr3x\").concat(\".network\")"
299+
"cloud.network.id": "alphanumericSequence(\"IDfr3x\").concat(\".network\")"
300300
},
301301
"attributeOperations": [
302302
"copyFromParent(\"aws_ec2\", \"host.id\")"

example-definitions/qa/metric-definition.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ metrics:
2828
aggregationTemporality: delta
2929
valueFunction: 'arithmeticSequence(1, 0, "")'
3030
isMonotonic: true
31-
reportingResources: [ pod ]
31+
filteredReportingResources:
32+
pod: ["k8s.cluster.name=cluster-skyrim"]
3233
- name: cpu.allocatable
3334
unit: "{cores}"
3435
otelType: summary
@@ -41,7 +42,8 @@ metrics:
4142
otelType: summary
4243
valueFunction: 'arithmeticSequenceSummary(1, 2, "", 5)'
4344
quantiles: [ 0, 1 ]
44-
reportingResources: [ pod ]
45+
filteredReportingResources:
46+
pod: ["k8s.pod.status=Running", "k8s.cluster.name=cluster-skyrim"]
4547
- name: system.filesystem.mountpoint.limit
4648
unit: "By"
4749
otelType: summary
@@ -72,7 +74,9 @@ metrics:
7274
valueFunction: 'arithmeticSequenceSummary(1, 2, "", 5)'
7375
isDouble: true
7476
quantiles: [ 0, 0.5, 0.75, 1 ]
75-
reportingResources: [ managed_replicaset, unmanaged_replicaset ]
77+
reportingResources: [ managed_replicaset ]
78+
filteredReportingResources:
79+
unmanaged_replicaset: ["k8s.workload.kind=unmanaged_replicaset"]
7680
- name: calls_min
7781
unit: "{calls}/min"
7882
otelType: summary

example-definitions/qa/resource-definition.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resources:
2121
namespace: 'distribution(3, 0, 0)'
2222
node: 'distribution(5, 2, 10)'
2323
attributes:
24-
k8s.cluster.name: '"cluster-".concat(alphanumericSequence("dppteam"))'
24+
k8s.cluster.name: '"cluster-".concat(alphanumericSequence("skyrim"))'
2525
- name: namespace
2626
count: 10
2727
childrenDistribution:
@@ -142,7 +142,7 @@ resources:
142142
- name: network
143143
count: 120
144144
attributes:
145-
clou.network.id: 'alphanumericSequence("IDfr3x").concat(".network")'
145+
cloud.network.id: 'alphanumericSequence("IDfr3x").concat(".network")'
146146
attributeOperations:
147147
- 'copyFromParent("aws_ec2", "host.id")'
148148
- name: filesystem

src/main/java/io/opentelemetry/contrib/generator/telemetry/metrics/MetricGeneratorThread.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,32 @@ public void run() {
9595
);
9696
otelMetrics.add(getMetricWithResourceAttributes(eachPartialMetric, resourceAttrs));
9797
}
98-
resourceMetric = ResourceMetrics.newBuilder()
99-
.setResource(reportingResource)
100-
.addScopeMetrics(ScopeMetrics.newBuilder()
101-
.setScope(InstrumentationScope.newBuilder()
102-
.setName(Constants.SELF_NAME)
103-
.setVersion(Constants.SELF_VERSION)
104-
.build())
105-
.addAllMetrics(otelMetrics)
106-
.build())
107-
.build();
108-
resourceMetricsList.add(resourceMetric);
98+
if (!otelMetrics.isEmpty()) {
99+
resourceMetric = ResourceMetrics.newBuilder()
100+
.setResource(reportingResource)
101+
.addScopeMetrics(ScopeMetrics.newBuilder()
102+
.setScope(InstrumentationScope.newBuilder()
103+
.setName(Constants.SELF_NAME)
104+
.setVersion(Constants.SELF_VERSION)
105+
.build())
106+
.addAllMetrics(otelMetrics)
107+
.build())
108+
.build();
109+
resourceMetricsList.add(resourceMetric);
110+
}
109111
}
110-
ExportMetricsServiceRequest resourceMetrics = ExportMetricsServiceRequest.newBuilder()
111-
.addAllResourceMetrics(resourceMetricsList)
112-
.build();
113-
log.info(requestID + ": Sending payload for: " + groupKey);
114-
log.debug(requestID + ": Complete payload for " + groupKey + ": " + resourceMetrics);
115-
boolean responseStatus = payloadHandler.postPayload(resourceMetrics);
116-
if (metricGeneratorState.getTransportStorage() != null) {
117-
metricGeneratorState.getTransportStorage().store(groupKey, resourceMetrics, responseStatus);
112+
if (resourceMetricsList.isEmpty()) {
113+
log.info("No metrics received from generator for: " + groupKey);
114+
} else {
115+
ExportMetricsServiceRequest resourceMetrics = ExportMetricsServiceRequest.newBuilder()
116+
.addAllResourceMetrics(resourceMetricsList)
117+
.build();
118+
log.info(requestID + ": Sending payload for: " + groupKey);
119+
//log.debug(requestID + ": Complete payload for " + groupKey + ": " + resourceMetrics);
120+
boolean responseStatus = payloadHandler.postPayload(resourceMetrics);
121+
if (metricGeneratorState.getTransportStorage() != null) {
122+
metricGeneratorState.getTransportStorage().store(groupKey, resourceMetrics, responseStatus);
123+
}
118124
}
119125
currentCount++;
120126
metricGeneratorState.getThreadPayloadCounts().put(groupKey, currentCount);
@@ -139,21 +145,28 @@ private Pair<List<Resource>, List<Set<String>>> getReportingResources() {
139145
.get(resourceType).stream()
140146
.filter(GeneratorResource::isActive)
141147
.toList();
148+
int allReportingResourcesCount = allReportingResources.size();
149+
log.info("All " + allReportingResourcesCount + " " + resourceType + " resources will report the metrics: " +
150+
String.join(", ", metricsWithAllResources));
142151
List<Set<String>> metricsForEachResource = new ArrayList<>();
143-
for (int resourceIndex=0; resourceIndex<allReportingResources.size(); resourceIndex++) {
152+
for (int resourceIndex=0; resourceIndex<allReportingResourcesCount; resourceIndex++) {
144153
metricsForEachResource.add(new HashSet<>(metricsWithAllResources));
145154
}
146155
Set<String> metricsWithFilteredResources = new HashSet<>(metrics.keySet());
147156
metricsWithFilteredResources.removeAll(metricsWithAllResources);
148157
for (String metricWithFilteredResources: metricsWithFilteredResources) {
149-
for (int resourceIndex=0; resourceIndex<allReportingResources.size(); resourceIndex++) {
158+
int selectedCount = 0;
159+
for (int resourceIndex=0; resourceIndex<allReportingResourcesCount; resourceIndex++) {
150160
if (allReportingResources.get(resourceIndex).getEvaluatedAttributes().entrySet().containsAll(
151161
metrics.get(metricWithFilteredResources).getParsedFilteredReportingResources()
152162
.get(resourceType).entrySet()
153163
)) {
154164
metricsForEachResource.get(resourceIndex).add(metricWithFilteredResources);
165+
selectedCount++;
155166
}
156167
}
168+
log.debug(selectedCount + " " + resourceType + " resources selected for metric " +
169+
metricWithFilteredResources);
157170
}
158171
return Pair.of(allReportingResources.stream().map(GeneratorResource::getOTelResource).toList(),
159172
metricsForEachResource);

src/test/java/io/opentelemetry/contrib/generator/telemetry/TestAllGeneratorsWithYAMLInput.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,8 @@ public void generateData() {
6161

6262
@Test
6363
public void validatePacketCounts() {
64-
int NETWORK_INTERFACE_COUNT = 120;
65-
int CONTAINER_COUNT = 150;
66-
int MACHINE_COUNT = 80;
67-
int NODE_COUNT = 25;
68-
int POD_COUNT = 75;
69-
int DISK_COUNT = 100;
70-
int AWS_RDS_COUNT = 50;
71-
int AWS_EBS_COUNT = 50;
72-
int METRIC_REPORTING_RESOURCES_COUNT = NETWORK_INTERFACE_COUNT + CONTAINER_COUNT + MACHINE_COUNT + NODE_COUNT +
73-
POD_COUNT + DISK_COUNT + AWS_EBS_COUNT + AWS_RDS_COUNT;
74-
int LOG_REPORTING_RESOURCES_COUNT = CONTAINER_COUNT + NODE_COUNT + 2 * POD_COUNT + MACHINE_COUNT;
75-
int metricPayloadCount = 10;
76-
int logsPayloadCount = 20;
77-
int expectedMetricPackets = METRIC_REPORTING_RESOURCES_COUNT * metricPayloadCount;
78-
int expectedLogsPackets = LOG_REPORTING_RESOURCES_COUNT * logsPayloadCount;
64+
int expectedMetricPackets = 6500;
65+
int expectedLogsPackets = 8100;
7966
int expectedSpanPackets = 11518;
8067
Assert.assertEquals(testStore.getMetricsPacketCount(), expectedMetricPackets, "Mismatch in expected metric packets count");
8168
Assert.assertEquals(testStore.getLogsPacketCount(), expectedLogsPackets, "Mismatch in expected log packets count");

src/test/java/io/opentelemetry/contrib/generator/telemetry/TestMetricsGenerator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class TestMetricsGenerator {
3939
private final int MACHINE_COUNT = 80;
4040
private final int NODE_COUNT = 25;
4141
private final int POD_COUNT = 75;
42+
private final int POD_RESTARTS_POD_COUNT = 15;
43+
private final int FS_USED_POD_COUNT = 4;
4244
private final int DISK_COUNT = 100;
4345
private final int AWS_RDS_COUNT = 50;
4446
private final int AWS_EBS_COUNT = 50;
@@ -70,16 +72,18 @@ public void testPayloadAndPacketCounts() {
7072
public void testMetricCounts() {
7173
int systemNetworkInKbSec_Count = (NETWORK_INTERFACE_COUNT + CONTAINER_COUNT + MACHINE_COUNT) * PAYLOAD_COUNT;
7274
int systemNetworkOutKbSec_Count = (NETWORK_INTERFACE_COUNT + CONTAINER_COUNT + MACHINE_COUNT) * PAYLOAD_COUNT;
73-
int podRestarts_Count = POD_COUNT * PAYLOAD_COUNT;
75+
int podRestarts_Count = POD_RESTARTS_POD_COUNT * PAYLOAD_COUNT;
7476
int cpuUsed_Count = (NODE_COUNT + CONTAINER_COUNT + POD_COUNT + MACHINE_COUNT) * PAYLOAD_COUNT;
75-
int filesystemUsed_Count = (REPORTING_RESOURCES_COUNT - NETWORK_INTERFACE_COUNT) * PAYLOAD_COUNT;
77+
int filesystemUsed_Count = (DISK_COUNT + AWS_RDS_COUNT + AWS_EBS_COUNT + NODE_COUNT + CONTAINER_COUNT +
78+
MACHINE_COUNT + FS_USED_POD_COUNT) * PAYLOAD_COUNT;
7679
int memoryUsed_Count = (AWS_RDS_COUNT + NODE_COUNT + CONTAINER_COUNT + POD_COUNT + MACHINE_COUNT) * PAYLOAD_COUNT;
80+
Assert.assertEquals(testStore.getMetricsCount().get("pod.restarts").get(), podRestarts_Count,
81+
"Mismatch in metrics count for metric pod.restarts");
7782
Assert.assertEquals(testStore.getMetricsCount().get("system.network.in.kb.sec").get(), systemNetworkInKbSec_Count,
7883
"Mismatch in metrics count for metric system.network.in.kb.sec");
7984
Assert.assertEquals(testStore.getMetricsCount().get("system.network.out.kb.sec").get(), systemNetworkOutKbSec_Count,
8085
"Mismatch in metrics count for metric system.network.out.kb.sec");
81-
Assert.assertEquals(testStore.getMetricsCount().get("pod.restarts").get(), podRestarts_Count,
82-
"Mismatch in metrics count for metric pod.restarts");
86+
8387
Assert.assertEquals(testStore.getMetricsCount().get("cpu.used").get(), cpuUsed_Count,
8488
"Mismatch in metrics count for metric cpu.used");
8589
Assert.assertEquals(testStore.getMetricsCount().get("filesystem.used").get(), filesystemUsed_Count,

src/test/java/io/opentelemetry/contrib/generator/telemetry/helpers/TestPayloadHandler.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
2222
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
2323
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
24-
import io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans;
24+
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
2525
import io.opentelemetry.proto.trace.v1.ResourceSpans;
2626
import io.opentelemetry.proto.trace.v1.ScopeSpans;
2727
import io.opentelemetry.proto.trace.v1.Span;
@@ -55,29 +55,25 @@ public TestPayloadHandler() {
5555

5656
@Override
5757
public boolean postPayload(GeneratedMessageV3 message) {
58-
if (message instanceof ExportMetricsServiceRequest) {
59-
ExportMetricsServiceRequest payload = (ExportMetricsServiceRequest) message;
58+
if (message instanceof ExportMetricsServiceRequest payload) {
6059
metricPayloads.add(payload);
6160
int resourceMetricsCount = payload.getResourceMetricsCount();
6261
metricsPacketCount.addAndGet(resourceMetricsCount);
63-
payload.getResourceMetrics(0)
64-
.getScopeMetrics(0)
65-
.getMetricsList()
66-
.forEach(metric -> {
67-
if (!metricsCount.containsKey(metric.getName())) {
68-
synchronized (metricsCount) {
69-
metricsCount.putIfAbsent(metric.getName(), new AtomicInteger(0));
70-
}
62+
for (ResourceMetrics eachRM: payload.getResourceMetricsList()) {
63+
eachRM.getScopeMetrics(0).getMetricsList().forEach(metric -> {
64+
if (!metricsCount.containsKey(metric.getName())) {
65+
synchronized (metricsCount) {
66+
metricsCount.putIfAbsent(metric.getName(), new AtomicInteger(0));
7167
}
72-
metricsCount.get(metric.getName()).addAndGet(resourceMetricsCount);
73-
});
74-
} else if (message instanceof ExportLogsServiceRequest) {
75-
ExportLogsServiceRequest payload = (ExportLogsServiceRequest) message;
68+
}
69+
metricsCount.get(metric.getName()).addAndGet(1);
70+
});
71+
}
72+
} else if (message instanceof ExportLogsServiceRequest payload) {
7673
logsPayloads.add(payload);
7774
int resourceLogsCount = payload.getResourceLogsCount();
7875
logsPacketCount.addAndGet(resourceLogsCount);
79-
} else if (message instanceof ExportTraceServiceRequest) {
80-
ExportTraceServiceRequest payload = (ExportTraceServiceRequest) message;
76+
} else if (message instanceof ExportTraceServiceRequest payload) {
8177
tracePayloads.add(payload);
8278
int spanPacketCount = payload.getResourceSpansCount();
8379
spansPacketCount.addAndGet(spanPacketCount);

src/test/resources/test-definitions/metrics-test.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ metrics:
4343
aggregationTemporality: delta
4444
isMonotonic: true
4545
valueFunction: 'arithmeticSequence(0, 1, "/3")'
46-
reportingResources: [pod]
46+
filteredReportingResources:
47+
pod: ["k8s.cluster.name=cluster-aukus"]
4748
copyResourceAttributes: ["k8s.pod.ip", "k8s.node.ip.internal"]
4849
- name: cpu.used
4950
unit: "{cores}"
5051
otelType: gauge
5152
valueFunction: 'absoluteSineSequence("*50")'
5253
isDouble: true
53-
reportingResources: [node, container, machine, pod]
54+
reportingResources: [node, machine, container]
55+
filteredReportingResources:
56+
pod: []
57+
container: ["k8s.node.name=quad.node"]
5458
copyResourceAttributes: ["k8s.pod.ip", "k8s.node.ip.internal"]
5559
attributes:
5660
max.cpu.processes: '[counter("app-process-"), roundRobin(["kernel_task", "java", "mds"])]'
@@ -60,7 +64,9 @@ metrics:
6064
aggregationTemporality: delta
6165
valueFunction: 'logarithmicSequence(30, 1, "*1024")'
6266
isDouble: true
63-
reportingResources: [disk, aws_rds, aws_ebs, node, container, machine, pod]
67+
reportingResources: [disk, aws_rds, aws_ebs, node, container, machine]
68+
filteredReportingResources:
69+
pod: ["k8s.pod.status=Running", "k8s.cluster.name=cluster-aukus"]
6470
- name: memory.used
6571
unit: MBy
6672
otelType: sum

0 commit comments

Comments
 (0)