Update ratpack-1.4 to new agent api

This commit is contained in:
Trask Stalnaker 2019-10-19 11:57:46 -07:00
parent 462a6632f7
commit 9775ae5b2f
7 changed files with 42 additions and 49 deletions

View File

@ -1,13 +1,13 @@
package datadog.trace.instrumentation.ratpack; package datadog.trace.instrumentation.ratpack;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType;
import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
import static java.util.Collections.singletonMap; 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.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import io.opentracing.util.GlobalTracer;
import java.util.Map; 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;
@ -46,7 +46,7 @@ public final class ContinuationInstrumentation extends Instrumenter.Default {
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrap(@Advice.Argument(value = 0, readOnly = false) Block block) { 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) { public void muzzleCheck(final PathBinding binding) {

View File

@ -1,5 +1,6 @@
package datadog.trace.instrumentation.ratpack; package datadog.trace.instrumentation.ratpack;
import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named; 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 com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.util.GlobalTracer;
import java.util.Map; 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;
@ -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 * Here we pass along the span instead of a continuation because we aren't sure the callback
* will actually be called. * will actually be called.
*/ */
final Span span = GlobalTracer.get().activeSpan(); final AgentSpan span = activeSpan();
onError = ActionWrapper.wrapIfNeeded(onError, span); onError = ActionWrapper.wrapIfNeeded(onError, span);
segment = ActionWrapper.wrapIfNeeded(segment, span); segment = ActionWrapper.wrapIfNeeded(segment, span);
} }

View File

@ -1,18 +1,18 @@
package datadog.trace.instrumentation.ratpack; package datadog.trace.instrumentation.ratpack;
import datadog.trace.context.TraceScope; import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
import io.opentracing.Scope;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentScope;
import io.opentracing.util.GlobalTracer; import datadog.trace.instrumentation.api.AgentSpan;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import ratpack.func.Action; import ratpack.func.Action;
@Slf4j @Slf4j
public class ActionWrapper<T> implements Action<T> { public class ActionWrapper<T> implements Action<T> {
private final Action<T> delegate; 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; assert span != null;
this.delegate = delegate; this.delegate = delegate;
this.span = span; this.span = span;
@ -20,15 +20,13 @@ public class ActionWrapper<T> implements Action<T> {
@Override @Override
public void execute(final T t) throws Exception { public void execute(final T t) throws Exception {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { try (final AgentScope scope = activateSpan(span, false)) {
if (scope instanceof TraceScope) { scope.setAsyncPropagation(true);
((TraceScope) scope).setAsyncPropagation(true);
}
delegate.execute(t); 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) { if (delegate instanceof ActionWrapper || span == null) {
return delegate; return delegate;
} }

View File

@ -1,18 +1,18 @@
package datadog.trace.instrumentation.ratpack; package datadog.trace.instrumentation.ratpack;
import datadog.trace.context.TraceScope; import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
import io.opentracing.Scope;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentScope;
import io.opentracing.util.GlobalTracer; import datadog.trace.instrumentation.api.AgentSpan;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import ratpack.func.Block; import ratpack.func.Block;
@Slf4j @Slf4j
public class BlockWrapper implements Block { public class BlockWrapper implements Block {
private final Block delegate; 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; assert span != null;
this.delegate = delegate; this.delegate = delegate;
this.span = span; this.span = span;
@ -20,15 +20,13 @@ public class BlockWrapper implements Block {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) { try (final AgentScope scope = activateSpan(span, false)) {
if (scope instanceof TraceScope) { scope.setAsyncPropagation(true);
((TraceScope) scope).setAsyncPropagation(true);
}
delegate.execute(); 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) { if (delegate instanceof BlockWrapper || span == null) {
return delegate; return delegate;
} }

View File

@ -1,17 +1,18 @@
package datadog.trace.instrumentation.ratpack; package datadog.trace.instrumentation.ratpack;
import static datadog.trace.instrumentation.ratpack.RatpackServerDecorator.DECORATE;
import io.opentracing.Span;
import java.util.Optional; import java.util.Optional;
import datadog.trace.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import ratpack.handling.Context; import ratpack.handling.Context;
import static datadog.trace.instrumentation.ratpack.RatpackServerDecorator.DECORATE;
public class ErrorHandlerAdvice { public class ErrorHandlerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static void captureThrowable( public static void captureThrowable(
@Advice.Argument(0) final Context ctx, @Advice.Argument(1) final Throwable throwable) { @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()) { if (span.isPresent()) {
DECORATE.onError(span.get(), throwable); DECORATE.onError(span.get(), throwable);
} }

View File

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

View File

@ -1,14 +1,13 @@
package datadog.trace.instrumentation.ratpack; 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 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.Attribute;
import io.netty.util.AttributeKey; 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.Context;
import ratpack.handling.Handler; import ratpack.handling.Handler;
import ratpack.http.Request; 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 * This constant is copied over from datadog.trace.instrumentation.netty41.AttributeKeys. The key
* string must be kept consistent. * string must be kept consistent.
*/ */
public static final AttributeKey<Span> SERVER_ATTRIBUTE_KEY = public static final AttributeKey<AgentSpan> SERVER_ATTRIBUTE_KEY =
AttributeKey.valueOf( AttributeKey.valueOf(
"datadog.trace.instrumentation.netty41.server.HttpServerTracingHandler.span"); "datadog.trace.instrumentation.netty41.server.HttpServerTracingHandler.span");
@Override @Override
public void handle(final Context ctx) { public void handle(final Context ctx) {
final Tracer tracer = GlobalTracer.get();
final Request request = ctx.getRequest(); final Request request = ctx.getRequest();
final Attribute<Span> spanAttribute = final Attribute<AgentSpan> spanAttribute =
ctx.getDirectChannelAccess().getChannel().attr(SERVER_ATTRIBUTE_KEY); 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. // 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.afterStart(ratpackSpan);
DECORATE.onConnection(ratpackSpan, request); DECORATE.onConnection(ratpackSpan, request);
DECORATE.onRequest(ratpackSpan, request); DECORATE.onRequest(ratpackSpan, request);
ctx.getExecution().add(ratpackSpan); ctx.getExecution().add(ratpackSpan);
try (final Scope scope = tracer.scopeManager().activate(ratpackSpan, false)) { try (final AgentScope scope = activateSpan(ratpackSpan, false)) {
if (scope instanceof TraceScope) { scope.setAsyncPropagation(true);
((TraceScope) scope).setAsyncPropagation(true);
}
ctx.getResponse() ctx.getResponse()
.beforeSend( .beforeSend(
response -> { response -> {
try (final Scope ignored = tracer.scopeManager().activate(ratpackSpan, false)) { try (final AgentScope ignored = activateSpan(ratpackSpan, false)) {
if (nettySpan != null) { if (nettySpan != null) {
// Rename the netty span resource name with the ratpack route. // Rename the netty span resource name with the ratpack route.
DECORATE.onContext(nettySpan, ctx); DECORATE.onContext(nettySpan, ctx);