diff --git a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java index 78773f42a6..2600fe9f49 100644 --- a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java +++ b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java @@ -7,12 +7,8 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import com.google.auto.service.AutoService; import datadog.trace.agent.tooling.Instrumenter; -import datadog.trace.context.TraceScope; -import io.opentracing.Scope; -import io.opentracing.Span; import io.opentracing.util.GlobalTracer; import java.util.Map; -import lombok.extern.slf4j.Slf4j; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -35,7 +31,7 @@ public final class ContinuationInstrumentation extends Instrumenter.Default { @Override public String[] helperClassNames() { return new String[] { - getClass().getName() + "$ResumeAdvice$BlockWrapper", + packageName + ".BlockWrapper", }; } @@ -50,45 +46,12 @@ public final class ContinuationInstrumentation extends Instrumenter.Default { @Advice.OnMethodEnter(suppress = Throwable.class) public static void wrap(@Advice.Argument(value = 0, readOnly = false) Block block) { - final Span span = GlobalTracer.get().activeSpan(); - if (span != null) { - block = BlockWrapper.wrapIfNeeded(block, span); - } + block = BlockWrapper.wrapIfNeeded(block, GlobalTracer.get().activeSpan()); } public void muzzleCheck(final PathBinding binding) { // This was added in 1.4. Added here to ensure consistency with other instrumentation. binding.getDescription(); } - - @Slf4j - public static class BlockWrapper implements Block { - private final Block delegate; - private final Span span; - - private BlockWrapper(final Block delegate, final Span span) { - assert span != null; - this.delegate = delegate; - this.span = span; - } - - @Override - public void execute() throws Exception { - try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { - if (scope instanceof TraceScope) { - ((TraceScope) scope).setAsyncPropagation(true); - } - delegate.execute(); - } - } - - public static Block wrapIfNeeded(final Block delegate, final Span span) { - if (delegate instanceof BlockWrapper) { - return delegate; - } - log.debug("Wrapping block {}", delegate); - return new BlockWrapper(delegate, span); - } - } } } diff --git a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java index 8e130a1e20..a2d59bc293 100644 --- a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java +++ b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java @@ -7,12 +7,9 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import com.google.auto.service.AutoService; import datadog.trace.agent.tooling.Instrumenter; -import datadog.trace.context.TraceScope; -import io.opentracing.Scope; import io.opentracing.Span; import io.opentracing.util.GlobalTracer; import java.util.Map; -import lombok.extern.slf4j.Slf4j; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; @@ -36,7 +33,7 @@ public final class DefaultExecutionInstrumentation extends Instrumenter.Default @Override public String[] helperClassNames() { return new String[] { - getClass().getName() + "$DelimitAdvice$ActionWrapper", + packageName + ".ActionWrapper", }; } @@ -55,50 +52,18 @@ public final class DefaultExecutionInstrumentation extends Instrumenter.Default public static void wrap( @Advice.Argument(value = 0, readOnly = false) Action onError, @Advice.Argument(value = 1, readOnly = false) Action segment) { + /** + * Here we pass along the span instead of a continuation because we aren't sure the callback + * will actually be called. + */ final Span span = GlobalTracer.get().activeSpan(); - if (span != null) { - /** - * Here we pass along the span instead of a continuation because we aren't sure it won't be - * used for both callbacks. - */ - onError = ActionWrapper.wrapIfNeeded(onError, span); - segment = ActionWrapper.wrapIfNeeded(segment, span); - } + onError = ActionWrapper.wrapIfNeeded(onError, span); + segment = ActionWrapper.wrapIfNeeded(segment, span); } public void muzzleCheck(final PathBinding binding) { // This was added in 1.4. Added here to ensure consistency with other instrumentation. binding.getDescription(); } - - @Slf4j - public static class ActionWrapper implements Action { - private final Action delegate; - private final Span span; - - private ActionWrapper(final Action delegate, final Span span) { - assert span != null; - this.delegate = delegate; - this.span = span; - } - - @Override - public void execute(final T t) throws Exception { - try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { - if (scope instanceof TraceScope) { - ((TraceScope) scope).setAsyncPropagation(true); - } - delegate.execute(t); - } - } - - public static Action wrapIfNeeded(final Action delegate, final Span span) { - if (delegate instanceof ActionWrapper || span == null) { - return delegate; - } - log.debug("Wrapping action task {}", delegate); - return new ActionWrapper(delegate, span); - } - } } } diff --git a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ActionWrapper.java b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ActionWrapper.java new file mode 100644 index 0000000000..ffd62209fc --- /dev/null +++ b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ActionWrapper.java @@ -0,0 +1,38 @@ +package datadog.trace.instrumentation.ratpack; + +import datadog.trace.context.TraceScope; +import io.opentracing.Scope; +import io.opentracing.Span; +import io.opentracing.util.GlobalTracer; +import lombok.extern.slf4j.Slf4j; +import ratpack.func.Action; + +@Slf4j +public class ActionWrapper implements Action { + private final Action delegate; + private final Span span; + + private ActionWrapper(final Action delegate, final Span span) { + assert span != null; + this.delegate = delegate; + this.span = span; + } + + @Override + public void execute(final T t) throws Exception { + try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { + if (scope instanceof TraceScope) { + ((TraceScope) scope).setAsyncPropagation(true); + } + delegate.execute(t); + } + } + + public static Action wrapIfNeeded(final Action delegate, final Span span) { + if (delegate instanceof ActionWrapper || span == null) { + return delegate; + } + log.debug("Wrapping action task {}", delegate); + return new ActionWrapper(delegate, span); + } +} diff --git a/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/BlockWrapper.java b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/BlockWrapper.java new file mode 100644 index 0000000000..4803af1242 --- /dev/null +++ b/dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/BlockWrapper.java @@ -0,0 +1,38 @@ +package datadog.trace.instrumentation.ratpack; + +import datadog.trace.context.TraceScope; +import io.opentracing.Scope; +import io.opentracing.Span; +import io.opentracing.util.GlobalTracer; +import lombok.extern.slf4j.Slf4j; +import ratpack.func.Block; + +@Slf4j +public class BlockWrapper implements Block { + private final Block delegate; + private final Span span; + + private BlockWrapper(final Block delegate, final Span span) { + assert span != null; + this.delegate = delegate; + this.span = span; + } + + @Override + public void execute() throws Exception { + try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { + if (scope instanceof TraceScope) { + ((TraceScope) scope).setAsyncPropagation(true); + } + delegate.execute(); + } + } + + public static Block wrapIfNeeded(final Block delegate, final Span span) { + if (delegate instanceof BlockWrapper || span == null) { + return delegate; + } + log.debug("Wrapping block {}", delegate); + return new BlockWrapper(delegate, span); + } +}