Skywalking Java 插件开发太简单了

Posted isea533

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Skywalking Java 插件开发太简单了相关的知识,希望对你有一定的参考价值。

在写 Skywalking Java 插件开发注意点 时,就感觉不舒服,扩展不应该这么麻烦,既然 skywalking-agent 中存在第三方的类,直接用不就好了吗,何必用 maven-shade-plugin 这么麻烦。经过验证,确实很简单。

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>io.mybatis</groupId>
    <artifactId>xxxx-skywalking</artifactId>
    <version>1.0</version>

    <properties>
        <skywalking.version>8.10.0</skywalking.version>
    </properties>

    <dependencies>
        <!-- TODO 你自己的依赖 -->
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-agent</artifactId>
            <version>$skywalking.version</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- 插件打包可以不带版本号,避免放入plugins多个版本 -->
        <finalName>$project.artifactId</finalName>
    </build>
</project>

这么简单就够了。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

以上是关于Skywalking Java 插件开发太简单了的主要内容,如果未能解决你的问题,请参考以下文章

过时Skywalking Java 插件开发注意点

过时Skywalking Java 插件开发注意点

skywalking插件工作原理剖析

Skywalking系列博客3-Java Agent插件

Skywalking系列博客3-Java Agent插件

skywalking plugin 开发初探 ONS plugin 实践