Pull out ratpack helper into seprate class to avoid copy-paste
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.RatpackUtils
import datadog.trace.api.DDSpanTypes
-import io.opentracing.Scope
-import io.opentracing.SpanContext
-import io.opentracing.propagation.Format
-import io.opentracing.propagation.TextMap
import io.opentracing.tag.Tags
-import io.opentracing.util.GlobalTracer
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.message.BasicHeader
-import ratpack.handling.Context
import spock.lang.Shared
import static datadog.trace.agent.test.TestUtils.runUnderTrace
@@ -24,23 +19,9 @@ class ApacheHttpClientTest extends AgentTestRunner {
def server = 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))
- Scope scope =
- GlobalTracer.get()
- .buildSpan("test-http-server")
- .asChildOf(extractedContext)
- .startActive(true)
- scope.close()
- }
+ RatpackUtils.handleDistributedRequest(context)
+ String msg = "<html><body><h1>Hello test.</h1>\n"
response.status(200).send(msg)
}
}
@@ -128,22 +109,4 @@ class ApacheHttpClientTest extends AgentTestRunner {
clientSpan.getTags()[Tags.PEER_PORT.getKey()] == server.getAddress().port
clientSpan.getTags()[Tags.SPAN_KIND.getKey()] == Tags.SPAN_KIND_CLIENT
}
-
- private static class RatpackResponseAdapter implements TextMap {
- final Context context
-
- RatpackResponseAdapter(Context context) {
- this.context = context
- }
-
- @Override
- void put(String key, String value) {
- context.response.set(key, value)
- }
-
- @Override
- Iterator<Map.Entry<String, String>> iterator() {
- return context.request.getHeaders().asMultiValueMap().entrySet().iterator()
- }
- }
}

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
import datadog.trace.agent.test.AgentTestRunner
+import datadog.trace.agent.test.RatpackUtils
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
-import io.opentracing.Scope
-import io.opentracing.SpanContext
-import io.opentracing.propagation.Format
-import io.opentracing.propagation.TextMap
import io.opentracing.tag.Tags
import io.opentracing.util.GlobalTracer
-import ratpack.handling.Context
import spock.lang.Shared
import static datadog.trace.agent.test.ListWriterAssert.assertTraces
@@ -28,21 +24,7 @@ class HttpUrlConnectionTest extends AgentTestRunner {
def server = ratpack {
handlers {
all {
- 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))
- Scope scope =
- GlobalTracer.get()
- .buildSpan("test-http-server")
- .asChildOf((SpanContext) extractedContext)
- .startActive(true)
- scope.close()
- }
+ RatpackUtils.handleDistributedRequest(context)
response.status(STATUS)
// Ratpack seems to be sending body with HEAD requests - RFC specifically forbids this.
@@ -442,22 +424,4 @@ class HttpUrlConnectionTest extends AgentTestRunner {
}
}
}
-
- private static class RatpackResponseAdapter implements TextMap {
- final Context context
-
- RatpackResponseAdapter(Context context) {
- this.context = context
- }
-
- @Override
- void put(String key, String value) {
- context.response.set(key, value)
- }
-
- @Override
- Iterator<Map.Entry<String, String>> iterator() {
- return context.request.getHeaders().asMultiValueMap().entrySet().iterator()
- }
- }
}

ADD
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/RatpackUtils.groovy
+package datadog.trace.agent.test
+
+import io.opentracing.Scope
+import io.opentracing.SpanContext
+import io.opentracing.propagation.Format
+import io.opentracing.propagation.TextMap
+import io.opentracing.util.GlobalTracer
+import ratpack.handling.Context
+
+class RatpackUtils {
+
+ static handleDistributedRequest(Context context) {
+ 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))
+ Scope scope =
+ GlobalTracer.get()
+ .buildSpan("test-http-server")
+ .asChildOf(extractedContext)
+ .startActive(true)
+ scope.close()
+ }
+ }
+
+ private static class RatpackResponseAdapter implements TextMap {
+ final Context context
+
+ RatpackResponseAdapter(Context context) {
+ this.context = context
+ }
+
+ @Override
+ void put(String key, String value) {
+ context.response.set(key, value)
+ }
+
+ @Override
+ Iterator<Map.Entry<String, String>> iterator() {
+ return context.request.getHeaders().asMultiValueMap().entrySet().iterator()
+ }
+ }
+}

MODIFY
dd-java-agent/testing/testing.gradle
dd-java-agent/testing/testing.gradle
@@ -5,8 +5,8 @@ minimumInstructionCoverage = 0.6
excludedClassesConverage += [
'datadog.trace.agent.test.*Assert',
'datadog.trace.agent.test.AgentTestRunner.ErrorCountingListener',
- 'datadog.trace.agent.test.TestUtils',
- 'datadog.trace.agent.test.OkHttpUtils'
+ 'datadog.trace.agent.test.*Utils',
+ 'datadog.trace.agent.test.*Utils.*'
]
dependencies {
@@ -19,6 +19,8 @@ dependencies {
compile deps.guava
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.10.0'
+ // Note: this should be the same version as in java.gradle
+ compileOnly group: 'io.ratpack', name: 'ratpack-groovy-test', version: '1.4.6'
compile project(':dd-trace-ot')
compile project(':dd-java-agent:agent-tooling')

Pull out ratpack helper into seprate class to avoid copy-paste