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

Fix netty issue #6469

Merged
merged 3 commits into from
Aug 18, 2022
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 @@ -6,9 +6,11 @@
package io.opentelemetry.javaagent.instrumentation.netty.v4.common.client;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
Expand All @@ -20,6 +22,11 @@ public final class NettySslInstrumentationHandler extends ChannelDuplexHandler {
private static final Class<?> SSL_HANDSHAKE_COMPLETION_EVENT;
private static final MethodHandle GET_CAUSE;

// this is used elsewhere to manage the link between the underlying (user) handler and our handler
// which is needed below so that we can unlink this handler when we remove it below
private static final VirtualField<ChannelHandler, ChannelHandler> instrumentationHandlerField =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps here should be a comment that the same virtual field is used elsewhere. Currently it is not obvious what it is for because in this file this virtual field is only set to null.

VirtualField.find(ChannelHandler.class, ChannelHandler.class);

static {
Class<?> sslHandshakeCompletionEvent = null;
MethodHandle getCause = null;
Expand All @@ -41,12 +48,15 @@ public final class NettySslInstrumentationHandler extends ChannelDuplexHandler {
}

private final NettySslInstrumenter instrumenter;
private final ChannelHandler realHandler;
private Context parentContext;
private NettySslRequest request;
private Context context;

public NettySslInstrumentationHandler(NettySslInstrumenter instrumenter) {
public NettySslInstrumentationHandler(
NettySslInstrumenter instrumenter, ChannelHandler realHandler) {
this.instrumenter = instrumenter;
this.realHandler = realHandler;
}

@Override
Expand All @@ -55,6 +65,7 @@ public void channelRegistered(ChannelHandlerContext ctx) {
// are on classpath); checking just to be extra safe
if (SSL_HANDSHAKE_COMPLETION_EVENT == null) {
ctx.pipeline().remove(this);
instrumentationHandlerField.set(realHandler, null);
ctx.fireChannelRegistered();
return;
}
Expand Down Expand Up @@ -94,6 +105,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (SSL_HANDSHAKE_COMPLETION_EVENT.isInstance(evt)) {
if (ctx.pipeline().context(this) != null) {
ctx.pipeline().remove(this);
instrumentationHandlerField.set(realHandler, null);
}

if (context != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void addHandler(
// the SslHandler lives in the netty-handler module, using class name comparison to avoid
// adding a dependency
} else if (handler.getClass().getName().equals("io.netty.handler.ssl.SslHandler")) {
ourHandler = new NettySslInstrumentationHandler(sslInstrumenter());
ourHandler = new NettySslInstrumentationHandler(sslInstrumenter(), handler);
}

if (ourHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static void addHandler(
// the SslHandler lives in the netty-handler module, using class name comparison to avoid
// adding a dependency
} else if (handler.getClass().getName().equals("io.netty.handler.ssl.SslHandler")) {
ourHandler = new NettySslInstrumentationHandler(sslInstrumenter());
ourHandler = new NettySslInstrumentationHandler(sslInstrumenter(), handler);
}

if (ourHandler != null) {
Expand Down