聊聊skywalking的spring-annotation-plugin

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

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.javagit

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.javagithub

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.javaspring

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.javaapache

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.javabash

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.javaide

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方法由子類實現ui

doc

相關文章
相關標籤/搜索