diff --git a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java index 2a3116148669da3e05714cc1c9af7d4cd1e78b71..8d6f16851978418cf994caaec52dab6f885ad8c9 100644 --- a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java +++ b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java @@ -116,6 +116,8 @@ public class ComponentsDefine { public static final OfficialComponent VERTX = new OfficialComponent(59, "Vert.x"); + public static final OfficialComponent SHARDING_SPHERE = new OfficialComponent(60, "ShardingSphere"); + private static ComponentsDefine INSTANCE = new ComponentsDefine(); private String[] components; @@ -125,7 +127,7 @@ public class ComponentsDefine { } public ComponentsDefine() { - components = new String[60]; + components = new String[100]; addComponent(TOMCAT); addComponent(HTTPCLIENT); addComponent(DUBBO); @@ -141,6 +143,7 @@ public class ComponentsDefine { addComponent(JETTY_CLIENT); addComponent(JETTY_SERVER); addComponent(SHARDING_JDBC); + addComponent(SHARDING_SPHERE); addComponent(GRPC); addComponent(ELASTIC_JOB); addComponent(HTTP_ASYNC_CLIENT); diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index ff5904e88c478b7cf3ffa5d4ce5742afc76a73a8..1bd9a70cdc96ef318d8204e21f650da9e9798630 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -44,6 +44,7 @@ jetty-plugin spymemcached-2.x-plugin sharding-jdbc-1.5.x-plugin + sharding-sphere-3.x-plugin xmemcached-2.x-plugin grpc-1.x-plugin mysql-5.x-plugin diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..b488c9b91243d6605a83408b45a007e18524093a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml @@ -0,0 +1,72 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 6.2.0-SNAPSHOT + + 4.0.0 + + apm-sharding-sphere-3.x-plugin + jar + + sharding-sphere-3.x-plugin + http://maven.apache.org + + + UTF-8 + + + + + mysql + mysql-connector-java + [2.0.14,6.0.6] + test + + + io.shardingsphere + sharding-core + [3.0.0,4.0.0) + provided + + + io.shardingsphere + sharding-jdbc-core + [3.0.0,4.0.0) + provided + + + io.shardingsphere + sharding-proxy + [3.0.0,4.0.0) + provided + + + + + + + maven-deploy-plugin + + + + diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/Constant.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/Constant.java new file mode 100644 index 0000000000000000000000000000000000000000..ca1286f255f2a9b270736f834bf4e1fdf9b0e5cc --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/Constant.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere; + +public final class Constant { + + public static final String CONTEXT_SNAPSHOT = "CONTEXT_SNAPSHOT"; +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ExecuteInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..b6515debbcb06e29824dd55d9b4eef0edb389b96 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ExecuteInterceptor.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere; + +import io.shardingsphere.core.executor.ShardingExecuteDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; +import java.util.Map; + +/** + * {@link ExecuteInterceptor} enhances {@link io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback}, creating a local span that records the execution of sql. + * + * @author zhangyonglun + */ +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); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/JDBCRootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/JDBCRootInvokeInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..03fd31c194e42b05c8a96e9aef82287545852257 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/JDBCRootInvokeInterceptor.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere; + +import io.shardingsphere.core.executor.ShardingExecuteDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * {@link JDBCRootInvokeInterceptor} enhances {@link io.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor}, creating a local span that records the overall execution of sql. + * + * @author zhangyonglun + */ +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); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ParseInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ParseInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..c4857608929ea1f63fe0c2868970967cf6f37f53 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ParseInterceptor.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * {@link ParseInterceptor} enhances {@link io.shardingsphere.core.routing.router.sharding.ParsingSQLRouter}, creating a local span that records the parse of sql. + * + * @author zhangyonglun + */ +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); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..8a8e024147dd49be2a8580f56ffb35c1b4d0b02b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere; + +import io.shardingsphere.core.executor.ShardingExecuteDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * ProxyRootInvokeInterceptor enhances io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor, creating a local span that records the overall execution of sql. + * + * @author zhangyonglun + */ +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); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ExecuteInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ExecuteInstrumentation.java new file mode 100644 index 0000000000000000000000000000000000000000..43f8c6effdbfbe272a5d32539f730447b7e68cce --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ExecuteInstrumentation.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ExecuteInstrumentation} presents that skywalking intercepts {@link io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback}. + * + * @author zhangyonglun + */ +public class ExecuteInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback"; + + private static final String EXECUTE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.ExecuteInterceptor"; + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("execute0"); + } + + @Override + public String getMethodsInterceptor() { + return EXECUTE_INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/JDBCRootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/JDBCRootInvokeInstrumentation.java new file mode 100644 index 0000000000000000000000000000000000000000..b2ae3330af8d1d11f71f3aab4d21c177000c199b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/JDBCRootInvokeInstrumentation.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link JDBCRootInvokeInstrumentation} presents that skywalking intercepts {@link io.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor}. + * + * @author zhangyonglun + */ +public class JDBCRootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "io.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor"; + + private static final String JDBC_ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.JDBCRootInvokeInterceptor"; + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("executeCallback"); + } + + @Override + public String getMethodsInterceptor() { + return JDBC_ROOT_INVOKE_INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ParseInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ParseInstrumentation.java new file mode 100644 index 0000000000000000000000000000000000000000..d0b9e29dd7145d1f7cb831999bbe107edf1f1324 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ParseInstrumentation.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ParseInstrumentation} presents that skywalking intercepts {@link io.shardingsphere.core.routing.router.sharding.ParsingSQLRouter}. + * + * @author zhangyonglun + */ +public class ParseInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "io.shardingsphere.core.routing.router.sharding.ParsingSQLRouter"; + + private static final String EXECUTE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.ParseInterceptor"; + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("parse"); + } + + @Override + public String getMethodsInterceptor() { + return EXECUTE_INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java new file mode 100644 index 0000000000000000000000000000000000000000..69430a2b40c10a1162f2646ac531bda2a813a35a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.shardingsphere.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * ProxyRootInvokeInstrumentation presents that skywalking intercepts io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor. + * + * @author zhangyonglun + */ +public class ProxyRootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor"; + + private static final String PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.ProxyRootInvokeInterceptor"; + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("run"); + } + + @Override + public String getMethodsInterceptor() { + return PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000000000000000000000000000000000..a9b138431d0634ac177f463b6b58e4093edb674f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +sharding-sphere-3.x=org.apache.skywalking.apm.plugin.shardingsphere.define.ProxyRootInvokeInstrumentation +sharding-sphere-3.x=org.apache.skywalking.apm.plugin.shardingsphere.define.JDBCRootInvokeInstrumentation +sharding-sphere-3.x=org.apache.skywalking.apm.plugin.shardingsphere.define.ParseInstrumentation +sharding-sphere-3.x=org.apache.skywalking.apm.plugin.shardingsphere.define.ExecuteInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/InterceptorTest.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/InterceptorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d2875ac68367804476becb835b56262b3c191dff --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/InterceptorTest.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.skywalking.apm.plugin.shardingsphere; + +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; +import org.apache.skywalking.apm.agent.test.tools.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.util.HashMap; +import java.util.List; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class InterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private ProxyRootInvokeInterceptor proxyRootInvokeInterceptor; + + private JDBCRootInvokeInterceptor jdbcRootInvokeInterceptor; + + private ParseInterceptor parseInterceptor; + + private ExecuteInterceptor executeInterceptor; + + @Before + public void setUp() { + proxyRootInvokeInterceptor = new ProxyRootInvokeInterceptor(); + jdbcRootInvokeInterceptor = new JDBCRootInvokeInterceptor(); + parseInterceptor = new ParseInterceptor(); + executeInterceptor = new ExecuteInterceptor(); + } + + @Test + public void assertProxyRootInvoke() { + proxyRootInvokeInterceptor.beforeMethod(null, null, null, null, null); + proxyRootInvokeInterceptor.afterMethod(null, null, null, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/ProxyRootInvoke/")); + } + + @Test + public void assertJDBCRootInvoke() { + jdbcRootInvokeInterceptor.beforeMethod(null, null, null, null, null); + jdbcRootInvokeInterceptor.afterMethod(null, null, null, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/JDBCRootInvoke/")); + } + + @Test + public void assertParse() { + Object[] allArguments = new Object[] {"SELECT * FROM t_order", false}; + parseInterceptor.beforeMethod(null, null, allArguments, null, null); + parseInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/parseSQL/")); + SpanAssert.assertTag(spans.get(0), 0, "SELECT * FROM t_order"); + } + + @Test + public void assertExecute() { + Object[] allArguments = new Object[] {null, null, new HashMap()}; + executeInterceptor.beforeMethod(null, null, allArguments, null, null); + executeInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/executeSQL/")); + } +} diff --git a/docker/config/component-libraries.yml b/docker/config/component-libraries.yml index 767e636491aa23ffd31a28de6e007e2182c7df65..1b71218766793767f138a47cd6c149673354c54c 100644 --- a/docker/config/component-libraries.yml +++ b/docker/config/component-libraries.yml @@ -207,6 +207,9 @@ Zookeeper: Vertx: id: 59 languages: Java +ShardingSphere: + id: 60 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only diff --git a/docs/en/setup/service-agent/java-agent/Supported-list.md b/docs/en/setup/service-agent/java-agent/Supported-list.md index 6621fbd941aa43afa5af9b6f5e2d627e6da39377..2376cb79cabe8cb91e996115ec714f96671def27 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -24,6 +24,7 @@ * Oracle Driver (Optional¹) * H2 Driver 1.3.x -> 1.4.x * [Sharding-JDBC](https://github.com/shardingjdbc/sharding-jdbc) 1.5.x + * [ShardingSphere](https://github.com/apache/incubator-shardingsphere) 3.0.0 * PostgreSQL Driver 8.x, 9.x, 42.x * RPC Frameworks * [Dubbo](https://github.com/alibaba/dubbo) 2.5.4 -> 2.6.0 diff --git a/oap-server/server-starter/src/main/resources/component-libraries.yml b/oap-server/server-starter/src/main/resources/component-libraries.yml index 8759266baeed84db4d0b9ec0c899a8e5384fa483..0f674987b0ca4656e4fc341eb9c42b9eb71124dd 100644 --- a/oap-server/server-starter/src/main/resources/component-libraries.yml +++ b/oap-server/server-starter/src/main/resources/component-libraries.yml @@ -208,6 +208,10 @@ Vertx: id: 59 languages: Java +ShardingSphere: + id: 59 + languages: Java + # .NET/.NET Core components # [3000, 4000) for C#/.NET only AspNetCore: