skywalking自定义增强插件apm-customize-enhance-plugin


skywalking支持自定义增强插件,可在指定类中增强方法,但是需要在xml中将所有需要增强的方法都列出来,链接如下:https://github.com/apache/skywalking/blob/master/docs/en/setup/service-agent/java-agent/Customize-enhance-trace.md。现需要用正则表达式匹配所有方法。

1.找到类CustomizeConfiguration(其实它是一个枚举enum,把它当类就好)
找到parserMethodsMatcher()方法,进入named()方法,可以看到它其实是要和类名完全匹配的,而我们现在需要正则匹配,往下找关键字regex,找到nameMatches()方法,用nameMatches()替换named()。

2.需要让被增强的类显示它的operationName,而不是显示*****
找到类BaseInterceptorMethods,在44行代码后添加

String operationName = MethodConfiguration.getOperationName(configuration);
            if (StringUtil.isEmpty(operationName)){
                operationName = MethodUtil.generateOperationName(method);
            }

,并将类CustomizeConfigurationresolverClassAndMethod()方法的

  if (StringUtil.isEmpty(MethodConfiguration.getOperationName(configuration))) {
                MethodConfiguration.setOperationName(configuration, MethodConfiguration.getMethod(configuration));
            }

以上代码删掉。

3.用mvn clean compile package -Pagent,dist -Dcheckstyle.skip=true -DskipTests -Drat.skip=true命令重新打包编译。并按照官方步骤,激活jar包,写xml,配置xml位置等步骤完成配置,详情请参照本文开头链接。我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<enhanced><class class_name="com.bx.bean.Order">
    <method method=".*()" static="false"/>
    <method method=".*(.*)" static="false"/>
</class> </enhanced>

我的xml位置:

plugin.customize.enhance_file=/Users/luorui/bxbank/apm/skywalking-agent/config/customize_enhance.xml

4.看agent-log日志,我的日志位置在/Users/luorui/bxbank/apm-demo/agent-log。报错:

ERROR 2019-10-21 11:58:47:989 main SkyWalkingAgent : Enhance class
com.bx.bean.Order error. java.lang.IllegalArgumentException: Cannot
resolve ambiguous delegation of public abstract java.lang.Object
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance.getSkyWalkingDynamicField()
to
org.apache.skywalking.apm.dependencies.net.bytebuddy.implementation.bind.MethodDelegationBinder M e t h o d B i n d i n g MethodBinding MethodBindingBuilder B u i l d @ 8 d 68 a 7 d 2 o r o r g . a p a c h e . s k y w a l k i n g . a p m . d e p e n d e n c i e s . n e t . b y t e b u d d y . i m p l e m e n t a t i o n . b i n d . M e t h o d D e l e g a t i o n B i n d e r Build@8d68a7d2 or org.apache.skywalking.apm.dependencies.net.bytebuddy.implementation.bind.MethodDelegationBinder Build@8d68a7d2ororg.apache.skywalking.apm.dependencies.net.bytebuddy.implementation.bind.MethodDelegationBinderMethodBinding B u i l d e r Builder BuilderBuild@e770cdb2

在skywalking的官方github上搜索java.lang.IllegalArgumentException: Cannot resolve ambiguous delegation of public abstract java.lang.Object org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance.getSkyWalkingDynamicField()

也就是说java.lang.Objectorg.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance.getSkyWalkingDynamicField()这两种对象无法被解析。吴晟大佬亲自解答:
在这里插入图片描述
在类CustomizeUtil中,添加如下代码:

JAVA_CLASS.put("java.lang.Object", java.lang.Object.class);

在类ConfigMatcher的matches()方法中,添加如下代码:

  if (target instanceof MethodDescription){
            MethodDescription methodDescription = (MethodDescription)target;
            String typeName = methodDescription.getDeclaringType().getTypeName();
            if (CustomizeUtil.isJavaClass(typeName) || typeName.matches("org.apache.skywalking.*")){
                return false;
            }
        }

再次打包编译。成功!
显示该类里所有非静态方法。

5.测试:显示该类里所有静态方法(√)

<?xml version="1.0" encoding="UTF-8"?>
<enhanced><class class_name="com.bx.bean.Order">
    <method method=".*()" static="true"/>
    <method method=".*(.*)" static="true"/>
</class> </enhanced>

显示该类里所有以get开头的实例方法(√)

<?xml version="1.0" encoding="UTF-8"?>
<enhanced><class class_name="com.bx.bean.Order">
    <method method="get.*()" static="false"/>
    <method method="get.*(.*)" static="false"/>
</class> </enhanced>

显示所有方法与get方法是否冲突(不冲突,只显示get方法)

<?xml version="1.0" encoding="UTF-8"?>
<enhanced><class class_name="com.bx.bean.Order">
    <method method=".*()" static="false"/>
    <method method="get.*(.*)" static="false"/>
</class> </enhanced>
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