Update ratpack-1.4 to new agent api
MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/ContinuationInstrumentation.java
package datadog.trace.instrumentation.ratpack;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
+import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.util.GlobalTracer;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
@@ -46,7 +46,7 @@ public final class ContinuationInstrumentation extends Instrumenter.Default {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrap(@Advice.Argument(value = 0, readOnly = false) Block block) {
- block = BlockWrapper.wrapIfNeeded(block, GlobalTracer.get().activeSpan());
+ block = BlockWrapper.wrapIfNeeded(block, activeSpan());
}
public void muzzleCheck(final PathBinding binding) {

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java/datadog/trace/instrumentation/ratpack/DefaultExecutionInstrumentation.java
package datadog.trace.instrumentation.ratpack;
+import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -7,8 +8,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
-import io.opentracing.Span;
-import io.opentracing.util.GlobalTracer;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
@@ -56,7 +56,7 @@ public final class DefaultExecutionInstrumentation extends Instrumenter.Default
* 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();
+ final AgentSpan span = activeSpan();
onError = ActionWrapper.wrapIfNeeded(onError, span);
segment = ActionWrapper.wrapIfNeeded(segment, span);
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ActionWrapper.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ActionWrapper.java
package datadog.trace.instrumentation.ratpack;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.util.GlobalTracer;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import lombok.extern.slf4j.Slf4j;
import ratpack.func.Action;
@Slf4j
public class ActionWrapper<T> implements Action<T> {
private final Action<T> delegate;
- private final Span span;
+ private final AgentSpan span;
- private ActionWrapper(final Action<T> delegate, final Span span) {
+ private ActionWrapper(final Action<T> delegate, final AgentSpan span) {
assert span != null;
this.delegate = delegate;
this.span = span;
@@ -20,15 +20,13 @@ public class ActionWrapper<T> implements Action<T> {
@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);
- }
+ try (final AgentScope scope = activateSpan(span, false)) {
+ scope.setAsyncPropagation(true);
delegate.execute(t);
}
}
- public static <T> Action<T> wrapIfNeeded(final Action<T> delegate, final Span span) {
+ public static <T> Action<T> wrapIfNeeded(final Action<T> delegate, final AgentSpan span) {
if (delegate instanceof ActionWrapper || span == null) {
return delegate;
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/BlockWrapper.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/BlockWrapper.java
package datadog.trace.instrumentation.ratpack;
-import datadog.trace.context.TraceScope;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.util.GlobalTracer;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import lombok.extern.slf4j.Slf4j;
import ratpack.func.Block;
@Slf4j
public class BlockWrapper implements Block {
private final Block delegate;
- private final Span span;
+ private final AgentSpan span;
- private BlockWrapper(final Block delegate, final Span span) {
+ private BlockWrapper(final Block delegate, final AgentSpan span) {
assert span != null;
this.delegate = delegate;
this.span = span;
@@ -20,15 +20,13 @@ public class BlockWrapper implements Block {
@Override
public void execute() throws Exception {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
- if (scope instanceof TraceScope) {
- ((TraceScope) scope).setAsyncPropagation(true);
- }
+ try (final AgentScope scope = activateSpan(span, false)) {
+ scope.setAsyncPropagation(true);
delegate.execute();
}
}
- public static Block wrapIfNeeded(final Block delegate, final Span span) {
+ public static Block wrapIfNeeded(final Block delegate, final AgentSpan span) {
if (delegate instanceof BlockWrapper || span == null) {
return delegate;
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ErrorHandlerAdvice.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/ErrorHandlerAdvice.java
package datadog.trace.instrumentation.ratpack;
-import static datadog.trace.instrumentation.ratpack.RatpackServerDecorator.DECORATE;
-
-import io.opentracing.Span;
import java.util.Optional;
+
+import datadog.trace.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import ratpack.handling.Context;
+import static datadog.trace.instrumentation.ratpack.RatpackServerDecorator.DECORATE;
+
public class ErrorHandlerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void captureThrowable(
@Advice.Argument(0) final Context ctx, @Advice.Argument(1) final Throwable throwable) {
- final Optional<Span> span = ctx.maybeGet(Span.class);
+ final Optional<AgentSpan> span = ctx.maybeGet(AgentSpan.class);
if (span.isPresent()) {
DECORATE.onError(span.get(), throwable);
}

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/RatpackServerDecorator.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/RatpackServerDecorator.java
@@ -3,7 +3,7 @@ package datadog.trace.instrumentation.ratpack;
import com.google.common.net.HostAndPort;
import datadog.trace.agent.decorator.HttpServerDecorator;
import datadog.trace.api.DDTags;
-import io.opentracing.Span;
+import datadog.trace.instrumentation.api.AgentSpan;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
import ratpack.handling.Context;
@@ -70,7 +70,7 @@ public class RatpackServerDecorator extends HttpServerDecorator<Request, Request
}
}
- public Span onContext(final Span span, final Context ctx) {
+ public AgentSpan onContext(final AgentSpan span, final Context ctx) {
String description = ctx.getPathBinding().getDescription();
if (description == null || description.isEmpty()) {
@@ -86,7 +86,7 @@ public class RatpackServerDecorator extends HttpServerDecorator<Request, Request
}
@Override
- public Span onError(final Span span, final Throwable throwable) {
+ public AgentSpan onError(final AgentSpan span, final Throwable throwable) {
// Attempt to unwrap ratpack.handling.internal.HandlerException without direct reference.
if (throwable instanceof Error && throwable.getCause() != null) {
return super.onError(span, throwable.getCause());

MODIFY
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/TracingHandler.java
dd-java-agent/instrumentation/ratpack-1.4/src/main/java8/datadog/trace/instrumentation/ratpack/TracingHandler.java
package datadog.trace.instrumentation.ratpack;
+import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
+import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.ratpack.RatpackServerDecorator.DECORATE;
-import datadog.trace.context.TraceScope;
+import datadog.trace.instrumentation.api.AgentScope;
+import datadog.trace.instrumentation.api.AgentSpan;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
-import io.opentracing.Scope;
-import io.opentracing.Span;
-import io.opentracing.Tracer;
-import io.opentracing.util.GlobalTracer;
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.http.Request;
@@ -20,35 +19,32 @@ public final class TracingHandler implements Handler {
* This constant is copied over from datadog.trace.instrumentation.netty41.AttributeKeys. The key
* string must be kept consistent.
*/
- public static final AttributeKey<Span> SERVER_ATTRIBUTE_KEY =
+ public static final AttributeKey<AgentSpan> SERVER_ATTRIBUTE_KEY =
AttributeKey.valueOf(
"datadog.trace.instrumentation.netty41.server.HttpServerTracingHandler.span");
@Override
public void handle(final Context ctx) {
- final Tracer tracer = GlobalTracer.get();
final Request request = ctx.getRequest();
- final Attribute<Span> spanAttribute =
+ final Attribute<AgentSpan> spanAttribute =
ctx.getDirectChannelAccess().getChannel().attr(SERVER_ATTRIBUTE_KEY);
- final Span nettySpan = spanAttribute.get();
+ final AgentSpan nettySpan = spanAttribute.get();
// Relying on executor instrumentation to assume the netty span is in context as the parent.
- final Span ratpackSpan = tracer.buildSpan("ratpack.handler").start();
+ final AgentSpan ratpackSpan = startSpan("ratpack.handler");
DECORATE.afterStart(ratpackSpan);
DECORATE.onConnection(ratpackSpan, request);
DECORATE.onRequest(ratpackSpan, request);
ctx.getExecution().add(ratpackSpan);
- try (final Scope scope = tracer.scopeManager().activate(ratpackSpan, false)) {
- if (scope instanceof TraceScope) {
- ((TraceScope) scope).setAsyncPropagation(true);
- }
+ try (final AgentScope scope = activateSpan(ratpackSpan, false)) {
+ scope.setAsyncPropagation(true);
ctx.getResponse()
.beforeSend(
response -> {
- try (final Scope ignored = tracer.scopeManager().activate(ratpackSpan, false)) {
+ try (final AgentScope ignored = activateSpan(ratpackSpan, false)) {
if (nettySpan != null) {
// Rename the netty span resource name with the ratpack route.
DECORATE.onContext(nettySpan, ctx);

Update ratpack-1.4 to new agent api