在写 Skywalking Java 插件开发注意点 时,就感觉不舒服,扩展不应该这么麻烦,既然 skywalking-agent 中存在第三方的类,直接用不就好了吗,何必用 maven-shade-plugin 这么麻烦。经过验证,确实很简单。
pom.xml 依赖4.0.0 io.mybatis xxxx-skywalking 1.0 8.10.0 org.apache.skywalking apm-agent ${skywalking.version} provided ${project.artifactId}
这么简单就够了。apm-agent 中包含了 net.bytebuddy,不必使用 apm-agent-core。
实现的代码中,import 的就是 skywalking 经过 maven-shade-plugin 处理后的包。
拦截定义中的 import 示例:
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.dependencies.net.bytebuddy.description.method.MethodDescription; import org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ElementMatcher; import static org.apache.skywalking.apm.agent.core.plugin.match.MethodInheritanceAnnotationMatcher.byMethodInheritanceAnnotationMatcher; import static org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ElementMatchers.namedOneOf;
拦截方法中的 import 示例:
import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.context.tag.StringTag; import org.apache.skywalking.apm.agent.core.context.tag.Tags; import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.dependencies.com.google.gson.Gson; import org.apache.skywalking.apm.dependencies.com.google.gson.GsonBuilder; import org.apache.skywalking.apm.network.trace.component.Component; import org.apache.skywalking.apm.network.trace.component.OfficialComponent;
直接用内部的 org.apache.skywalking.apm.dependencies.com.google.gson.Gson。



