专栏首页码匠的流水账聊聊skywalking的spring-annotation-plugin
原创

聊聊skywalking的spring-annotation-plugin

本文主要研究一下skywalking的spring-annotation-plugin

AbstractSpringBeanInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/AbstractSpringBeanInstrumentation.java

public abstract class AbstractSpringBeanInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
    private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.spring.annotations.SpringAnnotationInterceptor";
    public static final String INTERCEPT_GET_SKYWALKING_DYNAMIC_FIELD_METHOD = "getSkyWalkingDynamicField";
    public static final String INTERCEPT_SET_SKYWALKING_DYNAMIC_FIELD_METHOD = "setSkyWalkingDynamicField";
​
    @Override public final ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return new ConstructorInterceptPoint[0];
    }
​
    @Override public final InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {
            new DeclaredInstanceMethodsInterceptPoint() {
                @Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    return isPublic().and(not(isDeclaredBy(Object.class)).and(not(named(INTERCEPT_GET_SKYWALKING_DYNAMIC_FIELD_METHOD))).and(not(named(INTERCEPT_SET_SKYWALKING_DYNAMIC_FIELD_METHOD))));
                }
​
                @Override public String getMethodsInterceptor() {
                    return INTERCEPTOR_CLASS;
                }
​
                @Override public boolean isOverrideArgs() {
                    return false;
                }
            }
        };
    }
}
  • AbstractSpringBeanInstrumentation继承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.plugin.spring.annotations.SpringAnnotationInterceptor拦截非getSkyWalkingDynamicField及非setSkyWalkingDynamicField的public方法,至于拦截哪些类,其enhanceClass方法由子类实现

SpringBeanInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/bean/SpringBeanInstrumentation.java

public class SpringBeanInstrumentation extends AbstractSpringBeanInstrumentation {
​
    public static final String ENHANCE_ANNOTATION = "org.springframework.context.annotation.Bean";
​
    @Override protected ClassMatch enhanceClass() {
        return byClassAnnotationMatch(new String[] {ENHANCE_ANNOTATION});
    }
}
  • SpringBeanInstrumentation继承了AbstractSpringBeanInstrumentation,其enhanceClass拦截的是org.springframework.context.annotation.Bean注解的类

SpringServicesInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/services/SpringServicesInstrumentation.java

public class SpringServicesInstrumentation extends AbstractSpringBeanInstrumentation {
​
    public static final String ENHANCE_ANNOTATION = "org.springframework.stereotype.Service";
​
    @Override protected ClassMatch enhanceClass() {
        return byClassAnnotationMatch(new String[] {ENHANCE_ANNOTATION});
    }
}
  • SpringServicesInstrumentation继承了AbstractSpringBeanInstrumentation,其enhanceClass拦截的是org.springframework.stereotype.Service注解的类

SpringComponentInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/component/SpringComponentInstrumentation.java

public class SpringComponentInstrumentation extends AbstractSpringBeanInstrumentation {
​
    public static final String ENHANCE_ANNOTATION = "org.springframework.stereotype.Component";
​
    @Override protected ClassMatch enhanceClass() {
        return byClassAnnotationMatch(new String[] {ENHANCE_ANNOTATION});
    }
}
  • SpringComponentInstrumentation继承了AbstractSpringBeanInstrumentation,其enhanceClass拦截的是org.springframework.stereotype.Component注解的类

SpringRepositoryInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/repository/SpringRepositoryInstrumentation.java

public class SpringRepositoryInstrumentation extends AbstractSpringBeanInstrumentation {
​
    public static final String ENHANCE_ANNOTATION = "org.springframework.stereotype.Repository";
​
    @Override protected ClassMatch enhanceClass() {
        return byClassAnnotationMatch(new String[] {ENHANCE_ANNOTATION});
    }
}
  • SpringRepositoryInstrumentation继承了AbstractSpringBeanInstrumentation,其enhanceClass拦截的是org.springframework.stereotype.Repository注解的类

SpringAnnotationInterceptor

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-annotation-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/annotations/SpringAnnotationInterceptor.java

public class SpringAnnotationInterceptor implements InstanceMethodsAroundInterceptor {
    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
        MethodInterceptResult result) throws Throwable {
        String operationName = objInst.getClass().getName() + "." + method.getName();
        ContextManager.createLocalSpan(operationName);
    }
​
    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
        Object ret) throws Throwable {
        ContextManager.stopSpan();
        return ret;
    }
​
    @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Throwable t) {
        ContextManager.activeSpan().errorOccurred().log(t);
    }
}
  • SpringAnnotationInterceptor实现了InstanceMethodsAroundInterceptor接口,其beforeMethod方法主要是执行ContextManager.createLocalSpan(operationName);其afterMethod方法执行ContextManager.stopSpan();其handleMethodException执行ContextManager.activeSpan().errorOccurred().log(t)

小结

AbstractSpringBeanInstrumentation继承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.plugin.spring.annotations.SpringAnnotationInterceptor拦截非getSkyWalkingDynamicField及非setSkyWalkingDynamicField的public方法,至于拦截哪些类,其enhanceClass方法由子类实现

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊skywalking的kafka-plugin

    skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/kafka-plugin/src/main/resources/skyw...

    codecraft
  • 聊聊CanalEventSink

    canal-1.1.4/sink/src/main/java/com/alibaba/otter/canal/sink/CanalEventSink.java

    codecraft
  • 聊聊skywalking的spring-webflux-plugin

    skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-web...

    codecraft
  • apache的开源项目-模板引擎(Velocity)_学习了两天就上手啦_源码下载

    首先,如果你对Velocity不是很了解,还是建议你去apache的官方网站上去走走....

    Hongten
  • 用工厂方法模式来下不同订单 顶

    Controller如下,用传递的内容来判断是哪种类型的订单,并给抽象订单工厂来获取具体的订单工厂,通过具体的订单工厂来生成订单服务,完成下单功能。考虑到线程安...

    算法之名
  • Java开发小技巧_02

    jeremyxu
  • Java-内部类

    如果想从外部类的非静态方法之外的任意位置创建某个内部类的对象(在静态方法内部创建某个内部类的对象),那么必须像在main()方法中那样,具体地指明这个对象的类型...

    桑鱼
  • Dagger2系列——初识

    经过一段时间的纠结和水深火热,终于渐渐领悟了Dagger2,在此分享一下学习心得,希望同样对Dagger2水深火热的你们有点帮助。

    蜻蜓队长
  • -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

    public void write(byte[] b,int off,int len)

    noteless
  • 学界 | 模型的泛化能力仅和Hessian谱有关吗?

    经验表明,通过训练深度神经网络得到的不同局部最优值往往并不能以相同的方式泛化到未知数据集上,即使取得了相同的训练损失。近年来,这一问题日益受到经验和理论深度学习...

    机器之心

扫码关注云+社区

领取腾讯云代金券