Introduce a java 8 source set for Ratpack integration
MODIFY
dd-java-agent/instrumentation/ratpack-1.4/ratpack-1.4.gradle
dd-java-agent/instrumentation/ratpack-1.4/ratpack-1.4.gradle
@@ -9,37 +9,42 @@ versionScan {
]
}
-if (JavaVersion.current().java8Compatible) {
apply from: "${rootDir}/gradle/java.gradle"
- // Ratpack only supports Java 1.8+
+/*
+Here we introduce a sourceSet for the java 8 code which needs to be compiled with a source and target of 1.8
+The instrumentation classes must be compiled with java 7 and do nothing when ratpack is not on the classpath. The
+java 8 classes are used lazily so there is no direct linking between the 1.7 and 1.8 bytecode.
+*/
+sourceSets {
+ main_java8 {
+ java.srcDirs "${project.projectDir}/src/main/java8"
+ }
+}
+
+compileMain_java8Java {
sourceCompatibility = 1.8
targetCompatibility = 1.8
+}
+
dependencies {
- compileOnly group: 'io.ratpack', name: 'ratpack-core', version: '1.4.0'
+ main_java8CompileOnly group: 'io.ratpack', name: 'ratpack-core', version: '1.4.0'
+
+ main_java8Compile project(':dd-trace-ot')
+ main_java8Compile project(':dd-java-agent:agent-tooling')
+
+ main_java8Compile deps.bytebuddy
+ main_java8Compile deps.opentracing
+ main_java8Compile deps.autoservice
- compile project(':dd-trace-ot')
- compile project(':dd-java-agent:agent-tooling')
+ compileOnly sourceSets.main_java8.compileClasspath
- compile deps.bytebuddy
- compile deps.opentracing
- compile deps.autoservice
+ compile sourceSets.main_java8.output
testCompile project(':dd-java-agent:testing')
testCompile group: 'io.ratpack', name: 'ratpack-test', version: '1.4.0'
testCompile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.6.0'
}
-}
-
-if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
- sourceSets {
- test {
- groovy {
- // These classes use Ratpack which requires Java 8. (Currently also incompatible with Java 9.)
- exclude '**/RatpackTest.groovy'
- }
- }
- }
-}
+testJava8Only += '**/RatpackTest.class'

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackHttpClientInstrumentation.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackHttpClientInstrumentation.java
package datadog.trace.instrumentation.ratpack;
-import static datadog.trace.instrumentation.ratpack.RatpackInstrumentation.ACTION_TYPE_DESCRIPTION;
-import static datadog.trace.instrumentation.ratpack.RatpackInstrumentation.CLASSLOADER_CONTAINS_RATPACK_1_4_OR_ABOVE;
-import static datadog.trace.instrumentation.ratpack.RatpackInstrumentation.EXEC_NAME;
-import static io.opentracing.log.Fields.ERROR_OBJECT;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -14,36 +10,24 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.HelperInjector;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Span;
-import io.opentracing.tag.Tags;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.ratpack.impl.RatpackHttpClientAdvice;
import java.net.URI;
-import java.util.Collections;
-import java.util.concurrent.atomic.AtomicReference;
import net.bytebuddy.agent.builder.AgentBuilder;
-import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
-import ratpack.exec.Promise;
-import ratpack.exec.Result;
-import ratpack.func.Action;
-import ratpack.http.client.ReceivedResponse;
-import ratpack.http.client.RequestSpec;
-import ratpack.http.client.StreamedResponse;
@AutoService(Instrumenter.class)
public final class RatpackHttpClientInstrumentation extends Instrumenter.Configurable {
private static final HelperInjector HTTP_CLIENT_HELPER_INJECTOR =
new HelperInjector(
- "datadog.trace.instrumentation.ratpack.RatpackHttpClientInstrumentation$RatpackHttpClientRequestAdvice",
- "datadog.trace.instrumentation.ratpack.RatpackHttpClientInstrumentation$RatpackHttpClientRequestStreamAdvice",
- "datadog.trace.instrumentation.ratpack.RatpackHttpClientInstrumentation$RatpackHttpGetAdvice",
- "datadog.trace.instrumentation.ratpack.RatpackHttpClientInstrumentation$GetRequestSpecAction");
+ "datadog.trace.instrumentation.ratpack.impl.RatpackHttpClientAdvice$RatpackHttpClientRequestAdvice",
+ "datadog.trace.instrumentation.ratpack.impl.RatpackHttpClientAdvice$RatpackHttpClientRequestStreamAdvice",
+ "datadog.trace.instrumentation.ratpack.impl.RatpackHttpClientAdvice$RatpackHttpGetAdvice");
public static final TypeDescription.ForLoadedType URI_TYPE_DESCRIPTION =
new TypeDescription.ForLoadedType(URI.class);
public RatpackHttpClientInstrumentation() {
- super(EXEC_NAME);
+ super(RatpackInstrumentation.EXEC_NAME);
}
@Override
@@ -57,153 +41,35 @@ public final class RatpackHttpClientInstrumentation extends Instrumenter.Configu
return agentBuilder
.type(
not(isInterface()).and(hasSuperType(named("ratpack.http.client.HttpClient"))),
- CLASSLOADER_CONTAINS_RATPACK_1_4_OR_ABOVE)
+ RatpackInstrumentation.CLASSLOADER_CONTAINS_RATPACK_1_4_OR_ABOVE)
.transform(HTTP_CLIENT_HELPER_INJECTOR)
.transform(
DDAdvice.create()
.advice(
named("request")
- .and(takesArguments(URI_TYPE_DESCRIPTION, ACTION_TYPE_DESCRIPTION)),
- RatpackHttpClientRequestAdvice.class.getName()))
+ .and(
+ takesArguments(
+ URI_TYPE_DESCRIPTION,
+ RatpackInstrumentation.ACTION_TYPE_DESCRIPTION)),
+ RatpackHttpClientAdvice.RatpackHttpClientRequestAdvice.class.getName()))
.transform(
DDAdvice.create()
.advice(
named("requestStream")
- .and(takesArguments(URI_TYPE_DESCRIPTION, ACTION_TYPE_DESCRIPTION)),
- RatpackHttpClientRequestStreamAdvice.class.getName()))
+ .and(
+ takesArguments(
+ URI_TYPE_DESCRIPTION,
+ RatpackInstrumentation.ACTION_TYPE_DESCRIPTION)),
+ RatpackHttpClientAdvice.RatpackHttpClientRequestStreamAdvice.class.getName()))
.transform(
DDAdvice.create()
.advice(
- named("get").and(takesArguments(URI_TYPE_DESCRIPTION, ACTION_TYPE_DESCRIPTION)),
- RatpackHttpGetAdvice.class.getName()))
+ named("get")
+ .and(
+ takesArguments(
+ URI_TYPE_DESCRIPTION,
+ RatpackInstrumentation.ACTION_TYPE_DESCRIPTION)),
+ RatpackHttpClientAdvice.RatpackHttpGetAdvice.class.getName()))
.asDecorator();
}
-
- public static class RequestAction implements Action<RequestSpec> {
-
- private final Action<? super RequestSpec> requestAction;
- private final AtomicReference<Span> spanRef;
-
- public RequestAction(Action<? super RequestSpec> requestAction, AtomicReference<Span> spanRef) {
- this.requestAction = requestAction;
- this.spanRef = spanRef;
- }
-
- @Override
- public void execute(RequestSpec requestSpec) throws Exception {
- requestAction.execute(
- new WrappedRequestSpec(
- requestSpec,
- GlobalTracer.get(),
- GlobalTracer.get().scopeManager().active(),
- spanRef));
- }
- }
-
- public static class ResponseAction implements Action<Result<ReceivedResponse>> {
- private final AtomicReference<Span> spanRef;
-
- public ResponseAction(AtomicReference<Span> spanRef) {
- this.spanRef = spanRef;
- }
-
- @Override
- public void execute(Result<ReceivedResponse> result) {
- Span span = spanRef.get();
- if (span == null) {
- return;
- }
- span.finish();
- if (result.isError()) {
- Tags.ERROR.set(span, true);
- span.log(Collections.singletonMap(ERROR_OBJECT, result.getThrowable()));
- } else {
- Tags.HTTP_STATUS.set(span, result.getValue().getStatusCode());
- }
- }
- }
-
- public static class StreamedResponseAction implements Action<Result<StreamedResponse>> {
- private final Span span;
-
- public StreamedResponseAction(Span span) {
- this.span = span;
- }
-
- @Override
- public void execute(Result<StreamedResponse> result) {
- span.finish();
- if (result.isError()) {
- Tags.ERROR.set(span, true);
- span.log(Collections.singletonMap(ERROR_OBJECT, result.getThrowable()));
- } else {
- Tags.HTTP_STATUS.set(span, result.getValue().getStatusCode());
- }
- }
- }
-
- public static class RatpackHttpClientRequestAdvice {
- @Advice.OnMethodEnter
- public static AtomicReference<Span> injectTracing(
- @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
- AtomicReference<Span> span = new AtomicReference<>();
-
- //noinspection UnusedAssignment
- requestAction = new RequestAction(requestAction, span);
-
- return span;
- }
-
- @Advice.OnMethodExit
- public static void finishTracing(
- @Advice.Return(readOnly = false) Promise<ReceivedResponse> promise,
- @Advice.Enter AtomicReference<Span> ref) {
-
- //noinspection UnusedAssignment
- promise = promise.wiretap(new ResponseAction(ref));
- }
- }
-
- public static class RatpackHttpClientRequestStreamAdvice {
- @Advice.OnMethodEnter
- public static AtomicReference<Span> injectTracing(
- @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
- AtomicReference<Span> span = new AtomicReference<>();
-
- //noinspection UnusedAssignment
- requestAction = new RequestAction(requestAction, span);
-
- return span;
- }
-
- @Advice.OnMethodExit
- public static void finishTracing(
- @Advice.Return(readOnly = false) Promise<StreamedResponse> promise,
- @Advice.Enter AtomicReference<Span> ref) {
- Span span = ref.get();
- if (span == null) {
- return;
- }
-
- //noinspection UnusedAssignment
- promise = promise.wiretap(new StreamedResponseAction(span));
- }
- }
-
- public static class RatpackHttpGetAdvice {
- @Advice.OnMethodEnter
- public static void ensureGetMethodSet(
- @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
- //noinspection UnusedAssignment
- requestAction = requestAction.prepend(new GetRequestSpecAction());
- }
- }
-
- /** This was a method reference but we can't use Java 8 due to Java 7 support */
- public static class GetRequestSpecAction implements Action<RequestSpec> {
- @Override
- public void execute(RequestSpec requestSpec) {
- requestSpec.get();
- }
- }
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackInstrumentation.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackInstrumentation.java
@@ -4,25 +4,15 @@ import static datadog.trace.agent.tooling.ClassLoaderMatcher.classLoaderHasClass
import static net.bytebuddy.matcher.ElementMatchers.*;
import com.google.auto.service.AutoService;
-import datadog.opentracing.scopemanager.ContextualScopeManager;
import datadog.trace.agent.tooling.DDAdvice;
import datadog.trace.agent.tooling.HelperInjector;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Scope;
-import io.opentracing.ScopeManager;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.ratpack.impl.RatpackServerAdvice;
import java.lang.reflect.Modifier;
-import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.builder.AgentBuilder;
-import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
-import ratpack.exec.ExecStarter;
-import ratpack.func.Action;
-import ratpack.handling.HandlerDecorator;
-import ratpack.registry.Registry;
-import ratpack.registry.RegistrySpec;
@AutoService(Instrumenter.class)
@Slf4j
@@ -31,17 +21,16 @@ public final class RatpackInstrumentation extends Instrumenter.Configurable {
static final String EXEC_NAME = "ratpack";
private static final HelperInjector SERVER_REGISTRY_HELPER_INJECTOR =
new HelperInjector(
- "datadog.trace.instrumentation.ratpack.RatpackScopeManager",
- "datadog.trace.instrumentation.ratpack.TracingHandler",
- "datadog.trace.instrumentation.ratpack.RatpackInstrumentation$RatpackServerRegistryAdvice");
+ "datadog.trace.instrumentation.ratpack.impl.RatpackScopeManager",
+ "datadog.trace.instrumentation.ratpack.impl.TracingHandler",
+ "datadog.trace.instrumentation.ratpack.impl.RatpackServerAdvice$RatpackServerRegistryAdvice");
private static final HelperInjector EXEC_STARTER_HELPER_INJECTOR =
new HelperInjector(
- "datadog.trace.instrumentation.ratpack.RatpackInstrumentation$ExecStarterAdvice",
- "datadog.trace.instrumentation.ratpack.RatpackInstrumentation$ExecStarterAction");
+ "datadog.trace.instrumentation.ratpack.impl.RatpackServerAdvice$ExecStarterAdvice",
+ "datadog.trace.instrumentation.ratpack.impl.RatpackServerAdvice$ExecStarterAction");
static final TypeDescription.Latent ACTION_TYPE_DESCRIPTION =
- new TypeDescription.Latent(
- "ratpack.func.Action", Modifier.PUBLIC, null, Collections.emptyList());
+ new TypeDescription.Latent("ratpack.func.Action", Modifier.PUBLIC, null);
static final ElementMatcher.Junction.AbstractBase<ClassLoader>
CLASSLOADER_CONTAINS_RATPACK_1_4_OR_ABOVE =
@@ -68,7 +57,7 @@ public final class RatpackInstrumentation extends Instrumenter.Configurable {
DDAdvice.create()
.advice(
isMethod().and(isStatic()).and(named("buildBaseRegistry")),
- RatpackServerRegistryAdvice.class.getName()))
+ RatpackServerAdvice.RatpackServerRegistryAdvice.class.getName()))
.asDecorator()
.type(
not(isInterface()).and(hasSuperType(named("ratpack.exec.ExecStarter"))),
@@ -78,7 +67,7 @@ public final class RatpackInstrumentation extends Instrumenter.Configurable {
DDAdvice.create()
.advice(
named("register").and(takesArguments(ACTION_TYPE_DESCRIPTION)),
- ExecStarterAdvice.class.getName()))
+ RatpackServerAdvice.ExecStarterAdvice.class.getName()))
.asDecorator()
.type(
named("ratpack.exec.Execution")
@@ -89,70 +78,7 @@ public final class RatpackInstrumentation extends Instrumenter.Configurable {
DDAdvice.create()
.advice(
named("fork").and(returns(named("ratpack.exec.ExecStarter"))),
- ExecutionAdvice.class.getName()))
+ RatpackServerAdvice.ExecutionAdvice.class.getName()))
.asDecorator();
}
-
- public static class RatpackServerRegistryAdvice {
- @Advice.OnMethodExit(suppress = Throwable.class)
- public static void injectTracing(@Advice.Return(readOnly = false) Registry registry) {
- RatpackScopeManager ratpackScopeManager = new RatpackScopeManager();
- // the value returned from ServerRegistry.buildBaseRegistry needs to be modified to add our
- // scope manager and handler decorator to the registry
- //noinspection UnusedAssignment
- registry =
- registry.join(
- Registry.builder()
- .add(ScopeManager.class, ratpackScopeManager)
- .add(HandlerDecorator.prepend(new TracingHandler()))
- .build());
-
- if (GlobalTracer.isRegistered()) {
- if (GlobalTracer.get().scopeManager() instanceof ContextualScopeManager) {
- ((ContextualScopeManager) GlobalTracer.get().scopeManager())
- .addScopeContext(ratpackScopeManager);
- }
- } else {
- log.warn("No GlobalTracer registered");
- }
- }
- }
-
- public static class ExecStarterAdvice {
- @Advice.OnMethodEnter
- public static void addScopeToRegistry(
- @Advice.Argument(value = 0, readOnly = false) Action<? super RegistrySpec> action) {
- Scope active = GlobalTracer.get().scopeManager().active();
- if (active != null) {
- //noinspection UnusedAssignment
- action = new ExecStarterAction(active).append(action);
- }
- }
- }
-
- public static class ExecutionAdvice {
- @Advice.OnMethodExit
- public static void addScopeToRegistry(@Advice.Return ExecStarter starter) {
- Scope active = GlobalTracer.get().scopeManager().active();
- if (active != null) {
- starter.register(new ExecStarterAction(active));
- }
- }
- }
-
- public static class ExecStarterAction implements Action<RegistrySpec> {
- private final Scope active;
-
- @SuppressWarnings("WeakerAccess")
- public ExecStarterAction(Scope active) {
- this.active = active;
- }
-
- @Override
- public void execute(RegistrySpec spec) {
- if (active != null) {
- spec.add(active);
- }
- }
- }
}

