Improve test coverage and reduce duplication
MODIFY
dd-java-agent/instrumentation/akka-http-10.0/src/test/groovy/AkkaHttpClientInstrumentationTest.groovy
dd-java-agent/instrumentation/akka-http-10.0/src/test/groovy/AkkaHttpClientInstrumentationTest.groovy
@@ -74,16 +74,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
}
assertTraces(TEST_WRITER, 2) {
- trace(0, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[1][0])
- errored false
- tags {
- defaultTags()
- }
- }
- }
+ server.distributedRequestTrace(it, 0, TEST_WRITER[1][0])
trace(1, 1) {
span(0) {
parent()
@@ -203,16 +194,7 @@ class AkkaHttpClientInstrumentationTest extends AgentTestRunner {
}
assertTraces(TEST_WRITER, 2) {
- trace(0, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[1][0])
- errored false
- tags {
- defaultTags()
- }
- }
- }
+ server.distributedRequestTrace(it, 0, TEST_WRITER[1][0])
trace(1, 1) {
span(0) {
parent()

MODIFY
dd-java-agent/instrumentation/apache-httpclient-4.3/src/test/groovy/ApacheHttpClientTest.groovy
dd-java-agent/instrumentation/apache-httpclient-4.3/src/test/groovy/ApacheHttpClientTest.groovy
import datadog.opentracing.DDSpan
import datadog.trace.agent.test.AgentTestRunner
-import datadog.trace.agent.test.asserts.ListWriterAssert
import datadog.trace.agent.test.asserts.TraceAssert
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
@@ -59,7 +58,7 @@ class ApacheHttpClientTest extends AgentTestRunner {
response.getStatusLine().getStatusCode() == 200
// one trace on the server, one trace on the client
assertTraces(TEST_WRITER, 2) {
- serverTrace(it, 0, TEST_WRITER[1][1])
+ server.distributedRequestTrace(it, 0, TEST_WRITER[1][1])
trace(1, 2) {
clientParentSpan(it, 0)
successClientSpan(it, 1, span(0))
@@ -82,8 +81,8 @@ class ApacheHttpClientTest extends AgentTestRunner {
response.getStatusLine().getStatusCode() == 200
// two traces on the server, one trace on the client
assertTraces(TEST_WRITER, 3) {
- serverTrace(it, 0, TEST_WRITER[2][2])
- serverTrace(it, 1, TEST_WRITER[2][1])
+ server.distributedRequestTrace(it, 0, TEST_WRITER[2][2])
+ server.distributedRequestTrace(it, 1, TEST_WRITER[2][1])
trace(2, 3) {
clientParentSpan(it, 0)
successClientSpan(it, 1, span(0))
@@ -106,8 +105,8 @@ class ApacheHttpClientTest extends AgentTestRunner {
response.getStatusLine().getStatusCode() == 200
// two traces on the server, one trace on the client
assertTraces(TEST_WRITER, 3) {
- serverTrace(it, 0, TEST_WRITER[2][2])
- serverTrace(it, 1, TEST_WRITER[2][1])
+ server.distributedRequestTrace(it, 0, TEST_WRITER[2][2])
+ server.distributedRequestTrace(it, 1, TEST_WRITER[2][1])
trace(2, 3) {
clientParentSpan(it, 0)
successClientSpan(it, 1, span(0))
@@ -131,8 +130,8 @@ class ApacheHttpClientTest extends AgentTestRunner {
def exception = thrown(ClientProtocolException)
// two traces on the server, one trace on the client
assertTraces(TEST_WRITER, 3) {
- serverTrace(it, 0, TEST_WRITER[2][2])
- serverTrace(it, 1, TEST_WRITER[2][1])
+ server.distributedRequestTrace(it, 0, TEST_WRITER[2][2])
+ server.distributedRequestTrace(it, 1, TEST_WRITER[2][1])
trace(2, 3) {
clientParentSpan(it, 0, exception)
redirectClientSpan(it, 1, span(0))
@@ -160,21 +159,6 @@ class ApacheHttpClientTest extends AgentTestRunner {
}
}
- def serverTrace(ListWriterAssert writer, int index, DDSpan parent) {
- writer.trace(index, 1) {
- span(0) {
- childOf parent
- serviceName "unnamed-java-app"
- operationName "test-http-server"
- resourceName "test-http-server"
- errored false
- tags {
- defaultTags()
- }
- }
- }
- }
-
def clientParentSpan(TraceAssert trace, int index, Throwable exception = null) {
trace.span(index) {
parent()

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/HttpUrlConnectionTest.groovy
dd-java-agent/instrumentation/http-url-connection/src/test/groovy/HttpUrlConnectionTest.groovy
@@ -57,26 +57,8 @@ class HttpUrlConnectionTest extends AgentTestRunner {
expect:
assertTraces(TEST_WRITER, 3) {
- trace(0, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[2][2])
- errored false
- tags {
- defaultTags()
- }
- }
- }
- trace(1, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[2][1])
- errored false
- tags {
- defaultTags()
- }
- }
- }
+ server.distributedRequestTrace(it, 0, TEST_WRITER[2][2])
+ server.distributedRequestTrace(it, 1, TEST_WRITER[2][1])
trace(2, 3) {
span(0) {
operationName "someTrace"
@@ -315,16 +297,7 @@ class HttpUrlConnectionTest extends AgentTestRunner {
expect:
assertTraces(TEST_WRITER, 2) {
- trace(0, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[1][1])
- errored false
- tags {
- defaultTags()
- }
- }
- }
+ server.distributedRequestTrace(it, 0, TEST_WRITER[1][1])
trace(1, 3) {
span(0) {
operationName "someTrace"
@@ -421,16 +394,7 @@ class HttpUrlConnectionTest extends AgentTestRunner {
expect:
assertTraces(TEST_WRITER, 2) {
- trace(0, 1) {
- span(0) {
- operationName "test-http-server"
- childOf(TEST_WRITER[1][2])
- errored false
- tags {
- defaultTags()
- }
- }
- }
+ server.distributedRequestTrace(it, 0, TEST_WRITER[1][2])
trace(1, 4) {
span(0) {
operationName "someTrace"

MODIFY
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/server/http/TestHttpServer.groovy
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/server/http/TestHttpServer.groovy
package datadog.trace.agent.test.server.http
+import datadog.opentracing.DDSpan
import datadog.trace.agent.test.TestUtils
+import datadog.trace.agent.test.asserts.ListWriterAssert
import io.opentracing.SpanContext
+import io.opentracing.Tracer
import io.opentracing.propagation.Format
import io.opentracing.util.GlobalTracer
import org.eclipse.jetty.http.HttpMethods
@@ -35,6 +38,8 @@ class TestHttpServer implements AutoCloseable {
private final Server internalServer
private HandlersSpec handlers
+ public Tracer tracer = GlobalTracer.get()
+
final URI address
private final AtomicReference<HandlerApi.RequestApi> last = new AtomicReference<>()
@@ -88,6 +93,23 @@ class TestHttpServer implements AutoCloseable {
clone(handlers)
}
+ static distributedRequestTrace(ListWriterAssert traces, int index, DDSpan parentSpan = null) {
+ traces.trace(index, 1) {
+ span(0) {
+ operationName "test-http-server"
+ errored false
+ if (parentSpan == null) {
+ parent()
+ } else {
+ childOf(parentSpan)
+ }
+ tags {
+ defaultTags()
+ }
+ }
+ }
+ }
+
private class HandlersSpec {
List<Handler> configured = []
@@ -180,7 +202,7 @@ class TestHttpServer implements AutoCloseable {
}
}
- static class HandlerApi {
+ class HandlerApi {
private final Request req
private final HttpServletResponse resp
@@ -210,9 +232,8 @@ class TestHttpServer implements AutoCloseable {
}
if (isDDServer) {
final SpanContext extractedContext =
- GlobalTracer.get()
- .extract(Format.Builtin.HTTP_HEADERS, new HttpServletRequestExtractAdapter(req))
- def builder = GlobalTracer.get()
+ tracer.extract(Format.Builtin.HTTP_HEADERS, new HttpServletRequestExtractAdapter(req))
+ def builder = tracer
.buildSpan("test-http-server")
if (extractedContext != null) {
builder.asChildOf(extractedContext)
@@ -263,7 +284,7 @@ class TestHttpServer implements AutoCloseable {
ResponseApi status(int status) {
this.status = status
- this
+ return this
}
void send() {

RENAME
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/server/http/HttpServletRequestExtractAdapter.java
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/server/http/HttpServletRequestExtractAdapter.java
MODIFY
dd-java-agent/testing/src/test/groovy/server/ServerTest.groovy
dd-java-agent/testing/src/test/groovy/server/ServerTest.groovy
package server
+import datadog.opentracing.DDTracer
import datadog.trace.agent.test.utils.OkHttpUtils
+import datadog.trace.common.writer.ListWriter
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import spock.lang.Shared
import spock.lang.Specification
+import static datadog.trace.agent.test.asserts.ListWriterAssert.assertTraces
import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
class ServerTest extends Specification {
@@ -84,6 +87,9 @@ class ServerTest extends Specification {
post("/post") {
response.send("/post response")
}
+ put("/put") {
+ response.send("/put response")
+ }
prefix("/base") {
response.send("${request.path} response")
}
@@ -116,6 +122,7 @@ class ServerTest extends Specification {
method | path
"get" | "get"
"post" | "post"
+ "put" | "put"
"get" | "base"
"post" | "base"
"get" | "base/get"
@@ -282,6 +289,39 @@ class ServerTest extends Specification {
server.stop()
}
+ def "server handles distributed request"() {
+ setup:
+ def server = httpServer {
+ handlers {
+ all {
+ handleDistributedRequest()
+ response.send("done")
+ }
+ }
+ }
+ def writer = new ListWriter()
+ server.tracer = new DDTracer(writer)
+
+ when:
+ def request = new Request.Builder()
+ .url("$server.address")
+ .get()
+ .build()
+
+ def response = client.newCall(request).execute()
+
+ then:
+ response.code() == 200
+ response.body().string().trim() == "done"
+
+ assertTraces(writer, 1) {
+ server.distributedRequestTrace(it, 0)
+ }
+
+ cleanup:
+ server.stop()
+ }
+
def body() {
return new MultipartBody.Builder().addFormDataPart("key", "value").build()
}

MODIFY
dd-java-agent/testing/testing.gradle
dd-java-agent/testing/testing.gradle
@@ -3,10 +3,9 @@ apply from: "${rootDir}/gradle/java.gradle"
minimumBranchCoverage = 0.5
minimumInstructionCoverage = 0.6
excludedClassesConverage += [
- 'datadog.trace.agent.test.*Assert',
+ 'datadog.trace.agent.test.asserts.*Assert',
'datadog.trace.agent.test.AgentTestRunner.ErrorCountingListener',
- 'datadog.trace.agent.test.*Utils',
- 'datadog.trace.agent.test.*Utils.*'
+ 'datadog.trace.agent.test.OkHttpUtils'
]
dependencies {

Improve test coverage and reduce duplication