Skywalking Java 插件开发太简单了

Skywalking Java 插件开发太简单了,第1张

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

pom.xml 依赖

<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.0modelVersion>
    <groupId>io.mybatisgroupId>
    <artifactId>xxxx-skywalkingartifactId>
    <version>1.0version>

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

    <dependencies>
        
        <dependency>
            <groupId>org.apache.skywalkinggroupId>
            <artifactId>apm-agentartifactId>
            <version>${skywalking.version}version>
            <scope>providedscope>
        dependency>
    dependencies>

    <build>
        
        <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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存