ADD
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/RatpackHttpClientAdvice.java
+package datadog.trace.instrumentation.ratpack.impl;
+
+import static io.opentracing.log.Fields.ERROR_OBJECT;
+
+import io.opentracing.Span;
+import io.opentracing.tag.Tags;
+import io.opentracing.util.GlobalTracer;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+import net.bytebuddy.asm.Advice;
+import ratpack.exec.Promise;
+import ratpack.exec.Result;
+import ratpack.func.Action;
+import ratpack.http.client.ReceivedResponse;
+import ratpack.http.client.RequestSpec;
+import ratpack.http.client.StreamedResponse;
+
+public class RatpackHttpClientAdvice {
+ public static class RequestAction implements Action<RequestSpec> {
+
+ private final Action<? super RequestSpec> requestAction;
+ private final AtomicReference<Span> spanRef;
+
+ public RequestAction(Action<? super RequestSpec> requestAction, AtomicReference<Span> spanRef) {
+ this.requestAction = requestAction;
+ this.spanRef = spanRef;
+ }
+
+ @Override
+ public void execute(RequestSpec requestSpec) throws Exception {
+ requestAction.execute(
+ new WrappedRequestSpec(
+ requestSpec,
+ GlobalTracer.get(),
+ GlobalTracer.get().scopeManager().active(),
+ spanRef));
+ }
+ }
+
+ public static class ResponseAction implements Action<Result<ReceivedResponse>> {
+ private final AtomicReference<Span> spanRef;
+
+ public ResponseAction(AtomicReference<Span> spanRef) {
+ this.spanRef = spanRef;
+ }
+
+ @Override
+ public void execute(Result<ReceivedResponse> result) {
+ Span span = spanRef.get();
+ if (span == null) {
+ return;
+ }
+ span.finish();
+ if (result.isError()) {
+ Tags.ERROR.set(span, true);
+ span.log(Collections.singletonMap(ERROR_OBJECT, result.getThrowable()));
+ } else {
+ Tags.HTTP_STATUS.set(span, result.getValue().getStatusCode());
+ }
+ }
+ }
+
+ public static class StreamedResponseAction implements Action<Result<StreamedResponse>> {
+ private final Span span;
+
+ public StreamedResponseAction(Span span) {
+ this.span = span;
+ }
+
+ @Override
+ public void execute(Result<StreamedResponse> result) {
+ span.finish();
+ if (result.isError()) {
+ Tags.ERROR.set(span, true);
+ span.log(Collections.singletonMap(ERROR_OBJECT, result.getThrowable()));
+ } else {
+ Tags.HTTP_STATUS.set(span, result.getValue().getStatusCode());
+ }
+ }
+ }
+
+ public static class RatpackHttpClientRequestAdvice {
+ @Advice.OnMethodEnter
+ public static AtomicReference<Span> injectTracing(
+ @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
+ AtomicReference<Span> span = new AtomicReference<>();
+
+ //noinspection UnusedAssignment
+ requestAction = new RequestAction(requestAction, span);
+
+ return span;
+ }
+
+ @Advice.OnMethodExit
+ public static void finishTracing(
+ @Advice.Return(readOnly = false) Promise<ReceivedResponse> promise,
+ @Advice.Enter AtomicReference<Span> ref) {
+
+ //noinspection UnusedAssignment
+ promise = promise.wiretap(new ResponseAction(ref));
+ }
+ }
+
+ public static class RatpackHttpClientRequestStreamAdvice {
+ @Advice.OnMethodEnter
+ public static AtomicReference<Span> injectTracing(
+ @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
+ AtomicReference<Span> span = new AtomicReference<>();
+
+ //noinspection UnusedAssignment
+ requestAction = new RequestAction(requestAction, span);
+
+ return span;
+ }
+
+ @Advice.OnMethodExit
+ public static void finishTracing(
+ @Advice.Return(readOnly = false) Promise<StreamedResponse> promise,
+ @Advice.Enter AtomicReference<Span> ref) {
+ Span span = ref.get();
+ if (span == null) {
+ return;
+ }
+
+ //noinspection UnusedAssignment
+ promise = promise.wiretap(new StreamedResponseAction(span));
+ }
+ }
+
+ public static class RatpackHttpGetAdvice {
+ @Advice.OnMethodEnter
+ public static void ensureGetMethodSet(
+ @Advice.Argument(value = 1, readOnly = false) Action<? super RequestSpec> requestAction) {
+ //noinspection UnusedAssignment
+ requestAction = requestAction.prepend(RequestSpec::get);
+ }
+ }
+}

