Skip to content

Commit 168a449

Browse files
authored
Use hypertrace SB smoke test image and test payload trucation (#332)
* Use hypertrace SB smoke test image and test payload trucation Signed-off-by: Pavol Loffay <[email protected]> * fmt Signed-off-by: Pavol Loffay <[email protected]> * fix name Signed-off-by: Pavol Loffay <[email protected]>
1 parent cf0a537 commit 168a449

File tree

1 file changed

+98
-23
lines changed

1 file changed

+98
-23
lines changed

smoke-tests/src/test/java/org/hypertrace/agent/smoketest/SpringBootSmokeTest.java

+98-23
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
2121
import java.io.IOException;
2222
import java.util.ArrayList;
23+
import java.util.List;
2324
import java.util.jar.Attributes;
2425
import java.util.jar.JarFile;
26+
import java.util.stream.Collectors;
27+
import okhttp3.MediaType;
2528
import okhttp3.Request;
29+
import okhttp3.RequestBody;
2630
import okhttp3.Response;
31+
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
2732
import org.junit.jupiter.api.AfterAll;
2833
import org.junit.jupiter.api.Assertions;
2934
import org.junit.jupiter.api.BeforeEach;
@@ -34,14 +39,16 @@
3439
// key = "SMOKETEST_JAVAAGENT_PATH",
3540
// value =
3641
//
37-
// "/Users/ploffay/projects/hypertrace/javaagent/javaagent/build/libs/hypertrace-agent-1.0.1-SNAPSHOT-all.jar")
42+
// "/home/ploffay/projects/hypertrace/javaagent/javaagent/build/libs/hypertrace-agent-1.1.1-SNAPSHOT-all.jar")
3843
public class SpringBootSmokeTest extends AbstractSmokeTest {
3944

45+
private static final int DEFAULT_MAX_PAYLOAD_CAPTURE_SIZE = 128 * 1024;
46+
4047
@Override
4148
protected String getTargetImage(int jdk) {
42-
return "ghcr.io/open-telemetry/java-test-containers:smoke-springboot-jdk"
49+
return "hypertrace/java-agent-test-containers:smoke-springboot-jdk"
4350
+ jdk
44-
+ "-20210218.577304949";
51+
+ "-20210706.1005057969";
4552
}
4653

4754
private static GenericContainer app;
@@ -63,12 +70,18 @@ static synchronized void afterEach() {
6370
}
6471

6572
@Test
66-
public void get() throws IOException, InterruptedException {
67-
String url = String.format("http://localhost:%d/greeting", app.getMappedPort(8080));
68-
Request request = new Request.Builder().url(url).get().build();
73+
public void postJson() throws IOException, InterruptedException {
74+
String url = String.format("http://localhost:%d/echo", app.getMappedPort(8080));
75+
String requestBody = "request_body";
76+
Request request =
77+
new Request.Builder()
78+
.url(url)
79+
.addHeader("Content-Type", "application/json")
80+
.post(RequestBody.create(requestBody, MediaType.parse("application/json")))
81+
.build();
6982

7083
try (Response response = client.newCall(request).execute()) {
71-
Assertions.assertEquals(response.body().string(), "Hi!");
84+
Assertions.assertEquals(response.body().string(), requestBody);
7285
}
7386
ArrayList<ExportTraceServiceRequest> traces = new ArrayList<>(waitForTraces());
7487

@@ -107,8 +120,8 @@ public void get() throws IOException, InterruptedException {
107120
.getValue()
108121
.getStringValue());
109122

110-
Assertions.assertEquals(1, countSpansByName(traces, "/greeting"));
111-
Assertions.assertEquals(1, countSpansByName(traces, "WebController.greeting"));
123+
Assertions.assertEquals(1, countSpansByName(traces, "/echo"));
124+
Assertions.assertEquals(1, countSpansByName(traces, "WebController.echo"));
112125
Assertions.assertTrue(
113126
getInstrumentationLibSpanStream(traces)
114127
.anyMatch(
@@ -132,19 +145,81 @@ public void get() throws IOException, InterruptedException {
132145
.count()
133146
> 0);
134147

135-
// OTEL BS smoke test app does not have an endpoint that uses content type what we capture
136-
// enable once we add smoke tests apps to our build.
137-
// List<String> responseBodyAttributes =
138-
// getSpanStream(traces)
139-
// .flatMap(span -> span.getAttributesList().stream())
140-
// .filter(
141-
// attribute ->
142-
// attribute
143-
// .getKey()
144-
// .contains(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey()))
145-
// .map(attribute -> attribute.getValue().getStringValue())
146-
// .collect(Collectors.toList());
147-
// Assertions.assertEquals(1, responseBodyAttributes.size());
148-
// Assertions.assertEquals("Hi!", responseBodyAttributes.get(0));
148+
List<String> responseBodyAttributes =
149+
getSpanStream(traces)
150+
.flatMap(span -> span.getAttributesList().stream())
151+
.filter(
152+
attribute ->
153+
attribute
154+
.getKey()
155+
.contains(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey()))
156+
.map(attribute -> attribute.getValue().getStringValue())
157+
.collect(Collectors.toList());
158+
Assertions.assertEquals(1, responseBodyAttributes.size());
159+
Assertions.assertEquals(requestBody, responseBodyAttributes.get(0));
160+
List<String> requestBodyAttributes =
161+
getSpanStream(traces)
162+
.flatMap(span -> span.getAttributesList().stream())
163+
.filter(
164+
attribute ->
165+
attribute
166+
.getKey()
167+
.contains(HypertraceSemanticAttributes.HTTP_REQUEST_BODY.getKey()))
168+
.map(attribute -> attribute.getValue().getStringValue())
169+
.collect(Collectors.toList());
170+
Assertions.assertEquals(1, requestBodyAttributes.size());
171+
Assertions.assertEquals(requestBody, requestBodyAttributes.get(0));
172+
}
173+
174+
@Test
175+
public void postJson_payload_truncation() throws IOException {
176+
String url = String.format("http://localhost:%d/echo", app.getMappedPort(8080));
177+
String requestBody = createRequestBody(DEFAULT_MAX_PAYLOAD_CAPTURE_SIZE + 150, 'a');
178+
Request request =
179+
new Request.Builder()
180+
.url(url)
181+
.addHeader("Content-Type", "application/json")
182+
.post(RequestBody.create(requestBody, MediaType.parse("application/json")))
183+
.build();
184+
185+
try (Response response = client.newCall(request).execute()) {
186+
Assertions.assertEquals(response.body().string(), requestBody);
187+
}
188+
ArrayList<ExportTraceServiceRequest> traces = new ArrayList<>(waitForTraces());
189+
190+
List<String> responseBodyAttributes =
191+
getSpanStream(traces)
192+
.flatMap(span -> span.getAttributesList().stream())
193+
.filter(
194+
attribute ->
195+
attribute
196+
.getKey()
197+
.contains(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey()))
198+
.map(attribute -> attribute.getValue().getStringValue())
199+
.collect(Collectors.toList());
200+
Assertions.assertEquals(1, responseBodyAttributes.size());
201+
Assertions.assertEquals(
202+
DEFAULT_MAX_PAYLOAD_CAPTURE_SIZE, responseBodyAttributes.get(0).length());
203+
List<String> requestBodyAttributes =
204+
getSpanStream(traces)
205+
.flatMap(span -> span.getAttributesList().stream())
206+
.filter(
207+
attribute ->
208+
attribute
209+
.getKey()
210+
.contains(HypertraceSemanticAttributes.HTTP_REQUEST_BODY.getKey()))
211+
.map(attribute -> attribute.getValue().getStringValue())
212+
.collect(Collectors.toList());
213+
Assertions.assertEquals(1, requestBodyAttributes.size());
214+
Assertions.assertEquals(
215+
DEFAULT_MAX_PAYLOAD_CAPTURE_SIZE, requestBodyAttributes.get(0).length());
216+
}
217+
218+
private String createRequestBody(int size, char item) {
219+
StringBuilder stringBuilder = new StringBuilder();
220+
for (int i = 0; i < size; i++) {
221+
stringBuilder.append(item);
222+
}
223+
return stringBuilder.toString();
149224
}
150225
}

0 commit comments

Comments
 (0)