Some refactoring in tests

This commit is contained in:
Luca Abbati 2019-07-10 08:43:31 -04:00
parent 0f5dd2413c
commit 3ab63e7005
No known key found for this signature in database
GPG Key ID: 74DBB952D9BA17F2
5 changed files with 34 additions and 27 deletions

View File

@ -51,7 +51,7 @@ dependencies {
// testCompile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.3' // testCompile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.3'
// latestDepTestCompile group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '2.27' // latestDepTestCompile group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '2.27'
latestDepTestCompile group: 'com.sun.jersey', name: 'jersey-client', version: jerseyClientVersion latestDepTestCompile group: 'com.sun.jersey', name: 'jersey-client', version: '1.19.4'
// latestDepTestCompile group: 'org.apache.cxf', name: 'cxf-rt-rs-client', version: '3.2.6' // latestDepTestCompile group: 'org.apache.cxf', name: 'cxf-rt-rs-client', version: '3.2.6'
// latestDepTestCompile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.0.26.Final' // latestDepTestCompile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.0.26.Final'
} }

View File

@ -1,10 +1,9 @@
package datadog.trace.instrumentation.jaxrs.v1; package datadog.trace.instrumentation.jaxrs.v1;
import io.opentracing.propagation.TextMap; import io.opentracing.propagation.TextMap;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
public final class InjectAdapter implements TextMap { public final class InjectAdapter implements TextMap {
private final MultivaluedMap<String, Object> map; private final MultivaluedMap<String, Object> map;

View File

@ -3,11 +3,9 @@ package datadog.trace.instrumentation.jaxrs.v1;
import com.sun.jersey.api.client.ClientRequest; import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse;
import datadog.trace.agent.decorator.HttpClientDecorator; import datadog.trace.agent.decorator.HttpClientDecorator;
import java.net.URI; import java.net.URI;
public class JaxRsClientV1Decorator public class JaxRsClientV1Decorator extends HttpClientDecorator<ClientRequest, ClientResponse> {
extends HttpClientDecorator<ClientRequest, ClientResponse> {
public static final JaxRsClientV1Decorator DECORATE = new JaxRsClientV1Decorator(); public static final JaxRsClientV1Decorator DECORATE = new JaxRsClientV1Decorator();
@Override @Override

View File

@ -1,10 +1,12 @@
package datadog.trace.instrumentation.jaxrs.v1; package datadog.trace.instrumentation.jaxrs.v1;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator.DECORATE;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns; import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator.DECORATE;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import com.sun.jersey.api.client.ClientHandler; import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.ClientRequest; import com.sun.jersey.api.client.ClientRequest;
@ -15,13 +17,11 @@ import io.opentracing.Scope;
import io.opentracing.Span; import io.opentracing.Span;
import io.opentracing.propagation.Format; import io.opentracing.propagation.Format;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;
import java.util.Map;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
import static java.util.Collections.singletonMap;
import java.util.Map;
@AutoService(Instrumenter.class) @AutoService(Instrumenter.class)
public final class JaxRsClientV1Instrumentation extends Instrumenter.Default { public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
@ -50,18 +50,22 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
@Override @Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
System.out.println("######### REGISTERING");
return singletonMap( return singletonMap(
named("handle") named("handle")
.and(takesArgument(2, named("com.sun.jersey.api.client.ClientRequest"))) .and(takesArgument(0, safeHasSuperType(named("com.sun.jersey.api.client.ClientRequest"))))
.and(returns(named("com.sun.jersey.api.client.ClientResponse"))), .and(returns(safeHasSuperType(named("com.sun.jersey.api.client.ClientResponse")))),
HandleAdvice.class.getName() HandleAdvice.class.getName());
);
} }
public static class HandleAdvice { public static class HandleAdvice {
@Advice.OnMethodEnter @Advice.OnMethodEnter
public static void onEnter(@Advice.Argument(value = 1) final ClientRequest request, ClientHandler thisObj) { public static void onEnter(
@Advice.Argument(value = 0) final ClientRequest request,
@Advice.This final ClientHandler thisObj) {
System.out.println("############ ON HANDLE ENTER");
// WARNING: this might be a chain...so we only have to trace the first in the chain. // 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"); boolean isRootClientHandler = null == request.getProperties().get("dd.span");
@ -88,7 +92,13 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
} }
@Advice.OnMethodExit @Advice.OnMethodExit
public static void onExit(@Advice.Argument(value = 1) final ClientRequest request, ClientResponse response, ClientHandler thisObj) { public static void onExit(
@Advice.Argument(value = 0) final ClientRequest request,
@Advice.Return final ClientResponse response,
@Advice.This final ClientHandler thisObj) {
System.out.println("############ ON HANDLE ENTER");
Span span = (Span) request.getProperties().get("dd.span"); Span span = (Span) request.getProperties().get("dd.span");
if (null == span) { if (null == span) {
return; return;

View File

@ -4,16 +4,16 @@ import com.sun.jersey.client.impl.ClientRequestImpl
import datadog.trace.agent.test.base.HttpClientTest import datadog.trace.agent.test.base.HttpClientTest
import datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator import datadog.trace.instrumentation.jaxrs.v1.JaxRsClientV1Decorator
abstract class JaxRsClientV1Test extends HttpClientTest<JaxRsClientV1Decorator> { class JaxRsClientV1Test extends HttpClientTest<JaxRsClientV1Decorator> {
@Override @Override
int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) { int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) {
Client client = Client.create() Client client = Client.create()
def resource = client.resource(uri) def resource = client.resource(uri)
headers.each { resource.header(it.key, it.value) } headers.each { resource.header(it.key, it.value) }
def body = BODY_METHODS.contains(method) ? new ClientRequestImpl(uri, method) : null def body = BODY_METHODS.contains(method) ? new ClientRequestImpl(uri, method) : null
ClientResponse response = resource.method(method, body) ClientResponse response = resource.method(method, ClientResponse.class, body)
println "################## RESPOSNE #####################" + response
callback?.call() callback?.call()
return response.status return response.status