Skip to content

Commit ac1b57d

Browse files
committed
Fix test for v0.4 endpoint
Agent returns an error if no content sent, so test was failing. Need to send empty content for test to succeed.
1 parent 92e0ede commit ac1b57d

3 files changed

Lines changed: 46 additions & 25 deletions

File tree

dd-java-agent-ittests/dd-java-agent-ittests.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test {
5252
}
5353

5454
if (project.hasProperty("disableShadowRelocate") && disableShadowRelocate) {
55-
exclude 'datadog/trace/agent/ShadowPackageRenamingTest.class'
55+
exclude 'datadog/trace/agent/integration/classloading/ShadowPackageRenamingTest.class'
5656
}
5757
}
5858

dd-trace-ot/src/main/java/datadog/trace/common/writer/DDApi.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import java.io.IOException;
1111
import java.io.InputStreamReader;
1212
import java.io.OutputStream;
13-
import java.io.OutputStreamWriter;
1413
import java.net.HttpURLConnection;
1514
import java.net.URL;
1615
import java.util.ArrayList;
16+
import java.util.Collections;
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.concurrent.TimeUnit;
@@ -40,8 +40,8 @@ public class DDApi {
4040
private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
4141

4242
public DDApi(final String host, final int port) {
43-
if (endpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4)
44-
&& endpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) {
43+
if (traceEndpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4)
44+
&& serviceEndpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) {
4545
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT_V4;
4646
this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT_V4;
4747
} else {
@@ -163,25 +163,30 @@ private boolean putContent(
163163
}
164164
}
165165

166-
private boolean endpointAvailable(final String endpoint) {
167-
return endpointAvailable(endpoint, true);
166+
private boolean traceEndpointAvailable(final String endpoint) {
167+
return endpointAvailable(endpoint, Collections.emptyList(), true);
168168
}
169169

170-
private boolean endpointAvailable(final String endpoint, final boolean retry) {
170+
private boolean serviceEndpointAvailable(final String endpoint) {
171+
return endpointAvailable(endpoint, Collections.emptyMap(), true);
172+
}
173+
174+
private boolean endpointAvailable(final String endpoint, final Object data, final boolean retry) {
171175
try {
172176
final HttpURLConnection httpCon = getHttpURLConnection(endpoint);
173177

174178
// This is potentially called in premain, so we want to fail fast.
175179
httpCon.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(1));
176180
httpCon.setReadTimeout((int) TimeUnit.SECONDS.toMillis(1));
177181

178-
final OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
182+
final OutputStream out = httpCon.getOutputStream();
183+
objectMapper.writeValue(out, data);
179184
out.flush();
180185
out.close();
181186
return httpCon.getResponseCode() == 200;
182187
} catch (final IOException e) {
183188
if (retry) {
184-
return endpointAvailable(endpoint, false);
189+
return endpointAvailable(endpoint, data, false);
185190
}
186191
}
187192
return false;

dd-trace-ot/src/test/groovy/datadog/trace/api/writer/DDApiTest.groovy

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ class DDApiTest extends Specification {
3030
def agent = ratpack {
3131
handlers {
3232
put("v0.4/traces") {
33-
response.status(200).send()
33+
def status = request.contentLength > 0 ? 200 : 500
34+
response.status(status).send()
3435
}
3536
put("v0.4/services") {
36-
response.status(200).send()
37+
def status = request.contentLength > 0 ? 200 : 500
38+
response.status(status).send()
3739
}
3840
}
3941
}
@@ -56,7 +58,8 @@ class DDApiTest extends Specification {
5658
response.status(404).send()
5759
}
5860
put("v0.4/services") {
59-
response.status(200).send()
61+
def status = request.contentLength > 0 ? 200 : 500
62+
response.status(status).send()
6063
}
6164
}
6265
}
@@ -87,7 +90,8 @@ class DDApiTest extends Specification {
8790
}
8891
}
8992
put("v0.4/services") {
90-
response.status(200).send()
93+
def status = request.contentLength > 0 ? 200 : 500
94+
response.status(status).send()
9195
}
9296
}
9397
}
@@ -144,10 +148,12 @@ class DDApiTest extends Specification {
144148
def agent = ratpack {
145149
handlers {
146150
put("v0.4/traces") {
147-
response.status(200).send()
151+
def status = request.contentLength > 0 ? 200 : 500
152+
response.status(status).send()
148153
}
149154
put("v0.4/services") {
150-
response.status(200).send()
155+
def status = request.contentLength > 0 ? 200 : 500
156+
response.status(status).send()
151157
}
152158
}
153159
}
@@ -167,7 +173,8 @@ class DDApiTest extends Specification {
167173
def agent = ratpack {
168174
handlers {
169175
put("v0.4/traces") {
170-
response.status(200).send()
176+
def status = request.contentLength > 0 ? 200 : 500
177+
response.status(status).send()
171178
}
172179
put("v0.4/services") {
173180
response.status(404).send()
@@ -193,7 +200,8 @@ class DDApiTest extends Specification {
193200
def agent = ratpack {
194201
handlers {
195202
put("v0.4/traces") {
196-
response.status(200).send()
203+
def status = request.contentLength > 0 ? 200 : 500
204+
response.status(status).send()
197205
}
198206
put("v0.4/services") {
199207
requestContentType.set(request.contentType)
@@ -243,11 +251,13 @@ class DDApiTest extends Specification {
243251
def agent = ratpack {
244252
handlers {
245253
put("v0.4/traces") {
246-
response.status(200).send('{"hello":"test"}')
254+
def status = request.contentLength > 0 ? 200 : 500
255+
response.status(status).send('{"hello":"test"}')
247256
}
248257
put("v0.4/services") {
249258
if (servicesAvailable) {
250-
response.status(200).send('{"service-response":"from-test"}')
259+
def status = request.contentLength > 0 ? 200 : 500
260+
response.status(status).send('{"service-response":"from-test"}')
251261
} else {
252262
response.status(404).send('{"service-response":"from-test"}')
253263
}
@@ -280,10 +290,12 @@ class DDApiTest extends Specification {
280290
def v3Agent = ratpack {
281291
handlers {
282292
put("v0.3/traces") {
283-
response.status(200).send()
293+
def status = request.contentLength > 0 ? 200 : 500
294+
response.status(status).send()
284295
}
285296
put("v0.3/services") {
286-
response.status(200).send()
297+
def status = request.contentLength > 0 ? 200 : 500
298+
response.status(status).send()
287299
}
288300
}
289301
}
@@ -307,21 +319,25 @@ class DDApiTest extends Specification {
307319
def agent = ratpack {
308320
handlers {
309321
put("v0.3/traces") {
310-
response.status(200).send()
322+
def status = request.contentLength > 0 ? 200 : 500
323+
response.status(status).send()
311324
}
312325
put("v0.3/services") {
313-
response.status(200).send()
326+
def status = request.contentLength > 0 ? 200 : 500
327+
response.status(status).send()
314328
}
315329
put("v0.4/traces") {
316330
Blocking.exec {
317331
Thread.sleep(delayTrace)
318-
response.status(200).send()
332+
def status = request.contentLength > 0 ? 200 : 500
333+
response.status(status).send()
319334
}
320335
}
321336
put("v0.4/services") {
322337
Blocking.exec {
323338
Thread.sleep(delayServices)
324-
response.status(200).send()
339+
def status = request.contentLength > 0 ? 200 : 500
340+
response.status(status).send()
325341
}
326342
}
327343
}

0 commit comments

Comments
 (0)