forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractIntegrationTest.java
More file actions
321 lines (283 loc) · 12.2 KB
/
AbstractIntegrationTest.java
File metadata and controls
321 lines (283 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.jmxmetrics;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.awaitility.Awaitility.await;
import static org.testcontainers.Testcontainers.exposeHostPorts;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.grpc.GrpcService;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
import io.grpc.stub.StreamObserver;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceResponse;
import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc;
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.assertj.core.api.MapAssert;
import org.awaitility.core.ConditionTimeoutException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.MountableFile;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers(disabledWithoutDocker = true)
public abstract class AbstractIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(AbstractIntegrationTest.class);
private final boolean configFromStdin;
private final String configName;
private GenericContainer<?> scraper;
private OtlpGrpcServer otlpServer;
protected AbstractIntegrationTest(boolean configFromStdin, String configName) {
this.configFromStdin = configFromStdin;
this.configName = configName;
}
@BeforeAll
void beforeAll() {
otlpServer = new OtlpGrpcServer();
otlpServer.start();
exposeHostPorts(otlpServer.httpPort());
String otlpEndpoint = "http://host.testcontainers.internal:" + otlpServer.httpPort();
scraper = buildScraper(otlpEndpoint);
scraper.start();
}
@AfterAll
// No need to block other tests waiting on return.
@SuppressWarnings("FutureReturnValueIgnored")
void afterAll() {
otlpServer.stop();
scraper.stop();
}
@BeforeEach
void beforeEach() {
otlpServer.reset();
}
protected GenericContainer<?> buildScraper(String otlpEndpoint) {
String scraperJarPath = System.getProperty("shadow.jar.path");
List<String> scraperCommand = new ArrayList<>();
scraperCommand.add("java");
scraperCommand.add("-cp");
scraperCommand.add("/app/OpenTelemetryJava.jar");
scraperCommand.add("-Dotel.jmx.username=cassandra");
scraperCommand.add("-Dotel.jmx.password=cassandra");
scraperCommand.add("-Dotel.exporter.otlp.endpoint=" + otlpEndpoint);
scraperCommand.add("io.opentelemetry.contrib.jmxmetrics.JmxMetrics");
scraperCommand.add("-config");
if (configFromStdin) {
String cmd = String.join(" ", scraperCommand);
scraperCommand = Arrays.asList("sh", "-c", "cat /app/" + configName + " | " + cmd + " -");
} else {
scraperCommand.add("/app/" + configName);
}
return new GenericContainer<>("openjdk:8u272-jre-slim")
.withNetwork(Network.SHARED)
.withCopyFileToContainer(
MountableFile.forHostPath(scraperJarPath), "/app/OpenTelemetryJava.jar")
.withCopyFileToContainer(
MountableFile.forClasspathResource("script.groovy"), "/app/script.groovy")
.withCopyFileToContainer(
MountableFile.forClasspathResource(configName), "/app/" + configName)
.withCommand(scraperCommand.toArray(new String[0]))
.withLogConsumer(new Slf4jLogConsumer(logger))
.withStartupTimeout(Duration.ofMinutes(2))
.waitingFor(Wait.forLogMessage(".*Started GroovyRunner.*", 1));
}
protected static GenericContainer<?> cassandraContainer() {
return new GenericContainer<>("cassandra:3.11")
.withNetwork(Network.SHARED)
.withEnv("LOCAL_JMX", "no")
.withCopyFileToContainer(
MountableFile.forClasspathResource("cassandra/jmxremote.password", 0400),
"/etc/cassandra/jmxremote.password")
.withNetworkAliases("cassandra")
.withExposedPorts(7199)
.withStartupTimeout(Duration.ofMinutes(2))
.waitingFor(Wait.forLogMessage(".*Startup complete.*", 1));
}
protected final void waitAndAssertMetrics(Iterable<Consumer<Metric>> assertions) {
waitAndAssertMetrics(
() -> {
List<Metric> metrics =
otlpServer.getMetrics().stream()
.map(ExportMetricsServiceRequest::getResourceMetricsList)
.flatMap(rm -> rm.stream().map(ResourceMetrics::getScopeMetricsList))
.flatMap(Collection::stream)
.filter(
sm ->
sm.getScope().getName().equals("io.opentelemetry.contrib.jmxmetrics")
&& sm.getScope().getVersion().equals(expectedMeterVersion()))
.flatMap(sm -> sm.getMetricsList().stream())
.collect(Collectors.toList());
assertThat(metrics).isNotEmpty();
for (Consumer<Metric> assertion : assertions) {
assertThat(metrics).anySatisfy(assertion);
}
});
}
private static void waitAndAssertMetrics(Runnable assertion) {
try {
await().atMost(Duration.ofSeconds(30)).untilAsserted(assertion::run);
} catch (Throwable t) {
if (t instanceof ConditionTimeoutException) {
// Don't throw this failure since the stack is the awaitility thread, causing confusion.
// Instead, just assert one more time on the test thread, which will fail with a better
// stack trace - that is on the same thread as the test.
assertion.run();
} else {
throw t;
}
}
}
@SafeVarargs
@SuppressWarnings("varargs")
protected final void waitAndAssertMetrics(Consumer<Metric>... assertions) {
waitAndAssertMetrics(Arrays.asList(assertions));
}
protected void assertGauge(Metric metric, String name, String description, String unit) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasGauge()).isTrue();
assertThat(metric.getGauge().getDataPointsList())
.satisfiesExactly(point -> assertThat(point.getAttributesList()).isEmpty());
}
protected void assertSum(Metric metric, String name, String description, String unit) {
assertSum(metric, name, description, unit, /* isMonotonic= */ true);
}
protected void assertSum(
Metric metric, String name, String description, String unit, boolean isMonotonic) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasSum()).isTrue();
assertThat(metric.getSum().getDataPointsList())
.satisfiesExactly(point -> assertThat(point.getAttributesList()).isEmpty());
assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic);
}
protected void assertTypedGauge(
Metric metric, String name, String description, String unit, List<String> types) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasGauge()).isTrue();
assertTypedPoints(metric.getGauge().getDataPointsList(), types);
}
protected void assertTypedSum(
Metric metric, String name, String description, String unit, List<String> types) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasSum()).isTrue();
assertTypedPoints(metric.getSum().getDataPointsList(), types);
}
@SafeVarargs
protected final void assertSumWithAttributes(
Metric metric,
String name,
String description,
String unit,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasSum()).isTrue();
assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroupAssertions);
}
@SafeVarargs
protected final void assertGaugeWithAttributes(
Metric metric,
String name,
String description,
String unit,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasGauge()).isTrue();
assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions);
}
private static String expectedMeterVersion() {
// Automatically set by gradle when running the tests
String version = System.getProperty("gradle.project.version");
assertThat(version).isNotBlank();
return version;
}
@SuppressWarnings("unchecked")
private static void assertTypedPoints(List<NumberDataPoint> points, List<String> types) {
Consumer<MapAssert<String, String>>[] assertions =
types.stream()
.map(
type ->
(Consumer<MapAssert<String, String>>)
attrs -> attrs.containsOnly(entry("name", type)))
.toArray(Consumer[]::new);
assertAttributedPoints(points, assertions);
}
@SuppressWarnings("unchecked")
private static void assertAttributedPoints(
List<NumberDataPoint> points,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
Consumer<Map<String, String>>[] assertions =
Arrays.stream(attributeGroupAssertions)
.map(assertion -> (Consumer<Map<String, String>>) m -> assertion.accept(assertThat(m)))
.toArray(Consumer[]::new);
assertThat(points)
.extracting(
numberDataPoint ->
numberDataPoint.getAttributesList().stream()
.collect(
Collectors.toMap(
KeyValue::getKey, keyValue -> keyValue.getValue().getStringValue())))
.satisfiesExactlyInAnyOrder(assertions);
}
private static class OtlpGrpcServer extends ServerExtension {
private final BlockingQueue<ExportMetricsServiceRequest> metricRequests =
new LinkedBlockingDeque<>();
List<ExportMetricsServiceRequest> getMetrics() {
return new ArrayList<>(metricRequests);
}
void reset() {
metricRequests.clear();
}
@Override
protected void configure(ServerBuilder sb) {
sb.service(
GrpcService.builder()
.addService(
new MetricsServiceGrpc.MetricsServiceImplBase() {
@Override
public void export(
ExportMetricsServiceRequest request,
StreamObserver<ExportMetricsServiceResponse> responseObserver) {
metricRequests.add(request);
responseObserver.onNext(ExportMetricsServiceResponse.getDefaultInstance());
responseObserver.onCompleted();
}
})
.build());
sb.http(0);
}
}
}