Add tests for Jersey V1 client
MODIFY
dd-java-agent/instrumentation/jax-rs-client/jax-rs-client-1/src/main/java/datadog/trace/instrumentation/jaxrs/v1/JaxRsClientV1Instrumentation.java
dd-java-agent/instrumentation/jax-rs-client/jax-rs-client-1/src/main/java/datadog/trace/instrumentation/jaxrs/v1/JaxRsClientV1Instrumentation.java
@@ -6,6 +6,7 @@ import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator.DECORATE;
import com.google.auto.service.AutoService;
+import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import datadog.trace.agent.tooling.Instrumenter;
@@ -60,10 +61,11 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
public static class HandleAdvice {
@Advice.OnMethodEnter
- public static Span onEnter(@Advice.Argument(value = 1) final ClientRequest request) {
+ public static void onEnter(@Advice.Argument(value = 1) final ClientRequest request, ClientHandler thisObj) {
// WARNING: this might be a chain...so we only have to trace the first in the chain.
-
+ boolean isRootClientHandler = null == request.getProperties().get("dd.span");
+ if (isRootClientHandler) {
final Span span =
GlobalTracer.get()
.buildSpan("jax-rs.client.call")
@@ -80,17 +82,26 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
new InjectAdapter(request.getHeaders()));
request.getProperties().put("dd.span", span);
+ request.getProperties().put("dd.root.handler.hash", thisObj.hashCode());
+ }
}
-
- return span;
}
@Advice.OnMethodExit
- public static void onExit(@Advice.Argument(value = 1) final ClientRequest request, ClientResponse response) {
+ public static void onExit(@Advice.Argument(value = 1) final ClientRequest request, ClientResponse response, ClientHandler thisObj) {
Span span = (Span) request.getProperties().get("dd.span");
+ if (null == span) {
+ return;
+ }
+
+ request.getProperties().get("dd.root.handler.hash");
+
+ if (thisObj.hashCode() == (Integer) request.getProperties().get("dd.root.handler.hash")) {
+ // this is the root span, closing all the things
DECORATE.onResponse(span, response);
DECORATE.beforeFinish(span);
span.finish();
}
}
}
+}

ADD
dd-java-agent/instrumentation/jax-rs-client/jax-rs-client-1/src/test/groovy/JaxRsClientV1Test.groovy
+import com.sun.jersey.api.client.Client
+import com.sun.jersey.api.client.ClientResponse
+import com.sun.jersey.client.impl.ClientRequestImpl
+import datadog.trace.agent.test.base.HttpClientTest
+import datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator
+
+abstract class JaxRsClientV1Test extends HttpClientTest<JaxRsClientV1Decorator> {
+
+ @Override
+ int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) {
+
+ Client client = Client.create()
+ def resource = client.resource(uri)
+ headers.each { resource.header(it.key, it.value) }
+ def body = BODY_METHODS.contains(method) ? new ClientRequestImpl(uri, method) : null
+ ClientResponse response = resource.method(method, body)
+ callback?.call()
+
+ return response.status
+ }
+
+ @Override
+ JaxRsClientV1Decorator decorator() {
+ return JaxRsClientV1Decorator.DECORATE
+ }
+
+ @Override
+ String expectedOperationName() {
+ return "jax-rs.client.call"
+ }
+
+ boolean testRedirects() {
+ false
+ }
+}

Add tests for Jersey V1 client