RENAME
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackRequestExtractAdapter.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/RatpackRequestExtractAdapter.java
-package datadog.trace.instrumentation.ratpack;
+package datadog.trace.instrumentation.ratpack.impl;
import com.google.common.collect.ListMultimap;
import io.opentracing.propagation.TextMap;

RENAME
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RatpackScopeManager.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/RatpackScopeManager.java
-package datadog.trace.instrumentation.ratpack;
+package datadog.trace.instrumentation.ratpack.impl;
import datadog.opentracing.scopemanager.ScopeContext;
import io.opentracing.Scope;

ADD
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/RatpackServerAdvice.java
+package datadog.trace.instrumentation.ratpack.impl;
+
+import datadog.opentracing.scopemanager.ContextualScopeManager;
+import io.opentracing.Scope;
+import io.opentracing.ScopeManager;
+import io.opentracing.util.GlobalTracer;
+import lombok.extern.slf4j.Slf4j;
+import net.bytebuddy.asm.Advice;
+import ratpack.exec.ExecStarter;
+import ratpack.func.Action;
+import ratpack.handling.HandlerDecorator;
+import ratpack.registry.Registry;
+import ratpack.registry.RegistrySpec;
+
+@Slf4j
+public class RatpackServerAdvice {
+ public static class RatpackServerRegistryAdvice {
+ @Advice.OnMethodExit(suppress = Throwable.class)
+ public static void injectTracing(@Advice.Return(readOnly = false) Registry registry) {
+ RatpackScopeManager ratpackScopeManager = new RatpackScopeManager();
+ // the value returned from ServerRegistry.buildBaseRegistry needs to be modified to add our
+ // scope manager and handler decorator to the registry
+ //noinspection UnusedAssignment
+ registry =
+ registry.join(
+ Registry.builder()
+ .add(ScopeManager.class, ratpackScopeManager)
+ .add(HandlerDecorator.prepend(new TracingHandler()))
+ .build());
+
+ if (GlobalTracer.isRegistered()) {
+ if (GlobalTracer.get().scopeManager() instanceof ContextualScopeManager) {
+ ((ContextualScopeManager) GlobalTracer.get().scopeManager())
+ .addScopeContext(ratpackScopeManager);
+ }
+ } else {
+ log.warn("No GlobalTracer registered");
+ }
+ }
+ }
+
+ public static class ExecStarterAdvice {
+ @Advice.OnMethodEnter
+ public static void addScopeToRegistry(
+ @Advice.Argument(value = 0, readOnly = false) Action<? super RegistrySpec> action) {
+ Scope active = GlobalTracer.get().scopeManager().active();
+ if (active != null) {
+ //noinspection UnusedAssignment
+ action = new ExecStarterAction(active).append(action);
+ }
+ }
+ }
+
+ public static class ExecutionAdvice {
+ @Advice.OnMethodExit
+ public static void addScopeToRegistry(@Advice.Return ExecStarter starter) {
+ Scope active = GlobalTracer.get().scopeManager().active();
+ if (active != null) {
+ starter.register(new ExecStarterAction(active));
+ }
+ }
+ }
+
+ public static class ExecStarterAction implements Action<RegistrySpec> {
+ private final Scope active;
+
+ @SuppressWarnings("WeakerAccess")
+ public ExecStarterAction(Scope active) {
+ this.active = active;
+ }
+
+ @Override
+ public void execute(RegistrySpec spec) {
+ if (active != null) {
+ spec.add(active);
+ }
+ }
+ }
+}

