Skip to content

Commit

Permalink
Work around lambda instrumentation failure
Browse files Browse the repository at this point in the history
Ideally we would ignore instrumenting helper classes...
  • Loading branch information
trask committed Nov 24, 2021
1 parent dac1522 commit 6d63815
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.opentelemetry.context.Context;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -75,17 +77,7 @@ public void connect(
// netty SslHandler starts the handshake after it receives the channelActive() signal; this
// happens just after the connection is established
// this makes connect() promise a good place to start the SSL handshake span
promise.addListener(
future -> {
// there won't be any SSL handshake if the channel fails to connect
if (!future.isSuccess()) {
return;
}
request = NettySslRequest.create(ctx.channel());
if (instrumenter.shouldStart(parentContext, request)) {
context = instrumenter.start(parentContext, request);
}
});
promise.addListener(new StartListener(ctx));
ctx.connect(remoteAddress, localAddress, promise);
}

Expand All @@ -112,4 +104,25 @@ private static Throwable getCause(Object evt) {
return null;
}
}

private class StartListener implements GenericFutureListener<Future<Void>> {

private final ChannelHandlerContext ctx;

private StartListener(ChannelHandlerContext ctx) {
this.ctx = ctx;
}

@Override
public void operationComplete(Future<Void> future) {
// there won't be any SSL handshake if the channel fails to connect
if (!future.isSuccess()) {
return;
}
request = NettySslRequest.create(ctx.channel());
if (instrumenter.shouldStart(parentContext, request)) {
context = instrumenter.start(parentContext, request);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.netty.resolver.AddressResolverGroup;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.instrumentation.netty.common.NettyConnectionRequest;
Expand Down Expand Up @@ -106,7 +107,7 @@ private <U> Future<U> instrumentResolve(
Context context = instrumenter.start(parentContext, request);
try {
Future<U> future = resolveFunc.get();
return future.addListener(f -> instrumenter.end(context, request, null, f.cause()));
return future.addListener(new OnEndListener<>(request, context));
} catch (Throwable t) {
instrumenter.end(context, request, null, t);
throw t;
Expand All @@ -117,5 +118,22 @@ private <U> Future<U> instrumentResolve(
public void close() {
delegate.close();
}

// currently cannot use lambda for this
private class OnEndListener<U> implements GenericFutureListener<Future<U>> {

private final NettyConnectionRequest request;
private final Context context;

private OnEndListener(NettyConnectionRequest request, Context context) {
this.request = request;
this.context = context;
}

@Override
public void operationComplete(Future<U> future) {
instrumenter.end(context, request, null, future.cause());
}
}
}
}

0 comments on commit 6d63815

Please sign in to comment.