Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit netty exception capture to netty spans #3809

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyHttpClientTracer;
import io.opentelemetry.javaagent.instrumentation.netty.v4_0.server.NettyHttpServerTracer;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -49,16 +48,18 @@ public static class InvokeExceptionCaughtAdvice {
public static void onEnter(
@Advice.This ChannelHandlerContext channelContext,
@Advice.Argument(0) Throwable throwable) {
if (throwable != null) {
Attribute<Context> clientContextAttr =
channelContext.channel().attr(AttributeKeys.CLIENT_CONTEXT);
Context context = clientContextAttr.get();
if (context != null) {
NettyHttpClientTracer.tracer().endExceptionally(context, throwable);
} else {
NettyHttpServerTracer.tracer()
.onException(Java8BytecodeBridge.currentContext(), throwable);
}
Attribute<Context> clientContextAttr =
channelContext.channel().attr(AttributeKeys.CLIENT_CONTEXT);
Context clientContext = clientContextAttr.get();
if (clientContext != null) {
NettyHttpClientTracer.tracer().endExceptionally(clientContext, throwable);
return;
}
Attribute<Context> serverContextAttr =
channelContext.channel().attr(AttributeKeys.SERVER_CONTEXT);
Context serverContext = serverContextAttr.get();
if (serverContext != null) {
NettyHttpServerTracer.tracer().onException(serverContext, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ protected ConcurrentMap<String, AttributeKey<?>> computeValue(Class<?> type) {
attributeKey(AttributeKeys.class.getName() + ".write-context");

// this is the context that has the server span
public static final AttributeKey<Context> SERVER_SPAN =
attributeKey(AttributeKeys.class.getName() + ".server-span");
public static final AttributeKey<Context> SERVER_CONTEXT =
attributeKey(AttributeKeys.class.getName() + ".server-context");

public static final AttributeKey<Context> CLIENT_CONTEXT =
attributeKey(AttributeKeys.class.getName() + ".client-context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ protected int responseStatus(HttpResponse httpResponse) {

@Override
protected void attachServerContext(Context context, Channel channel) {
channel.attr(AttributeKeys.SERVER_SPAN).set(context);
channel.attr(AttributeKeys.SERVER_CONTEXT).set(context);
}

@Override
public Context getServerContext(Channel channel) {
return channel.attr(AttributeKeys.SERVER_SPAN).get();
return channel.attr(AttributeKeys.SERVER_CONTEXT).get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import io.opentelemetry.instrumentation.netty.v4_1.AttributeKeys;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.netty.v4_1.client.NettyHttpClientTracer;
import io.opentelemetry.javaagent.instrumentation.netty.v4_1.server.NettyHttpServerTracer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -46,15 +44,14 @@ public static class InvokeExceptionCaughtAdvice {
public static void onEnter(
@Advice.This ChannelHandlerContext channelContext,
@Advice.Argument(0) Throwable throwable) {
if (throwable != null) {
if (channelContext.channel().hasAttr(AttributeKeys.CLIENT_CONTEXT)) {
Attribute<Context> clientContextAttr =
channelContext.channel().attr(AttributeKeys.CLIENT_CONTEXT);
NettyHttpClientTracer.tracer().endExceptionally(clientContextAttr.get(), throwable);
} else {
NettyHttpServerTracer.tracer()
.onException(Java8BytecodeBridge.currentContext(), throwable);
}
if (channelContext.channel().hasAttr(AttributeKeys.CLIENT_CONTEXT)) {
Attribute<Context> clientContextAttr =
channelContext.channel().attr(AttributeKeys.CLIENT_CONTEXT);
NettyHttpClientTracer.tracer().endExceptionally(clientContextAttr.get(), throwable);
} else if (channelContext.channel().hasAttr(AttributeKeys.SERVER_CONTEXT)) {
trask marked this conversation as resolved.
Show resolved Hide resolved
Attribute<Context> serverContextAttr =
channelContext.channel().attr(AttributeKeys.SERVER_CONTEXT);
NettyHttpClientTracer.tracer().onException(serverContextAttr.get(), throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ protected int responseStatus(HttpResponse httpResponse) {

@Override
protected void attachServerContext(Context context, Channel channel) {
channel.attr(AttributeKeys.SERVER_SPAN).set(context);
channel.attr(AttributeKeys.SERVER_CONTEXT).set(context);
}

@Override
public Context getServerContext(Channel channel) {
return channel.attr(AttributeKeys.SERVER_SPAN).get();
return channel.attr(AttributeKeys.SERVER_CONTEXT).get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class AttributeKeys {
// this is the context that has the server span
//
// note: this attribute key is also used by ratpack instrumentation
public static final AttributeKey<Context> SERVER_SPAN =
public static final AttributeKey<Context> SERVER_CONTEXT =
AttributeKey.valueOf(AttributeKeys.class, "server-span");

public static final AttributeKey<Context> CLIENT_CONTEXT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class TracingHandler implements Handler {
@Override
public void handle(Context ctx) {
Attribute<io.opentelemetry.context.Context> spanAttribute =
ctx.getDirectChannelAccess().getChannel().attr(AttributeKeys.SERVER_SPAN);
ctx.getDirectChannelAccess().getChannel().attr(AttributeKeys.SERVER_CONTEXT);
io.opentelemetry.context.Context serverSpanContext = spanAttribute.get();

// Must use context from channel, as executor instrumentation is not accurate - Ratpack
Expand Down