【过时】Skywalking Java 插件开发注意点

53 篇文章 1 订阅
10 篇文章 0 订阅

本文刚写就已经过时,不删只是留作记录,建议直接看 Skywalking Java 插件开发太简单了

官方文档:Java Plugin Development Guide
官方插件: https://github.com/apache/skywalking-java

正常按照官方文档操作后,测试时,可以参考官方 SkyWalking的远程调试

我直接把插件引入到项目中后增加断点即可调试(需要注意和 agent plugins目录中的 jar 一致)。

测试过程中一直没有效果,debug才发现在执行过程中抛出了异常,异常没有输出到项目日志中,通过 debug 或者查看 agent 下面的 logs 目录中的 skywalking-api.log 可以看到。第一次遇到的错误是 net.bytebuddy 相关的错

ERROR 2022-05-07 13:06:10:701 main SkyWalkingAgent : Enhance class com.example.xxx.action.AcctAdjustAction error. 
java.lang.AbstractMethodError: com.example.xxx.skywalking.action.ActionClassInstanceMethodsEnhancePluginDefine$1.getMethodsMatcher()Lorg/apache/skywalking/apm/dependencies/net/bytebuddy/matcher/ElementMatcher;
	at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine.enhanceInstance(ClassEnhancePluginDefine.java:137)
	at org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine.enhance(AbstractClassEnhancePluginDefine.java:116)

这是因为在代码中使用的 net/bytebuddy/matcher/ElementMatcher,但是 skywalking 通过 shade 把名字改成了 org/apache/skywalking/apm/dependencies/net/bytebuddy/matcher/ElementMatcher,导致不匹配出错,配置 maven-shade-plugin 插件即可解决。

第二次是 gson 找不到的错,主要原因是我插件中用到了 gson 但是我自己的项目中没有依赖 gson,导致找不到,解决办法有两种,一个是自己项目添加 gson 依赖,一个是用 skywalking 中内置的 gson,和上面的 bytebuddy 一样配置即可。

综上所述,在官方文档基础上增加下面的配置后,即可使用:

<properties>
    <shade.package>org.apache.skywalking.apm.dependencies</shade.package>
    <shade.net.bytebuddy.source>net.bytebuddy</shade.net.bytebuddy.source>
    <shade.net.bytebuddy.target>${shade.package}.${shade.net.bytebuddy.source}</shade.net.bytebuddy.target>
    <shade.gson.source>com.google.gson</shade.gson.source>
    <shade.gson.target>${shade.package}.${shade.gson.source}</shade.gson.target>
</properties>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <createSourcesJar>true</createSourcesJar>
                        <shadeSourcesContent>true</shadeSourcesContent>
                        <relocations>
                            <relocation>
                                <pattern>${shade.net.bytebuddy.source}</pattern>
                                <shadedPattern>${shade.net.bytebuddy.target}</shadedPattern>
                            </relocation>
                            <relocation>
                                <pattern>${shade.gson.source}</pattern>
                                <shadedPattern>${shade.gson.target}</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

skywalking-agent.jar 里面可以看到下面包含的第三方依赖:
在这里插入图片描述

完整的初始 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>skywalking-plugin-xxx</artifactId>
    <version>1.0</version>

    <properties>
        <skywalking.version>8.10.0</skywalking.version>
        <shade.package>org.apache.skywalking.apm.dependencies</shade.package>
        <shade.net.bytebuddy.source>net.bytebuddy</shade.net.bytebuddy.source>
        <shade.net.bytebuddy.target>${shade.package}.${shade.net.bytebuddy.source}</shade.net.bytebuddy.target>
        <shade.gson.source>com.google.gson</shade.gson.source>
        <shade.gson.target>${shade.package}.${shade.gson.source}</shade.gson.target>
    </properties>

    <dependencies>
        <!-- TODO 你的依赖,你要拦截的类所在的包 -->

        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-agent-core</artifactId>
            <version>${skywalking.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>java-agent-util</artifactId>
            <version>${skywalking.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-test-tools</artifactId>
            <version>${skywalking.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>false</shadedArtifactAttached>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                            <createSourcesJar>true</createSourcesJar>
                            <shadeSourcesContent>true</shadeSourcesContent>
                            <relocations>
                                <relocation>
                                    <pattern>${shade.net.bytebuddy.source}</pattern>
                                    <shadedPattern>${shade.net.bytebuddy.target}</shadedPattern>
                                </relocation>
                                <!-- TODO 不用 gson 可以去掉,还有上面的 properties 配置 -->
                                <relocation>
                                    <pattern>${shade.gson.source}</pattern>
                                    <shadedPattern>${shade.gson.target}</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

skywalking 插件开发最容易出问题的就是上面的 pom.xml,有了这个之后,剩下的就是写一个 ClassInstanceMethodsEnhancePluginDefine 定义何时启用插件,处理哪些类和方法。写一个 InstanceMethodsAroundInterceptor 实现在方法执行前后和异常时的处理即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值