Some refactoring in tests
This commit is contained in:
parent
0f5dd2413c
commit
3ab63e7005
|
@ -51,7 +51,7 @@ dependencies {
|
|||
// 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: '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.jboss.resteasy', name: 'resteasy-client', version: '3.0.26.Final'
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package datadog.trace.instrumentation.jaxrs.v1;
|
||||
|
||||
import io.opentracing.propagation.TextMap;
|
||||
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
public final class InjectAdapter implements TextMap {
|
||||
private final MultivaluedMap<String, Object> map;
|
||||
|
|
|
@ -3,11 +3,9 @@ package datadog.trace.instrumentation.jaxrs.v1;
|
|||
import com.sun.jersey.api.client.ClientRequest;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import datadog.trace.agent.decorator.HttpClientDecorator;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class JaxRsClientV1Decorator
|
||||
extends HttpClientDecorator<ClientRequest, ClientResponse> {
|
||||
public class JaxRsClientV1Decorator extends HttpClientDecorator<ClientRequest, ClientResponse> {
|
||||
public static final JaxRsClientV1Decorator DECORATE = new JaxRsClientV1Decorator();
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package datadog.trace.instrumentation.jaxrs.v1;
|
||||
|
||||
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.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;
|
||||
|
@ -15,13 +17,11 @@ import io.opentracing.Scope;
|
|||
import io.opentracing.Span;
|
||||
import io.opentracing.propagation.Format;
|
||||
import io.opentracing.util.GlobalTracer;
|
||||
import java.util.Map;
|
||||
import net.bytebuddy.asm.Advice;
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import static java.util.Collections.singletonMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@AutoService(Instrumenter.class)
|
||||
public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
|
||||
|
@ -50,36 +50,40 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
|
|||
|
||||
@Override
|
||||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
|
||||
System.out.println("######### REGISTERING");
|
||||
return singletonMap(
|
||||
named("handle")
|
||||
.and(takesArgument(2, named("com.sun.jersey.api.client.ClientRequest")))
|
||||
.and(returns(named("com.sun.jersey.api.client.ClientResponse"))),
|
||||
HandleAdvice.class.getName()
|
||||
);
|
||||
.and(takesArgument(0, safeHasSuperType(named("com.sun.jersey.api.client.ClientRequest"))))
|
||||
.and(returns(safeHasSuperType(named("com.sun.jersey.api.client.ClientResponse")))),
|
||||
HandleAdvice.class.getName());
|
||||
}
|
||||
|
||||
public static class HandleAdvice {
|
||||
|
||||
@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.
|
||||
boolean isRootClientHandler = null == request.getProperties().get("dd.span");
|
||||
if (isRootClientHandler) {
|
||||
final Span span =
|
||||
GlobalTracer.get()
|
||||
.buildSpan("jax-rs.client.call")
|
||||
.withTag(DDTags.RESOURCE_NAME, request.getMethod() + " jax-rs.client.call")
|
||||
.start();
|
||||
GlobalTracer.get()
|
||||
.buildSpan("jax-rs.client.call")
|
||||
.withTag(DDTags.RESOURCE_NAME, request.getMethod() + " jax-rs.client.call")
|
||||
.start();
|
||||
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
|
||||
DECORATE.afterStart(span);
|
||||
DECORATE.onRequest(span, request);
|
||||
|
||||
GlobalTracer.get()
|
||||
.inject(
|
||||
span.context(),
|
||||
Format.Builtin.HTTP_HEADERS,
|
||||
new InjectAdapter(request.getHeaders()));
|
||||
.inject(
|
||||
span.context(),
|
||||
Format.Builtin.HTTP_HEADERS,
|
||||
new InjectAdapter(request.getHeaders()));
|
||||
|
||||
request.getProperties().put("dd.span", span);
|
||||
request.getProperties().put("dd.root.handler.hash", thisObj.hashCode());
|
||||
|
@ -88,7 +92,13 @@ public final class JaxRsClientV1Instrumentation extends Instrumenter.Default {
|
|||
}
|
||||
|
||||
@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");
|
||||
if (null == span) {
|
||||
return;
|
||||
|
|
|
@ -4,16 +4,16 @@ 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> {
|
||||
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)
|
||||
ClientResponse response = resource.method(method, ClientResponse.class, body)
|
||||
println "################## RESPOSNE #####################" + response
|
||||
callback?.call()
|
||||
|
||||
return response.status
|
||||
|
|
Loading…
Reference in New Issue