本文主要研究一下skywalking的sharding-sphere-plugin
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/resources/skywalking-plugin.def
sharding-sphere-4.x=org.apache.skywalking.apm.plugin.shardingsphere.v4.define.ProxyRootInvokeInstrumentation sharding-sphere-4.x=org.apache.skywalking.apm.plugin.shardingsphere.v4.define.JDBCRootInvokeInstrumentation sharding-sphere-4.x=org.apache.skywalking.apm.plugin.shardingsphere.v4.define.ParseInstrumentation sharding-sphere-4.x=org.apache.skywalking.apm.plugin.shardingsphere.v4.define.ExecuteInstrumentation
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/define/ProxyRootInvokeInstrumentation.java
public class ProxyRootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "org.apache.shardingsphere.shardingproxy.frontend.command.CommandExecutorTask"; private static final String PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.v4.ProxyRootInvokeInterceptor"; @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[]{ new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("run"); } @Override public String getMethodsInterceptor() { return PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { return false; } } }; } @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; } @Override protected ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/ProxyRootInvokeInterceptor.java
public class ProxyRootInvokeInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { ContextManager.createLocalSpan("/ShardingSphere/ProxyRootInvoke/").setComponent(ComponentsDefine.SHARDING_SPHERE); ShardingExecuteDataMap.getDataMap().put(Constant.CONTEXT_SNAPSHOT, ContextManager.capture()); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { ContextManager.stopSpan(); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { ContextManager.activeSpan().errorOccurred().log(t); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/define/JDBCRootInvokeInstrumentation.java
public class JDBCRootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "org.apache.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor"; private static final String JDBC_ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.v4.JDBCRootInvokeInterceptor"; @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[]{ new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("executeCallback"); } @Override public String getMethodsInterceptor() { return JDBC_ROOT_INVOKE_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { return false; } } }; } @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; } @Override protected ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/JDBCRootInvokeInterceptor.java
public class JDBCRootInvokeInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { ContextManager.createLocalSpan("/ShardingSphere/JDBCRootInvoke/").setComponent(ComponentsDefine.SHARDING_SPHERE); ShardingExecuteDataMap.getDataMap().put(Constant.CONTEXT_SNAPSHOT, ContextManager.capture()); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { ContextManager.stopSpan(); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { ContextManager.activeSpan().errorOccurred().log(t); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/define/ParseInstrumentation.java
public class ParseInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter"; private static final String EXECUTE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.v4.ParseInterceptor"; @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[]{ new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("parse"); } @Override public String getMethodsInterceptor() { return EXECUTE_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { return false; } } }; } @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; } @Override protected ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/ParseInterceptor.java
public class ParseInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { AbstractSpan span = ContextManager.createLocalSpan("/ShardingSphere/parseSQL/").setComponent(ComponentsDefine.SHARDING_SPHERE); Tags.DB_STATEMENT.set(span, (String) allArguments[0]); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { ContextManager.stopSpan(); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { ContextManager.activeSpan().errorOccurred().log(t); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/define/ExecuteInstrumentation.java
public class ExecuteInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteCallback"; private static final String EXECUTE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.v4.ExecuteInterceptor"; @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[]{ new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("execute0"); } @Override public String getMethodsInterceptor() { return EXECUTE_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { return false; } } }; } @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; } @Override protected ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/sharding-sphere-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v4/ExecuteInterceptor.java
public class ExecuteInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { ContextManager.createLocalSpan("/ShardingSphere/executeSQL/").setComponent(ComponentsDefine.SHARDING_SPHERE); ContextSnapshot contextSnapshot = (ContextSnapshot) ShardingExecuteDataMap.getDataMap().get(Constant.CONTEXT_SNAPSHOT); if (null == contextSnapshot) { contextSnapshot = (ContextSnapshot) ((Map) allArguments[2]).get(Constant.CONTEXT_SNAPSHOT); } if (null != contextSnapshot) { ContextManager.continued(contextSnapshot); } } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { ContextManager.stopSpan(); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { ContextManager.activeSpan().errorOccurred().log(t); } }
skywalking的sharding-sphere-plugin提供了ProxyRootInvokeInstrumentation、JDBCRootInvokeInstrumentation、ParseInstrumentation、ExecuteInstrumentation这几个增强