Use ratpack to implemented embedded http test util
MODIFY
dd-java-agent-ittests/dd-java-agent-ittests.gradle
dd-java-agent-ittests/dd-java-agent-ittests.gradle
@@ -9,9 +9,6 @@ dependencies {
testCompile deps.testLogging
- // simple, lightweight http server for testing
- testCompile group: 'org.nanohttpd', name: 'nanohttpd', version: '2.3.1'
-
testCompile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2'
testCompile group: 'org.mongodb', name: 'mongodb-driver-async', version: '3.4.2'
// run embeded mongodb for integration testing

ADD
dd-java-agent-ittests/src/test/groovy/com/datadoghq/agent/integration/TestHttpServer.groovy
+package com.datadoghq.agent.integration
+
+import io.opentracing.ActiveSpan;
+import io.opentracing.SpanContext;
+import io.opentracing.propagation.Format;
+import io.opentracing.propagation.TextMap;
+import io.opentracing.util.GlobalTracer;
+
+import ratpack.http.MutableHeaders
+import ratpack.http.Response
+import ratpack.groovy.test.embed.GroovyEmbeddedApp
+import ratpack.handling.Context
+
+import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack
+
+/**
+ * A simple http server used for testing.<br>
+ * Binds locally to {@link #PORT}.
+ *
+ * <p>To start: {@link #startServer()}<br>
+ * to stop: {@link #stopServer()}
+ */
+class TestHttpServer {
+ /**
+ * By default the test server will mock a datadog traced server. Set this header to a value of
+ * false to disable.
+ */
+ public static final String IS_DD_SERVER = "is-dd-server"
+
+ private static GroovyEmbeddedApp INSTANCE = null
+ private static int PORT = 0
+
+ static int getPort() {
+ if (PORT == 0) {
+ throw new RuntimeException("Server not started")
+ }
+ return PORT;
+ }
+
+ /**
+ * Start the server. Has no effect if already started.
+ *
+ * @throws IOException
+ */
+ static synchronized def startServer() {
+ if (null == INSTANCE) {
+ INSTANCE = ratpack {
+ handlers {
+ get {
+ String msg = "<html><body><h1>Hello test.</h1>\n"
+ boolean isDDServer = true
+ if (context.request.getHeaders().contains(IS_DD_SERVER)) {
+ isDDServer = Boolean.parseBoolean(context.request.getHeaders().get(IS_DD_SERVER))
+ }
+ if (isDDServer) {
+ final SpanContext extractedContext =
+ GlobalTracer.get()
+ .extract(Format.Builtin.HTTP_HEADERS, new RatpackResponseAdapter(context))
+ ActiveSpan span =
+ GlobalTracer.get()
+ .buildSpan("test-http-server")
+ .asChildOf(extractedContext)
+ .startActive()
+ span.deactivate()
+ }
+
+ response.status(200).send(msg)
+ }
+ }
+ }
+ PORT = INSTANCE.address.port
+ }
+ }
+
+ /** Stop the server. Has no effect if already stopped. */
+ static synchronized void stopServer() {
+ if (null != INSTANCE) {
+ INSTANCE.close()
+ INSTANCE = null
+ PORT = 0
+ }
+ }
+
+ private static class RatpackResponseAdapter implements TextMap {
+ final Context context;
+
+ public RatpackResponseAdapter(Context context) {
+ this.context = context
+ }
+
+ @Override
+ public void put(String key, String value) {
+ context.response.set(key, value)
+ }
+
+ @Override
+ public Iterator<Map.Entry<String, String>> iterator() {
+ return context.request.getHeaders().asMultiValueMap().entrySet().iterator()
+ }
+ }
+}

MODIFY
dd-java-agent-ittests/src/test/groovy/com/datadoghq/agent/integration/httpclient/ApacheHttpClientTest.groovy
dd-java-agent-ittests/src/test/groovy/com/datadoghq/agent/integration/httpclient/ApacheHttpClientTest.groovy
@@ -9,6 +9,7 @@ import io.opentracing.tag.Tags
import java.net.URI
import java.util.List
import org.apache.http.HttpResponse
+import org.apache.http.message.BasicHeader
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
@@ -46,7 +47,7 @@ class ApacheHttpClientTest extends Specification {
public void run() {
try {
HttpResponse response =
- client.execute(new HttpGet(new URI("http://localhost:" + TestHttpServer.PORT)))
+ client.execute(new HttpGet(new URI("http://localhost:" + TestHttpServer.getPort())))
assert response.getStatusLine().getStatusCode() == 200
} catch (Exception e) {
e.printStackTrace()
@@ -72,9 +73,9 @@ class ApacheHttpClientTest extends Specification {
clientSpan.getOperationName() == "GET"
clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
- clientSpan.getTags()[Tags.HTTP_URL.getKey()] == "http://localhost:8089"
+ clientSpan.getTags()[Tags.HTTP_URL.getKey()] == "http://localhost:" + TestHttpServer.getPort()
clientSpan.getTags()[Tags.PEER_HOSTNAME.getKey()] == "localhost"
- clientSpan.getTags()[Tags.PEER_PORT.getKey()] == 8089
+ clientSpan.getTags()[Tags.PEER_PORT.getKey()] == TestHttpServer.getPort()
clientSpan.getTags()[Tags.SPAN_KIND.getKey()] == Tags.SPAN_KIND_CLIENT
// client trace propagates to server
@@ -95,12 +96,10 @@ class ApacheHttpClientTest extends Specification {
@Override
public void run() {
try {
- HttpResponse response =
- client.execute(new HttpGet(new URI("http://localhost:"
- + TestHttpServer.PORT
- + "?"
- + TestHttpServer.IS_DD_SERVER
- + "=false")))
+ HttpGet request = new HttpGet(new URI("http://localhost:"
+ + TestHttpServer.getPort()))
+ request.addHeader(new BasicHeader(TestHttpServer.IS_DD_SERVER, "false"))
+ HttpResponse response = client.execute(request)
assert response.getStatusLine().getStatusCode() == 200
} catch (Exception e) {
e.printStackTrace()
@@ -123,9 +122,9 @@ class ApacheHttpClientTest extends Specification {
clientSpan.getOperationName() == "GET"
clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
- clientSpan.getTags()[Tags.HTTP_URL.getKey()] == "http://localhost:8089?is-dd-server=false"
+ clientSpan.getTags()[Tags.HTTP_URL.getKey()] == "http://localhost:" + TestHttpServer.getPort()
clientSpan.getTags()[Tags.PEER_HOSTNAME.getKey()] == "localhost"
- clientSpan.getTags()[Tags.PEER_PORT.getKey()] == 8089
+ clientSpan.getTags()[Tags.PEER_PORT.getKey()] == TestHttpServer.getPort()
clientSpan.getTags()[Tags.SPAN_KIND.getKey()] == Tags.SPAN_KIND_CLIENT
}

DELETE
dd-java-agent-ittests/src/test/java/com/datadoghq/agent/integration/TestHttpServer.java
-package com.datadoghq.agent.integration;
-
-import fi.iki.elonen.NanoHTTPD;
-import io.opentracing.ActiveSpan;
-import io.opentracing.SpanContext;
-import io.opentracing.propagation.Format;
-import io.opentracing.propagation.TextMap;
-import io.opentracing.util.GlobalTracer;
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-/**
- * A simple http server used for testing.<br>
- * Binds locally to {@link #PORT}.
- *
- * <p>To start: {@link #startServer()}<br>
- * to stop: {@link #stopServer()}
- */
-public class TestHttpServer extends NanoHTTPD {
- /** Port the test server will bind. */
- public static final int PORT = 8089;
- /**
- * By default the test server will mock a datadog traced server. Set this header to a value of
- * false to disable.
- */
- public static final String IS_DD_SERVER = "is-dd-server";
-
- private static TestHttpServer INSTANCE = null;
-
- /**
- * Start the server. Has no effect if already started.
- *
- * @throws IOException
- */
- public static synchronized void startServer() throws IOException {
- if (null == INSTANCE) {
- INSTANCE = new TestHttpServer();
- }
- }
-
- /** Stop the server. Has no effect if already stopped. */
- public static synchronized void stopServer() {
- if (null != INSTANCE) {
- INSTANCE.stop();
- INSTANCE = null;
- }
- }
-
- private TestHttpServer() throws IOException {
- super(PORT);
- start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
- }
-
- @Override
- public Response serve(IHTTPSession session) {
- String msg = "<html><body><h1>Hello test.</h1>\n";
- Map<String, List<String>> params = session.getParameters();
- boolean isDDServer = true;
- if (params.containsKey(IS_DD_SERVER)) {
- isDDServer = Boolean.parseBoolean(params.get(IS_DD_SERVER).get(0));
- }
- if (isDDServer) {
- final SpanContext extractedContext =
- GlobalTracer.get()
- .extract(Format.Builtin.HTTP_HEADERS, new HttpSessionInjectAdapter(session));
- ActiveSpan span =
- GlobalTracer.get()
- .buildSpan("test-http-server")
- .asChildOf(extractedContext)
- .startActive();
- span.deactivate();
- }
- return newFixedLengthResponse(
- NanoHTTPD.Response.Status.OK, "text/html", msg + "</body></html>\n");
- }
-
- private class HttpSessionInjectAdapter implements TextMap {
-
- private IHTTPSession session;
-
- public HttpSessionInjectAdapter(IHTTPSession httpRequest) {
- this.session = httpRequest;
- }
-
- @Override
- public void put(String key, String value) {
- session.getHeaders().put(key, value);
- }
-
- @Override
- public Iterator<Map.Entry<String, String>> iterator() {
- return session.getHeaders().entrySet().iterator();
- }
- }
-}

Use ratpack to implemented embedded http test util