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

Make netty ChannelPipeline removeLast return user handler #9584

Merged
merged 1 commit into from
Oct 2, 2023
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 @@ -141,23 +141,29 @@ public static class RemoveLastAdvice {

@Advice.OnMethodExit(suppress = Throwable.class)
public static void removeHandler(
@Advice.This ChannelPipeline pipeline, @Advice.Return ChannelHandler handler) {
@Advice.This ChannelPipeline pipeline,
@Advice.Return(readOnly = false) ChannelHandler handler) {
VirtualField<ChannelHandler, ChannelHandler> virtualField =
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
if (ourHandler != null) {
pipeline.remove(ourHandler);
// Context is null when our handler has already been removed. This happens when calling
// removeLast first removed our handler and we called removeLast again to remove the http
// handler.
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
} else if (handler
.getClass()
.getName()
.startsWith("io.opentelemetry.javaagent.instrumentation.netty.")) {
pipeline.removeLast();
handler = pipeline.removeLast();
} else if (handler
.getClass()
.getName()
.startsWith("io.opentelemetry.instrumentation.netty.")) {
pipeline.removeLast();
handler = pipeline.removeLast();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public void transform(TypeTransformer transformer) {

/**
* When certain handlers are added to the pipeline, we want to add our corresponding tracing
* handlers. If those handlers are later removed, we may want to remove our handlers. That is not
* currently implemented.
Comment on lines -51 to -52
Copy link
Member

Choose a reason for hiding this comment

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

👍

* handlers. If those handlers are later removed, we may want to remove our handlers.
*/
@SuppressWarnings("unused")
public static class ChannelPipelineAddAdvice {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ class ChannelPipelineTest extends AgentInstrumentationSpecification {
channelPipeline.last().getClass().getSimpleName() == "HttpClientTracingHandler"

when:
channelPipeline.removeLast()
def removed = channelPipeline.removeLast()

then: "there is no handler in pipeline"
channelPipeline.size() == 0
// removing tracing handler also removes the http handler and returns it
removed == httpHandler
}

private static class NoopChannelHandler extends ChannelHandlerAdapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void transform(TypeTransformer transformer) {

/**
* When certain handlers are added to the pipeline, we want to add our corresponding tracing
* handlers. If those handlers are later removed, we also remove our handlers. Support for
* replacing handlers and removeFirst/removeLast is currently not implemented.
* handlers. If those handlers are later removed, we also remove our handlers.
*/
@SuppressWarnings("unused")
public static class ChannelPipelineAddAdvice {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ class ChannelPipelineTest extends AgentInstrumentationSpecification {
channelPipeline.last().getClass().simpleName == "HttpClientTracingHandler"

when:
channelPipeline.removeLast()
def removed = channelPipeline.removeLast()

then: "there is no handler in pipeline"
channelPipeline.size() == 0
// removing tracing handler also removes the http handler and returns it
removed == httpHandler
}

private static class NoopChannelHandler extends ChannelHandlerAdapter {
Expand Down
Loading