Skip to content

Commit 14014fb

Browse files
committed
✅ add tests for micronaut 3
1 parent 0fb70a6 commit 14014fb

File tree

5 files changed

+429
-0
lines changed

5 files changed

+429
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
`java-library`
3+
id("net.bytebuddy.byte-buddy")
4+
id("io.opentelemetry.instrumentation.auto-instrumentation")
5+
}
6+
7+
val versions: Map<String, String> by extra
8+
9+
val micronautVersion = "3.2.0"
10+
val micronautTestVersion = "3.0.5"
11+
12+
dependencies {
13+
implementation(project(":instrumentation:netty:netty-4.1"))
14+
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-netty-4.1:${versions["opentelemetry_java_agent"]}")
15+
testImplementation(testFixtures(project(":testing-common")))
16+
testImplementation("io.micronaut.test:micronaut-test-junit5:${micronautTestVersion}")
17+
testImplementation("io.micronaut:micronaut-http-server-netty:${micronautVersion}")
18+
testImplementation("io.micronaut:micronaut-runtime:${micronautVersion}")
19+
testImplementation("io.micronaut:micronaut-inject:${micronautVersion}")
20+
testImplementation("io.micronaut:micronaut-http-client:${micronautVersion}")
21+
testAnnotationProcessor("io.micronaut:micronaut-inject-java:${micronautVersion}")
22+
testImplementation("io.micronaut.rxjava2:micronaut-rxjava2:1.1.0")
23+
testImplementation("io.micronaut.rxjava2:micronaut-rxjava2-http-server-netty:1.1.0")
24+
testImplementation("io.micronaut.rxjava2:micronaut-rxjava2-http-client:1.1.0")
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.javaagent.instrumentation.hypertrace.micronaut.v3;
18+
19+
import io.micronaut.http.HttpRequest;
20+
import io.micronaut.http.HttpResponse;
21+
import io.micronaut.http.client.HttpClient;
22+
import io.micronaut.http.client.annotation.Client;
23+
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
24+
import io.opentelemetry.sdk.trace.data.SpanData;
25+
import jakarta.inject.Inject;
26+
import java.util.List;
27+
import java.util.concurrent.TimeoutException;
28+
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
29+
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
30+
import org.hypertrace.agent.testing.TestHttpServer;
31+
import org.hypertrace.agent.testing.TestHttpServer.GetJsonHandler;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.Test;
36+
37+
@MicronautTest
38+
public class MicronautClientInstrumentationTest extends AbstractInstrumenterTest {
39+
40+
private static final String REQUEST_BODY = "hello_foo_bar";
41+
private static final String REQUEST_HEADER_NAME = "reqheadername";
42+
private static final String REQUEST_HEADER_VALUE = "reqheadervalue";
43+
44+
private static final TestHttpServer testHttpServer = new TestHttpServer();
45+
46+
@BeforeAll
47+
public static void startServer() throws Exception {
48+
testHttpServer.start();
49+
}
50+
51+
@AfterAll
52+
public static void closeServer() throws Exception {
53+
testHttpServer.close();
54+
}
55+
56+
@Inject
57+
@Client("/")
58+
private HttpClient client;
59+
60+
@Test
61+
public void getJson() throws InterruptedException, TimeoutException {
62+
String retrieve =
63+
client
64+
.toBlocking()
65+
.retrieve(
66+
HttpRequest.GET(
67+
String.format("http://localhost:%d/get_json", testHttpServer.port()))
68+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE));
69+
Assertions.assertEquals(GetJsonHandler.RESPONSE_BODY, retrieve);
70+
71+
TEST_WRITER.waitForTraces(1);
72+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
73+
Assertions.assertEquals(1, traces.size());
74+
Assertions.assertEquals(1, traces.get(0).size());
75+
SpanData clientSpan = traces.get(0).get(0);
76+
Assertions.assertEquals(
77+
REQUEST_HEADER_VALUE,
78+
clientSpan
79+
.getAttributes()
80+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
81+
Assertions.assertEquals(
82+
TestHttpServer.RESPONSE_HEADER_VALUE,
83+
clientSpan
84+
.getAttributes()
85+
.get(
86+
HypertraceSemanticAttributes.httpResponseHeader(
87+
TestHttpServer.RESPONSE_HEADER_NAME)));
88+
Assertions.assertNull(
89+
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
90+
Assertions.assertEquals(
91+
GetJsonHandler.RESPONSE_BODY,
92+
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
93+
}
94+
95+
@Test
96+
public void post() throws InterruptedException, TimeoutException {
97+
HttpResponse<Object> response =
98+
client
99+
.toBlocking()
100+
.exchange(
101+
HttpRequest.POST(
102+
String.format("http://localhost:%d/post", testHttpServer.port()),
103+
REQUEST_BODY)
104+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
105+
.header("Content-Type", "application/json"));
106+
Assertions.assertEquals(204, response.getStatus().getCode());
107+
108+
TEST_WRITER.waitForTraces(1);
109+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
110+
Assertions.assertEquals(1, traces.size());
111+
Assertions.assertEquals(1, traces.get(0).size());
112+
SpanData clientSpan = traces.get(0).get(0);
113+
Assertions.assertEquals(
114+
REQUEST_HEADER_VALUE,
115+
clientSpan
116+
.getAttributes()
117+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
118+
Assertions.assertEquals(
119+
TestHttpServer.RESPONSE_HEADER_VALUE,
120+
clientSpan
121+
.getAttributes()
122+
.get(
123+
HypertraceSemanticAttributes.httpResponseHeader(
124+
TestHttpServer.RESPONSE_HEADER_NAME)));
125+
Assertions.assertEquals(
126+
REQUEST_BODY,
127+
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
128+
Assertions.assertNull(
129+
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.javaagent.instrumentation.hypertrace.micronaut.v3;
18+
19+
import io.micronaut.runtime.server.EmbeddedServer;
20+
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
21+
import io.opentelemetry.sdk.trace.data.SpanData;
22+
import jakarta.inject.Inject;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.concurrent.TimeoutException;
26+
import okhttp3.Request;
27+
import okhttp3.RequestBody;
28+
import okhttp3.Response;
29+
import okio.Buffer;
30+
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
31+
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
35+
@MicronautTest
36+
public class MicronautInstrumentationTest extends AbstractInstrumenterTest {
37+
38+
public static final String REQUEST_HEADER_NAME = "reqheader";
39+
public static final String REQUEST_HEADER_VALUE = "reqheadervalue";
40+
41+
@Inject EmbeddedServer server;
42+
43+
@Test
44+
public void get() throws IOException, TimeoutException, InterruptedException {
45+
Request request =
46+
new Request.Builder()
47+
.url(String.format("http://localhost:%d/get_no_content", server.getPort()))
48+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
49+
.get()
50+
.build();
51+
52+
try (Response response = httpClient.newCall(request).execute()) {
53+
Assertions.assertEquals(204, response.code());
54+
}
55+
56+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
57+
TEST_WRITER.waitForTraces(1);
58+
Assertions.assertEquals(1, traces.size());
59+
List<SpanData> trace = traces.get(0);
60+
Assertions.assertEquals(1, trace.size());
61+
SpanData spanData = trace.get(0);
62+
63+
Assertions.assertEquals(
64+
REQUEST_HEADER_VALUE,
65+
spanData
66+
.getAttributes()
67+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
68+
Assertions.assertEquals(
69+
TestController.RESPONSE_HEADER_VALUE,
70+
spanData
71+
.getAttributes()
72+
.get(
73+
HypertraceSemanticAttributes.httpResponseHeader(
74+
TestController.RESPONSE_HEADER_NAME)));
75+
Assertions.assertNull(
76+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
77+
Assertions.assertNull(
78+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
79+
}
80+
81+
@Test
82+
public void postJson() throws IOException, TimeoutException, InterruptedException {
83+
RequestBody requestBody = requestBody(true, 3000, 75);
84+
Buffer requestBodyBuffer = new Buffer();
85+
requestBody.writeTo(requestBodyBuffer);
86+
String requestBodyStr = new String(requestBodyBuffer.readByteArray());
87+
88+
Request request =
89+
new Request.Builder()
90+
.url(String.format("http://localhost:%d/post", server.getPort()))
91+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
92+
.header("Transfer-Encoding", "chunked")
93+
.post(requestBody)
94+
.build();
95+
96+
try (Response response = httpClient.newCall(request).execute()) {
97+
Assertions.assertEquals(200, response.code());
98+
Assertions.assertEquals(TestController.RESPONSE_BODY, response.body().string());
99+
}
100+
101+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
102+
TEST_WRITER.waitForTraces(1);
103+
Assertions.assertEquals(1, traces.size());
104+
List<SpanData> trace = traces.get(0);
105+
Assertions.assertEquals(1, trace.size());
106+
SpanData spanData = trace.get(0);
107+
108+
Assertions.assertEquals(
109+
REQUEST_HEADER_VALUE,
110+
spanData
111+
.getAttributes()
112+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
113+
Assertions.assertEquals(
114+
TestController.RESPONSE_HEADER_VALUE,
115+
spanData
116+
.getAttributes()
117+
.get(
118+
HypertraceSemanticAttributes.httpResponseHeader(
119+
TestController.RESPONSE_HEADER_NAME)));
120+
Assertions.assertEquals(
121+
requestBodyStr,
122+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
123+
Assertions.assertEquals(
124+
TestController.RESPONSE_BODY,
125+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
126+
}
127+
128+
@Test
129+
public void stream() throws IOException, TimeoutException, InterruptedException {
130+
Request request =
131+
new Request.Builder()
132+
.url(String.format("http://localhost:%d/stream", server.getPort()))
133+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
134+
.get()
135+
.build();
136+
137+
StringBuilder responseBody = new StringBuilder();
138+
for (String body : TestController.streamBody()) {
139+
responseBody.append(body);
140+
}
141+
142+
try (Response response = httpClient.newCall(request).execute()) {
143+
Assertions.assertEquals(200, response.code());
144+
Assertions.assertEquals(responseBody.toString(), response.body().string());
145+
}
146+
147+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
148+
TEST_WRITER.waitForTraces(1);
149+
Assertions.assertEquals(1, traces.size());
150+
List<SpanData> trace = traces.get(0);
151+
Assertions.assertEquals(1, trace.size());
152+
SpanData spanData = trace.get(0);
153+
154+
Assertions.assertEquals(
155+
REQUEST_HEADER_VALUE,
156+
spanData
157+
.getAttributes()
158+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
159+
Assertions.assertNull(
160+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
161+
Assertions.assertEquals(
162+
responseBody.toString(),
163+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
164+
}
165+
166+
@Test
167+
public void blocking() throws IOException, TimeoutException, InterruptedException {
168+
Request request =
169+
new Request.Builder()
170+
.url(String.format("http://localhost:%d/post", server.getPort()))
171+
.header(REQUEST_HEADER_NAME, REQUEST_HEADER_VALUE)
172+
.header("mockblock", "true")
173+
.get()
174+
.build();
175+
176+
try (Response response = httpClient.newCall(request).execute()) {
177+
Assertions.assertEquals(403, response.code());
178+
Assertions.assertTrue(response.body().string().isEmpty());
179+
}
180+
181+
List<List<SpanData>> traces = TEST_WRITER.getTraces();
182+
TEST_WRITER.waitForTraces(1);
183+
Assertions.assertEquals(1, traces.size());
184+
List<SpanData> trace = traces.get(0);
185+
Assertions.assertEquals(1, trace.size());
186+
SpanData spanData = trace.get(0);
187+
188+
Assertions.assertEquals(
189+
REQUEST_HEADER_VALUE,
190+
spanData
191+
.getAttributes()
192+
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER_NAME)));
193+
Assertions.assertNull(
194+
spanData
195+
.getAttributes()
196+
.get(
197+
HypertraceSemanticAttributes.httpResponseHeader(
198+
TestController.RESPONSE_HEADER_NAME)));
199+
Assertions.assertNull(
200+
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
201+
}
202+
}

0 commit comments

Comments
 (0)