20
20
import io .opentelemetry .semconv .resource .attributes .ResourceAttributes ;
21
21
import java .io .IOException ;
22
22
import java .util .ArrayList ;
23
+ import java .util .List ;
23
24
import java .util .jar .Attributes ;
24
25
import java .util .jar .JarFile ;
26
+ import java .util .stream .Collectors ;
27
+ import okhttp3 .MediaType ;
25
28
import okhttp3 .Request ;
29
+ import okhttp3 .RequestBody ;
26
30
import okhttp3 .Response ;
31
+ import org .hypertrace .agent .core .instrumentation .HypertraceSemanticAttributes ;
27
32
import org .junit .jupiter .api .AfterAll ;
28
33
import org .junit .jupiter .api .Assertions ;
29
34
import org .junit .jupiter .api .BeforeEach ;
34
39
// key = "SMOKETEST_JAVAAGENT_PATH",
35
40
// value =
36
41
//
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")
38
43
public class SpringBootSmokeTest extends AbstractSmokeTest {
39
44
45
+ private static final int DEFAULT_MAX_PAYLOAD_CAPTURE_SIZE = 128 * 1024 ;
46
+
40
47
@ Override
41
48
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"
43
50
+ jdk
44
- + "-20210218.577304949 " ;
51
+ + "-20210706.1005057969 " ;
45
52
}
46
53
47
54
private static GenericContainer app ;
@@ -63,12 +70,18 @@ static synchronized void afterEach() {
63
70
}
64
71
65
72
@ 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 ();
69
82
70
83
try (Response response = client .newCall (request ).execute ()) {
71
- Assertions .assertEquals (response .body ().string (), "Hi!" );
84
+ Assertions .assertEquals (response .body ().string (), requestBody );
72
85
}
73
86
ArrayList <ExportTraceServiceRequest > traces = new ArrayList <>(waitForTraces ());
74
87
@@ -107,8 +120,8 @@ public void get() throws IOException, InterruptedException {
107
120
.getValue ()
108
121
.getStringValue ());
109
122
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 " ));
112
125
Assertions .assertTrue (
113
126
getInstrumentationLibSpanStream (traces )
114
127
.anyMatch (
@@ -132,19 +145,81 @@ public void get() throws IOException, InterruptedException {
132
145
.count ()
133
146
> 0 );
134
147
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 ();
149
224
}
150
225
}
0 commit comments