-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #451 from DataDog/tyler/netty-client-fixes
Allow trace to persist across netty connect.
- Loading branch information
Showing
25 changed files
with
498 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...entation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/AttributeKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package datadog.trace.instrumentation.netty40; | ||
|
||
import datadog.trace.context.TraceScope; | ||
import datadog.trace.instrumentation.netty40.server.HttpServerTracingHandler; | ||
import io.netty.util.AttributeKey; | ||
import io.opentracing.Span; | ||
|
||
public class AttributeKeys { | ||
public static final AttributeKey<TraceScope.Continuation> | ||
PARENT_CONNECT_CONTINUATION_ATTRIBUTE_KEY = | ||
new AttributeKey<>("datadog.trace.instrumentation.netty40.parent.connect.continuation"); | ||
|
||
public static final AttributeKey<Span> SERVER_ATTRIBUTE_KEY = | ||
new AttributeKey<>(HttpServerTracingHandler.class.getName() + ".span"); | ||
|
||
public static final AttributeKey<Span> CLIENT_ATTRIBUTE_KEY = | ||
new AttributeKey<>(HttpServerTracingHandler.class.getName() + ".span"); | ||
} |
100 changes: 100 additions & 0 deletions
100
...main/java/datadog/trace/instrumentation/netty40/ChannelFutureListenerInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package datadog.trace.instrumentation.netty40; | ||
|
||
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; | ||
import static datadog.trace.agent.tooling.ClassLoaderMatcher.classLoaderHasClasses; | ||
import static io.opentracing.log.Fields.ERROR_OBJECT; | ||
import static net.bytebuddy.matcher.ElementMatchers.isInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
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.netty.channel.ChannelFuture; | ||
import io.opentracing.Scope; | ||
import io.opentracing.Span; | ||
import io.opentracing.tag.Tags; | ||
import io.opentracing.util.GlobalTracer; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class ChannelFutureListenerInstrumentation extends Instrumenter.Default { | ||
|
||
public ChannelFutureListenerInstrumentation() { | ||
super("netty", "netty-4.0"); | ||
} | ||
|
||
@Override | ||
protected boolean defaultEnabled() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return not(isInterface()) | ||
.and(safeHasSuperType(named("io.netty.channel.ChannelFutureListener"))); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderMatcher() { | ||
return classLoaderHasClasses("io.netty.handler.codec.spdy.SpdyOrHttpChooser"); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] {packageName + ".AttributeKeys"}; | ||
} | ||
|
||
@Override | ||
public Map<ElementMatcher, String> transformers() { | ||
final Map<ElementMatcher, String> transformers = new HashMap<>(); | ||
transformers.put( | ||
isMethod() | ||
.and(named("operationComplete")) | ||
.and(takesArgument(0, named("io.netty.channel.ChannelFuture"))), | ||
OperationCompleteAdvice.class.getName()); | ||
return transformers; | ||
} | ||
|
||
public static class OperationCompleteAdvice { | ||
@Advice.OnMethodEnter | ||
public static TraceScope activateScope(@Advice.Argument(0) final ChannelFuture future) { | ||
final TraceScope.Continuation continuation = | ||
future.channel().attr(AttributeKeys.PARENT_CONNECT_CONTINUATION_ATTRIBUTE_KEY).get(); | ||
|
||
if (continuation == null) { | ||
return null; | ||
} | ||
final TraceScope scope = continuation.activate(); | ||
|
||
final Throwable cause = future.cause(); | ||
if (cause != null) { | ||
final Span errorSpan = | ||
GlobalTracer.get() | ||
.buildSpan("netty.connect") | ||
.withTag(Tags.COMPONENT.getKey(), "netty") | ||
.start(); | ||
Tags.ERROR.set(errorSpan, true); | ||
errorSpan.log(Collections.singletonMap(ERROR_OBJECT, cause)); | ||
errorSpan.finish(); | ||
} | ||
|
||
return scope; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void deactivateScope( | ||
@Advice.Enter final TraceScope scope, @Advice.Thrown final Throwable throwable) { | ||
if (scope != null) { | ||
((Scope) scope).close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
.../src/main/java/datadog/trace/instrumentation/netty40/client/HttpClientTracingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.