-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathApplicationTest.java
More file actions
140 lines (123 loc) · 5.33 KB
/
ApplicationTest.java
File metadata and controls
140 lines (123 loc) · 5.33 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
package io.opentelemetry.example.telemetry;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.stop.Stop.stopQuietly;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import com.google.protobuf.InvalidProtocolBufferException;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.trace.v1.Span;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.Body;
import org.mockserver.model.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
/**
* The role of this class is to test the telemetry. It only works if the opentelemetry java agent is
* attached. The java agent trace exporter is configured to use the otlp exporter with http/protobuf
* protocol with the otel.exporter.otlp.protocol jvm argument.
*/
@SpringBootTest(
classes = {Application.class},
webEnvironment = RANDOM_PORT)
@AutoConfigureTestRestTemplate
class ApplicationTest {
@LocalServerPort private int port;
@Autowired TestRestTemplate template;
// Port of endpoint to export the telemetry data. 4318 is the default port when protocol is
// http/protobuf.
// https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md#otlp-exporter-span-metric-and-log-exporters
static final int EXPORTER_ENDPOINT_PORT = 4318;
// Server running to collect traces. The opentelemetry java agent is configured to export
// telemetry with the
// http/protobuf protocol.
static ClientAndServer collectorServer;
@BeforeAll
public static void setUp() {
collectorServer = startClientAndServer(EXPORTER_ENDPOINT_PORT);
collectorServer.when(request()).respond(response().withStatusCode(200));
}
@AfterAll
public static void tearDown() {
stopQuietly(collectorServer);
}
@Test
public void testTelemetry() {
template.getForEntity(URI.create("http://localhost:" + port + "/ping"), String.class);
// Assert
await()
.atMost(30, SECONDS)
.untilAsserted(
() -> {
var requests = collectorServer.retrieveRecordedRequests(request());
// verify traces
var spans = extractSpansFromRequests(requests);
assertThat(spans)
.extracting(Span::getName)
.contains("Controller.doWork", "GET", "GET /ping");
// verify metrics
var metrics = extractMetricsFromRequests(requests);
assertThat(metrics).extracting(Metric::getName).contains("apiCounter");
});
}
/**
* Extract spans from http requests received by a telemetry collector.
*
* @param requests Request received by a http server trace collector
* @return spans extracted from the request body
*/
private List<Span> extractSpansFromRequests(HttpRequest[] requests) {
return Arrays.stream(requests)
.map(HttpRequest::getBody)
.flatMap(body -> getExportTraceServiceRequest(body).stream())
.flatMap(r -> r.getResourceSpansList().stream())
.flatMap(r -> r.getScopeSpansList().stream())
.flatMap(r -> r.getSpansList().stream())
.collect(Collectors.toList());
}
private Optional<ExportTraceServiceRequest> getExportTraceServiceRequest(Body body) {
try {
return Optional.ofNullable(ExportTraceServiceRequest.parseFrom(body.getRawBytes()));
} catch (InvalidProtocolBufferException e) {
return Optional.empty();
}
}
/**
* Extract metrics from http requests received by a telemetry collector.
*
* @param requests Request received by an http server telemetry collector
* @return metrics extracted from the request body
*/
private List<Metric> extractMetricsFromRequests(HttpRequest[] requests) {
return Arrays.stream(requests)
.map(HttpRequest::getBody)
.flatMap(body -> getExportMetricsServiceRequest(body).stream())
.flatMap(r -> r.getResourceMetricsList().stream())
.flatMap(r -> r.getScopeMetricsList().stream())
.flatMap(r -> r.getMetricsList().stream())
.collect(Collectors.toList());
}
private Optional<ExportMetricsServiceRequest> getExportMetricsServiceRequest(Body body) {
try {
return Optional.ofNullable(ExportMetricsServiceRequest.parseFrom(body.getRawBytes()));
} catch (InvalidProtocolBufferException e) {
return Optional.empty();
}
}
}