本文主要研究一下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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论