Skip to content

Commit 974df5e

Browse files
authored
Add get tests to netty and vertx (#205)
Signed-off-by: Pavol Loffay <[email protected]>
1 parent 2b43e92 commit 974df5e

File tree

5 files changed

+176
-21
lines changed

5 files changed

+176
-21
lines changed

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/AttributeKeys.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ private static <T> AttributeKey<T> attributeKey(String key) {
6060
ConcurrentMap<String, AttributeKey<?>> classLoaderMap =
6161
map.computeIfAbsent(AttributeKey.class.getClassLoader(), mapSupplier);
6262
if (classLoaderMap.containsKey(key)) {
63-
return (AttributeKey<T>) classLoaderMap.get(key);
63+
@SuppressWarnings("unchecked")
64+
AttributeKey<T> attrKey = (AttributeKey<T>) classLoaderMap.get(key);
65+
return attrKey;
6466
}
6567

6668
AttributeKey<T> value = new AttributeKey<>(key);

instrumentation/netty/netty-4.0/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/Netty40ServerInstrumentationTest.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.netty.channel.nio.NioEventLoopGroup;
3232
import io.netty.channel.socket.nio.NioServerSocketChannel;
3333
import io.netty.handler.codec.http.DefaultFullHttpResponse;
34+
import io.netty.handler.codec.http.HttpRequest;
3435
import io.netty.handler.codec.http.HttpRequestDecoder;
3536
import io.netty.handler.codec.http.HttpResponse;
3637
import io.netty.handler.codec.http.HttpResponseEncoder;
@@ -91,19 +92,36 @@ protected void initChannel(Channel ch) throws Exception {
9192

9293
pipeline.addLast(
9394
new SimpleChannelInboundHandler() {
95+
96+
HttpRequest httpRequest;
97+
9498
@Override
9599
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
100+
101+
if (msg instanceof HttpRequest) {
102+
this.httpRequest = (HttpRequest) msg;
103+
}
104+
96105
// write response after all content has been received otherwise
97106
// server span is closed before request payload is captured
98107
if (msg instanceof LastHttpContent) {
99-
ByteBuf responseBody = Unpooled.wrappedBuffer(RESPONSE_BODY.getBytes());
100-
HttpResponse response =
101-
new DefaultFullHttpResponse(
102-
HTTP_1_1, HttpResponseStatus.valueOf(200), responseBody);
103-
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
104-
response.headers().add("Content-Type", "application-json");
105-
response.headers().set(CONTENT_LENGTH, responseBody.readableBytes());
106-
ctx.write(response);
108+
if (httpRequest.getUri().contains("get_no_content")) {
109+
HttpResponse response =
110+
new DefaultFullHttpResponse(
111+
HTTP_1_1, HttpResponseStatus.valueOf(204));
112+
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
113+
response.headers().set(CONTENT_LENGTH, 0);
114+
ctx.write(response);
115+
} else if (httpRequest.getUri().contains("post")) {
116+
ByteBuf responseBody = Unpooled.wrappedBuffer(RESPONSE_BODY.getBytes());
117+
HttpResponse response =
118+
new DefaultFullHttpResponse(
119+
HTTP_1_1, HttpResponseStatus.valueOf(200), responseBody);
120+
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
121+
response.headers().add("Content-Type", "application-json");
122+
response.headers().set(CONTENT_LENGTH, responseBody.readableBytes());
123+
ctx.write(response);
124+
}
107125
}
108126
}
109127

@@ -131,12 +149,48 @@ private static void stopServer() {
131149
}
132150
}
133151

152+
@Test
153+
public void get() throws IOException, TimeoutException, InterruptedException {
154+
Request request =
155+
new Request.Builder()
156+
.url(String.format("http://localhost:%d/get_no_content", port))
157+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
158+
.get()
159+
.build();
160+
161+
try (Response response = httpClient.newCall(request).execute()) {
162+
Assertions.assertEquals(204, response.code());
163+
}
164+
165+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
166+
TEST_WRITER.waitForTraces(1);
167+
Assertions.assertEquals(1, traces.size());
168+
List<SpanData> trace = traces.get(0);
169+
Assertions.assertEquals(1, trace.size());
170+
SpanData spanData = trace.get(0);
171+
172+
Assertions.assertEquals(
173+
REQUEST_HEADER_VALUE,
174+
spanData
175+
.getAttributes()
176+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
177+
Assertions.assertEquals(
178+
RESPONSE_HEADER_VALUE,
179+
spanData
180+
.getAttributes()
181+
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER_NAME)));
182+
Assertions.assertNull(
183+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
184+
Assertions.assertNull(
185+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
186+
}
187+
134188
@Test
135189
public void postJson() throws IOException, TimeoutException, InterruptedException {
136190
RequestBody requestBody = requestBody(true, 3000, 75);
137191
Request request =
138192
new Request.Builder()
139-
.url(String.format("http://localhost:%d", port))
193+
.url(String.format("http://localhost:%d/post", port))
140194
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
141195
.header("Transfer-Encoding", "chunked")
142196
.post(requestBody)
@@ -178,7 +232,7 @@ public void postJson() throws IOException, TimeoutException, InterruptedExceptio
178232
public void blocking() throws IOException, TimeoutException, InterruptedException {
179233
Request request =
180234
new Request.Builder()
181-
.url(String.format("http://localhost:%d", port))
235+
.url(String.format("http://localhost:%d/post", port))
182236
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
183237
.header("mockblock", "true")
184238
.get()

instrumentation/netty/netty-4.1/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_1/Netty41ServerInstrumentationTest.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.netty.channel.nio.NioEventLoopGroup;
3232
import io.netty.channel.socket.nio.NioServerSocketChannel;
3333
import io.netty.handler.codec.http.DefaultFullHttpResponse;
34+
import io.netty.handler.codec.http.HttpRequest;
3435
import io.netty.handler.codec.http.HttpRequestDecoder;
3536
import io.netty.handler.codec.http.HttpResponse;
3637
import io.netty.handler.codec.http.HttpResponseEncoder;
@@ -91,19 +92,36 @@ protected void initChannel(Channel ch) throws Exception {
9192

9293
pipeline.addLast(
9394
new SimpleChannelInboundHandler() {
95+
96+
HttpRequest httpRequest;
97+
9498
@Override
9599
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
100+
101+
if (msg instanceof HttpRequest) {
102+
this.httpRequest = (HttpRequest) msg;
103+
}
104+
96105
// write response after all content has been received otherwise
97106
// server span is closed before request payload is captured
98107
if (msg instanceof LastHttpContent) {
99-
ByteBuf responseBody = Unpooled.wrappedBuffer(RESPONSE_BODY.getBytes());
100-
HttpResponse response =
101-
new DefaultFullHttpResponse(
102-
HTTP_1_1, HttpResponseStatus.valueOf(200), responseBody);
103-
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
104-
response.headers().add("Content-Type", "application-json");
105-
response.headers().set(CONTENT_LENGTH, responseBody.readableBytes());
106-
ctx.write(response);
108+
if (httpRequest.getUri().contains("get_no_content")) {
109+
HttpResponse response =
110+
new DefaultFullHttpResponse(
111+
HTTP_1_1, HttpResponseStatus.valueOf(204));
112+
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
113+
response.headers().set(CONTENT_LENGTH, 0);
114+
ctx.write(response);
115+
} else if (httpRequest.getUri().contains("post")) {
116+
ByteBuf responseBody = Unpooled.wrappedBuffer(RESPONSE_BODY.getBytes());
117+
HttpResponse response =
118+
new DefaultFullHttpResponse(
119+
HTTP_1_1, HttpResponseStatus.valueOf(200), responseBody);
120+
response.headers().add(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
121+
response.headers().add("Content-Type", "application-json");
122+
response.headers().set(CONTENT_LENGTH, responseBody.readableBytes());
123+
ctx.write(response);
124+
}
107125
}
108126
}
109127

@@ -131,12 +149,48 @@ private static void stopServer() {
131149
}
132150
}
133151

152+
@Test
153+
public void get() throws IOException, TimeoutException, InterruptedException {
154+
Request request =
155+
new Request.Builder()
156+
.url(String.format("http://localhost:%d/get_no_content", port))
157+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
158+
.get()
159+
.build();
160+
161+
try (Response response = httpClient.newCall(request).execute()) {
162+
Assertions.assertEquals(204, response.code());
163+
}
164+
165+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
166+
TEST_WRITER.waitForTraces(1);
167+
Assertions.assertEquals(1, traces.size());
168+
List<SpanData> trace = traces.get(0);
169+
Assertions.assertEquals(1, trace.size());
170+
SpanData spanData = trace.get(0);
171+
172+
Assertions.assertEquals(
173+
REQUEST_HEADER_VALUE,
174+
spanData
175+
.getAttributes()
176+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
177+
Assertions.assertEquals(
178+
RESPONSE_HEADER_VALUE,
179+
spanData
180+
.getAttributes()
181+
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER_NAME)));
182+
Assertions.assertNull(
183+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
184+
Assertions.assertNull(
185+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
186+
}
187+
134188
@Test
135189
public void postJson() throws IOException, TimeoutException, InterruptedException {
136190
RequestBody requestBody = requestBody(true, 3000, 75);
137191
Request request =
138192
new Request.Builder()
139-
.url(String.format("http://localhost:%d", port))
193+
.url(String.format("http://localhost:%d/post", port))
140194
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
141195
.header("Transfer-Encoding", "chunked")
142196
.post(requestBody)
@@ -178,7 +232,7 @@ public void postJson() throws IOException, TimeoutException, InterruptedExceptio
178232
public void blocking() throws IOException, TimeoutException, InterruptedException {
179233
Request request =
180234
new Request.Builder()
181-
.url(String.format("http://localhost:%d", port))
235+
.url(String.format("http://localhost:%d/post", port))
182236
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
183237
.header("mockblock", "true")
184238
.get()

instrumentation/vertx-web-3.0/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/VertxInstrumentationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,42 @@ public void postJsonReturnNoChunked() throws IOException, TimeoutException, Inte
9696
postJson(String.format("http://localhost:%d/return_no_chunked", port));
9797
}
9898

99+
@Test
100+
public void get() throws IOException, TimeoutException, InterruptedException {
101+
Request request =
102+
new Request.Builder()
103+
.url(String.format("http://localhost:%d/get", port))
104+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
105+
.get()
106+
.build();
107+
try (Response response = httpClient.newCall(request).execute()) {
108+
Assertions.assertEquals(200, response.code());
109+
}
110+
111+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
112+
TEST_WRITER.waitForTraces(1);
113+
Assertions.assertEquals(1, traces.size());
114+
List<SpanData> trace = traces.get(0);
115+
Assertions.assertEquals(1, trace.size());
116+
SpanData spanData = trace.get(0);
117+
Assertions.assertNull(
118+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
119+
Assertions.assertEquals(
120+
REQUEST_HEADER_VALUE,
121+
spanData
122+
.getAttributes()
123+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
124+
Assertions.assertNull(
125+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
126+
Assertions.assertEquals(
127+
VertxWebServer.RESPONSE_HEADER_VALUE,
128+
spanData
129+
.getAttributes()
130+
.get(
131+
HypertraceSemanticAttributes.httpResponseHeader(
132+
VertxWebServer.RESPONSE_HEADER_NAME)));
133+
}
134+
99135
@Test
100136
public void blocking() throws IOException, TimeoutException, InterruptedException {
101137
Request request =

instrumentation/vertx-web-3.0/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/VertxWebServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public void start(Future<Void> startFuture) {
6969
ctx.response().setStatusCode(200).end(RESPONSE_BODY);
7070
}));
7171

72+
router
73+
.route("/get")
74+
.handler(
75+
ctx -> {
76+
ctx.response().setStatusCode(200);
77+
ctx.response().putHeader(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
78+
ctx.response().end();
79+
});
80+
7281
vertx
7382
.createHttpServer()
7483
.requestHandler(router::accept)

0 commit comments

Comments
 (0)