聊聊skywalking的springannotationplugin
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
- AbstractSpringBeanInstrumentation
- SpringAnnotationInterceptor
以上是 聊聊skywalking的springannotationplugin 的全部内容, 来源链接: utcz.com/z/514174.html