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);
}
,并将类CustomizeConfiguration
中resolverClassAndMethod()
方法的
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 201