RENAME
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/RequestSpecInjectAdapter.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/RequestSpecInjectAdapter.java
-package datadog.trace.instrumentation.ratpack;
+package datadog.trace.instrumentation.ratpack.impl;
import io.opentracing.propagation.TextMap;
import java.util.Iterator;

RENAME
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/TracingHandler.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/TracingHandler.java
-package datadog.trace.instrumentation.ratpack;
+package datadog.trace.instrumentation.ratpack.impl;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.DDTags;

RENAME
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/WrappedRequestSpec.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/impl/WrappedRequestSpec.java
-package datadog.trace.instrumentation.ratpack;
+package datadog.trace.instrumentation.ratpack.impl;
import io.opentracing.Scope;
import io.opentracing.Span;

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/test/groovy/RatpackTest.groovy
dd-java-agent/instrumentation/ratpack-1.4/src/test/groovy/RatpackTest.groovy
import datadog.opentracing.scopemanager.ContextualScopeManager
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanTypes
-import datadog.trace.instrumentation.ratpack.RatpackScopeManager
+import datadog.trace.instrumentation.ratpack.impl.RatpackScopeManager
import io.opentracing.Scope
import io.opentracing.util.GlobalTracer
import okhttp3.HttpUrl

Introduce a java 8 source set for Ratpack integration