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

#4056 remove oringin handler when removelast in netty #4123

Merged
merged 5 commits into from
Sep 17, 2021

Conversation

tydhot
Copy link
Member

@tydhot tydhot commented Sep 14, 2021

No description provided.

Comment on lines +145 to +149
} else if (handler
.getClass()
.getName()
.startsWith("io.opentelemetry.javaagent.instrumentation.netty.")) {
pipeline.removeLast();
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, what if somebody calls pipeline.addAfter(originalHandler, ...)? Then we'll remove some random custom handler that has nothing to do with our instrumentation. If we want this method to work correctly we should

  1. Maintain a two-way association between the originalHandler and ourHandler (i.e. put ourHandler -> originalHandler into the ContextStore too) and remove the originalHandler whenever removeLast() removes our handler
  2. Instrument addAfter() so that it actually adds the new handler after ourHandler whenever it's associated to the originalHandler

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, u r right.Maybe we can instrument addAfter() to make sure ourhandler will always be added after the originalHandler. I'll have a try.

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek left a comment

Choose a reason for hiding this comment

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

Can you add some tests (probably ChannelPipelineTest in netty-4.0 & netty-4.1 modules) that verify this new advice works with general add*() advices?

isMethod().and(named("removeLast")).and(returns(named("io.netty.channel.ChannelHandler"))),
AbstractNettyChannelPipelineInstrumentation.class.getName() + "$RemoveLastAdvice");
transformer.applyAdviceToMethod(
isMethod().and(named("addAfter")).and(takesArguments(3)),
Copy link
Member

Choose a reason for hiding this comment

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

The 3-args variant calls the 4-args one, it should be enough to instrument the 4-args one.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok i got it

@Advice.OnMethodExit(suppress = Throwable.class)
public static void addAfterHandler(
@Advice.This ChannelPipeline pipeline,
@Advice.Argument(value = 1, readOnly = false) String name) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add takesArgument(1, String.class) to the method matcher? In general we aim to enclose all types that we use in advice classes (arguments, return types) in the method matcher.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, of course

@tydhot
Copy link
Member Author

tydhot commented Sep 16, 2021

@mateuszrzeszutek thanks! It looked better now. But it seemed the check meet some problem now.

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek left a comment

Choose a reason for hiding this comment

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

Nice, thanks!
I left a minor comment about unit tests, but overall it looks good 👍

Comment on lines 104 to 106
channelPipeline.first() == httpHandler
// our handler was also added
channelPipeline.last().getClass() == HttpClientTracingHandler
Copy link
Member

Choose a reason for hiding this comment

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

These 2 assertions are already present in the then: section.

Copy link
Member

Choose a reason for hiding this comment

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

Same applies to the other test.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, should I remove then in this pr?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please 👍

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek left a comment

Choose a reason for hiding this comment

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

Thanks again!

@tydhot
Copy link
Member Author

tydhot commented Sep 17, 2021

@mateuszrzeszutek you're welcome :)

Copy link
Member

@trask trask left a comment

Choose a reason for hiding this comment

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

😅 that's a tricky few LOC, thx @tydhot!

@trask trask merged commit 3bc0e07 into open-telemetry:main Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Netty ChannelPipeline#removeLast() removes instrumentation handler
3 participants