You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2021/08/11 06:04:48 UTC

[GitHub] [skywalking] daimingzhi opened a new issue #7440: Discussion about multi-interceptors supporting in SkyWalking

daimingzhi opened a new issue #7440:
URL: https://github.com/apache/skywalking/issues/7440


   - Discussion : multi-interceptors supporting in SkyWalking
   
   As I have mentioned in [https://github.com/apache/skywalking/issues/6932#issuecomment-895825310](url)。
   
   For extensibility of SkyWaling, we should support multi-interceptors。
   
   I have two ways of doing this:
   
   # the first one
   We can construct multiple ClassFileTransformer instances. In each ClassFileTransformer, there will only be one pluginDefine for each class.To do this, we should group the plugindefine,and we will construct a ClassFileTransformer instance for each group.
   The code might look something like this
   
   ```java
   List<PluginFinder> pluginFinders = groupByPluginDefine();
   for (PluginFinder finder : pluginFinders) {
       agentBuilder.type(finder.buildMatch())
           .transform(new Transformer(finder))
           .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
           .with(new Listener())
           .installOn(instrumentation);
   }
   ```
   What the method named groupByPluginDefine method does is group the pluginDefine, and promise only one pluginDefine for each class per group.
   
   # the second one
   I have debugged code, and found the reason is every time a class is enhanced, it will delegate the original method to a new InstMethodsInter instance. So, the previous is overridden.
    ![image-20210811135321186](https://gitee.com/easy4coding/blogImage/raw/master/image/image-20210811135321186.png)
   
   To solve it, maybe we can cache the interceptor and merge them.
   
   Finally, I also looked for new apis in ByteBuddy as an alternative to `MethodDelegation`. but,I am newer for it.This will be a long time.
   This is my whole idea.I hope some seniors can give me some advice. Thanks.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] wu-sheng commented on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
wu-sheng commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-897358069


   I think you skipped the details, and assume SkyWalking agent is as simple as Bytebuddy. If so, you wouldn't see why there are so many codes for SkyWalking agent core. We are building on the top of Bytebuddy.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] daimingzhi commented on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
daimingzhi commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-896759050


   > The first solution may not work. Because the delegate mechanism only supports to do once.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] daimingzhi commented on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
daimingzhi commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-897351911


   > Because the delegate mechanism only supports to do once
   
   I didn't fully grasp the meaning of this。but,I tested the byteBuddy API like this
   
   - code of agent
   
   ```java
   public class ByteBuddyAgent {
   
       public static void premain(String agentArgs, Instrumentation inst) {
           // install first time
           new AgentBuilder.Default()
               .type(named("com.easy4coding.bytebuddy.Bar"))
               .transform(new AgentBuilder.Transformer() {
                   @Override
                   public DynamicType.Builder transform(DynamicType.Builder builder,
                                                        TypeDescription typeDescription,
                                                        ClassLoader classloader,
                                                        JavaModule javaModule) {
                       return builder.method(named("m"))
                           .intercept(MethodDelegation.to(new BarInterceptorFirst()));
                   }
               }).installOn(inst);
   		
           // install second time
           new AgentBuilder.Default()
               .type(named("com.easy4coding.bytebuddy.Bar"))
               .transform(new AgentBuilder.Transformer() {
                   @Override
                   public DynamicType.Builder transform(DynamicType.Builder builder,
                                                        TypeDescription typeDescription,
                                                        ClassLoader classloader,
                                                        JavaModule javaModule) {
                       return builder.method(named("m"))
                           .intercept(MethodDelegation.to(new BarInterceptorSecond()));
                   }
               }).installOn(inst);
       }
   }
   
   public class BarInterceptorFirst {
   
   	@RuntimeType
   	public Object intercept(@This Object obj, @AllArguments Object[] allArguments, @SuperCall Callable<?> zuper,
   	                        @Origin Method method) {
   
   		System.out.println("before method first");
   		Object ret = null;
   		try {
   			ret = zuper.call();
   		} catch (Exception e) {
   			e.printStackTrace();
   		}
   		System.out.println("after method first");
   
   		return ret;
   	}
   
   }
   
   public class BarInterceptorSecond {
   
   	@RuntimeType
   	public Object intercept(@This Object obj, @AllArguments Object[] allArguments, @SuperCall Callable<?> zuper,
   	                        @Origin Method method) {
   
   		System.out.println("before method Second");
   		Object ret = null;
   		try {
   			ret = zuper.call();
   		} catch (Exception e) {
   			e.printStackTrace();
   		}
   		System.out.println("after method Second");
   
   		return ret;
   	}
   
   }
   ```
   
   - testDemo
   
   ```java
   public class DuplicateInstallDemo {
   	public static void main(String[] args) {
   		new Bar().m();
   	}
   }
   ```
   
   The program output is as follows:
   
   ```shell
   before method 2
   before method
   method named m invoked
   after method
   after method 2
   ```
   
   What puzzles me most about the first one is, how do I group pluginDefine?
   
   What I did in my company was adjust the directory structure of the project and extend the AgentClassLoader。
   
   like this:
   
   ![image-20210812130038336](https://gitee.com/easy4coding/blogImage/raw/master/image/image-20210812130038336.png)
   
   But, this way is not suitable for skywaling obviously。
   
   > You missed one important port, `EnhancedInstance#dynamicField`, which has only one value.
   
   I'm not really familiar with this piece of logic now.I will spend more time learning about it,Also, I might have to learn more about ByteBuddy


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] wu-sheng edited a comment on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
wu-sheng edited a comment on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-897357069


   This is not about bytebuddy. SkyWalking agent is not bytebuddy agent. I think you mixed these.
   I never said Bytebuddy can't do this, it definitely can. I am from SkyWalking perspective.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] daimingzhi closed issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
daimingzhi closed issue #7440:
URL: https://github.com/apache/skywalking/issues/7440


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] daimingzhi removed a comment on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
daimingzhi removed a comment on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-896759050


   > The first solution may not work. Because the delegate mechanism only supports to do once.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] wu-sheng commented on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
wu-sheng commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-897357069


   This is not bytebuddy. SkyWalking agent is not bytebuddy agent. I think you mixed these.
   I never said Bytebuddy can do this, it definitely can. I am from SkyWalking perspective.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] wu-sheng closed issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
wu-sheng closed issue #7440:
URL: https://github.com/apache/skywalking/issues/7440


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [skywalking] wu-sheng commented on issue #7440: Discussion about multi-interceptors supporting in SkyWalking

Posted by GitBox <gi...@apache.org>.
wu-sheng commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-896550583


   The first solution may not work. Because the delegate mechanism only supports to do once. 
   
   > Finally, I also looked for new apis in ByteBuddy as an alternative to MethodDelegation. but,I am newer for it.This will be a long time.
   
   This should not work, I checked with the author before. 
   
   > I have debugged code, and found the reason is every time a class is enhanced, it will delegate the original method to a new InstMethodsInter instance. So, the previous is overridden.
   
   Yes, this is the reason. 
   
   You missed one important port, `EnhancedInstance#dynamicField`, which has only one value. But different interceptors could access(set/get) with or without other plugins. In this case, you will face ·race condition`, `visibility of memory operations across threads`, even `type cast exception` in the runtime.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org