本文主要研究一下skywalking的spring-annotation-pluginjava
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; } } }; } }
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}); } }
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}); } }
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}); } }
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.javaide
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}); } }
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.javacode
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); } }
AbstractSpringBeanInstrumentation繼承了ClassInstanceMethodsEnhancePluginDefine,它使用org.apache.skywalking.apm.plugin.spring.annotations.SpringAnnotationInterceptor攔截非getSkyWalkingDynamicField及非setSkyWalkingDynamicField的public方法,至於攔截哪些類,其enhanceClass方法由子類實現component