From 9df50a0abaaa454f15d08c769cbfb127c56b0925 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Mon, 19 Nov 2018 11:24:17 +0800 Subject: [PATCH 01/10] Provide plugin for ShardingSphere (#1933) --- .../trace/component/ComponentsDefine.java | 5 +- apm-sniffer/apm-sdk-plugin/pom.xml | 1 + .../sharding-sphere-3.x-plugin/pom.xml | 66 +++++++++++ .../apm/plugin/shardingsphere/Constant.java | 24 ++++ .../shardingsphere/ExecuteInterceptor.java | 57 ++++++++++ .../shardingsphere/ParseInterceptor.java | 54 +++++++++ .../shardingsphere/RootInvokeInterceptor.java | 53 +++++++++ .../define/ExecuteInstrumentation.java | 73 ++++++++++++ .../define/ParseInstrumentation.java | 73 ++++++++++++ .../define/RootInvokeInstrumentation.java | 73 ++++++++++++ .../src/main/resources/skywalking-plugin.def | 19 ++++ .../shardingsphere/InterceptorTest.java | 105 ++++++++++++++++++ docker/config/component-libraries.yml | 3 + .../main/resources/component-libraries.yml | 3 + 14 files changed, 608 insertions(+), 1 deletion(-) create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/Constant.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ExecuteInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ParseInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ExecuteInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ParseInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/InterceptorTest.java 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 93a57a9d5894..53693935387f 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 @@ -100,6 +100,8 @@ public class ComponentsDefine { public static final OfficialComponent UNDERTOW = new OfficialComponent(49, "Undertow"); + public static final OfficialComponent SHARDING_SPHERE = new OfficialComponent(51, "ShardingSphere"); + private static ComponentsDefine INSTANCE = new ComponentsDefine(); private String[] components; @@ -109,7 +111,7 @@ public static ComponentsDefine getInstance() { } public ComponentsDefine() { - components = new String[50]; + components = new String[100]; addComponent(TOMCAT); addComponent(HTTPCLIENT); addComponent(DUBBO); @@ -125,6 +127,7 @@ public 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 ef555b379c6b..ff5c517d3e8f 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -43,6 +43,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 000000000000..d422c95f9555 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml @@ -0,0 +1,66 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 6.0.0-beta-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-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 000000000000..ca1286f255f2 --- /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 000000000000..b3c1e17f915e --- /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,57 @@ +/* + * 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.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) ((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/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 000000000000..6f97d6555302 --- /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/RootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java new file mode 100644 index 000000000000..ee59ab7b0c4c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.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 RootInvokeInterceptor} enhances {@link io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor}, creating a local span that records the overall execution of sql. + * + * @author zhangyonglun + */ +public class RootInvokeInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/RootInvoke/").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 000000000000..43f8c6effdbf --- /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/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 000000000000..d0b9e29dd714 --- /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/RootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java new file mode 100644 index 000000000000..9fbb6bd89dee --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.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 RootInvokeInstrumentation} presents that skywalking intercepts {@link io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor}. + * + * @author zhangyonglun + */ +public class RootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor"; + + private static final String ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.RootInvokeInterceptor"; + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("run"); + } + + @Override + public String getMethodsInterceptor() { + return 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 000000000000..2c1da4904d49 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,19 @@ +# 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.RootInvokeInstrumentation +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 000000000000..b688ae325329 --- /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,105 @@ +/* + * 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 RootInvokeInterceptor rootInvokeInterceptor; + + private ParseInterceptor parseInterceptor; + + private ExecuteInterceptor executeInterceptor; + + @Before + public void setUp() { + rootInvokeInterceptor = new RootInvokeInterceptor(); + parseInterceptor = new ParseInterceptor(); + executeInterceptor = new ExecuteInterceptor(); + } + + @Test + public void assertRootInvoke() { + rootInvokeInterceptor.beforeMethod(null, null, null, null, null); + rootInvokeInterceptor.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/RootInvoke/")); + } + + @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 0878d4db9cc5..0f6a3e619d88 100644 --- a/docker/config/component-libraries.yml +++ b/docker/config/component-libraries.yml @@ -177,6 +177,9 @@ http: rpc: id: 50 languages: Java,C#,Node.js +ShardingSphere: + id: 51 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only 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 0878d4db9cc5..0f6a3e619d88 100644 --- a/oap-server/server-starter/src/main/resources/component-libraries.yml +++ b/oap-server/server-starter/src/main/resources/component-libraries.yml @@ -177,6 +177,9 @@ http: rpc: id: 50 languages: Java,C#,Node.js +ShardingSphere: + id: 51 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only From 4ee2d89683845149bbc2114f822c907c087ccf7a Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Mon, 19 Nov 2018 14:45:23 +0800 Subject: [PATCH 02/10] Fix java doc (#1933) --- .../apm/plugin/shardingsphere/ExecuteInterceptor.java | 2 +- .../skywalking/apm/plugin/shardingsphere/ParseInterceptor.java | 2 +- .../apm/plugin/shardingsphere/RootInvokeInterceptor.java | 2 +- .../plugin/shardingsphere/define/RootInvokeInstrumentation.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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 index b3c1e17f915e..1a872e202fe5 100644 --- 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 @@ -29,7 +29,7 @@ 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. + * {@link ExecuteInterceptor} enhances {@link io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback}, creating a local span that records the execution of sql. * * @author zhangyonglun */ 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 index 6f97d6555302..c4857608929e 100644 --- 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 @@ -29,7 +29,7 @@ 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. + * {@link ParseInterceptor} enhances {@link io.shardingsphere.core.routing.router.sharding.ParsingSQLRouter}, creating a local span that records the parse of sql. * * @author zhangyonglun */ diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java index ee59ab7b0c4c..a1f8f052a72c 100644 --- a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java @@ -28,7 +28,7 @@ import java.lang.reflect.Method; /** - * {@link RootInvokeInterceptor} enhances {@link io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor}, creating a local span that records the overall execution of sql. + * RootInvokeInterceptor enhances io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor, creating a local span that records the overall execution of sql. * * @author zhangyonglun */ diff --git a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java index 9fbb6bd89dee..c608c8aadbdf 100644 --- a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java @@ -29,7 +29,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; /** - * {@link RootInvokeInstrumentation} presents that skywalking intercepts {@link io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor}. + * RootInvokeInstrumentation presents that skywalking intercepts io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor. * * @author zhangyonglun */ From b7025d4da49a9a791838bae44b8d5b4ddf00f417 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Thu, 22 Nov 2018 16:34:14 +0800 Subject: [PATCH 03/10] Add JDBCRootInvokeInstrumentation (#1933) --- .../sharding-sphere-3.x-plugin/pom.xml | 6 ++ .../shardingsphere/ExecuteInterceptor.java | 6 +- .../JDBCRootInvokeInterceptor.java | 53 ++++++++++++++ ...r.java => ProxyRootInvokeInterceptor.java} | 6 +- .../define/JDBCRootInvokeInstrumentation.java | 73 +++++++++++++++++++ ...va => ProxyRootInvokeInstrumentation.java} | 8 +- .../src/main/resources/skywalking-plugin.def | 3 +- .../shardingsphere/InterceptorTest.java | 27 +++++-- 8 files changed, 167 insertions(+), 15 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/JDBCRootInvokeInterceptor.java rename apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/{RootInvokeInterceptor.java => ProxyRootInvokeInterceptor.java} (85%) create mode 100644 apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/JDBCRootInvokeInstrumentation.java rename apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/{RootInvokeInstrumentation.java => ProxyRootInvokeInstrumentation.java} (85%) 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 index d422c95f9555..8def7f88cfd5 100644 --- 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 @@ -48,6 +48,12 @@ [3.0.0,4.0.0) provided + + io.shardingsphere + sharding-jdbc-core + [3.0.0,4.0.0) + provided + io.shardingsphere sharding-proxy 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 index 1a872e202fe5..b6515debbcb0 100644 --- 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 @@ -18,6 +18,7 @@ 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; @@ -38,7 +39,10 @@ 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) ((Map) allArguments[2]).get(Constant.CONTEXT_SNAPSHOT); + 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); } 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 000000000000..03fd31c194e4 --- /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/RootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java similarity index 85% rename from apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java rename to apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java index a1f8f052a72c..8a8e024147dd 100644 --- a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/RootInvokeInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/ProxyRootInvokeInterceptor.java @@ -28,15 +28,15 @@ import java.lang.reflect.Method; /** - * RootInvokeInterceptor enhances io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor, creating a local span that records the overall execution of sql. + * ProxyRootInvokeInterceptor enhances io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor, creating a local span that records the overall execution of sql. * * @author zhangyonglun */ -public class RootInvokeInterceptor implements InstanceMethodsAroundInterceptor { +public class ProxyRootInvokeInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) { - ContextManager.createLocalSpan("/ShardingSphere/RootInvoke/").setComponent(ComponentsDefine.SHARDING_SPHERE); + ContextManager.createLocalSpan("/ShardingSphere/ProxyRootInvoke/").setComponent(ComponentsDefine.SHARDING_SPHERE); ShardingExecuteDataMap.getDataMap().put(Constant.CONTEXT_SNAPSHOT, ContextManager.capture()); } 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 000000000000..b2ae3330af8d --- /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/RootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java similarity index 85% rename from apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java rename to apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java index c608c8aadbdf..69430a2b40c1 100644 --- a/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/RootInvokeInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/define/ProxyRootInvokeInstrumentation.java @@ -29,15 +29,15 @@ import static net.bytebuddy.matcher.ElementMatchers.named; /** - * RootInvokeInstrumentation presents that skywalking intercepts io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor. + * ProxyRootInvokeInstrumentation presents that skywalking intercepts io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor. * * @author zhangyonglun */ -public class RootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { +public class ProxyRootInvokeInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String ENHANCE_CLASS = "io.shardingsphere.shardingproxy.frontend.mysql.CommandExecutor"; - private static final String ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.RootInvokeInterceptor"; + private static final String PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.shardingsphere.ProxyRootInvokeInterceptor"; @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { @@ -50,7 +50,7 @@ public ElementMatcher getMethodsMatcher() { @Override public String getMethodsInterceptor() { - return ROOT_INVOKE_INTERCEPTOR_CLASS; + return PROXY_ROOT_INVOKE_INTERCEPTOR_CLASS; } @Override 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 index 2c1da4904d49..a9b138431d06 100644 --- 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 @@ -14,6 +14,7 @@ # 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.RootInvokeInstrumentation +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 index b688ae325329..d2875ac68367 100644 --- 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 @@ -51,7 +51,9 @@ public class InterceptorTest { @Rule public AgentServiceRule serviceRule = new AgentServiceRule(); - private RootInvokeInterceptor rootInvokeInterceptor; + private ProxyRootInvokeInterceptor proxyRootInvokeInterceptor; + + private JDBCRootInvokeInterceptor jdbcRootInvokeInterceptor; private ParseInterceptor parseInterceptor; @@ -59,21 +61,34 @@ public class InterceptorTest { @Before public void setUp() { - rootInvokeInterceptor = new RootInvokeInterceptor(); + proxyRootInvokeInterceptor = new ProxyRootInvokeInterceptor(); + jdbcRootInvokeInterceptor = new JDBCRootInvokeInterceptor(); parseInterceptor = new ParseInterceptor(); executeInterceptor = new ExecuteInterceptor(); } @Test - public void assertRootInvoke() { - rootInvokeInterceptor.beforeMethod(null, null, null, null, null); - rootInvokeInterceptor.afterMethod(null, null, null, null, null); + 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/RootInvoke/")); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/JDBCRootInvoke/")); } @Test From d8b4eee3521f3a1ab2ed664ad4f541a337a71ae1 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Mon, 14 Jan 2019 13:02:20 +0800 Subject: [PATCH 04/10] Update to 6.0.0-GA-SNAPSHOT (#1933) --- apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 8def7f88cfd5..7b3356f8686b 100644 --- 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 @@ -21,7 +21,7 @@ apm-sdk-plugin org.apache.skywalking - 6.0.0-beta-SNAPSHOT + 6.0.0-GA-SNAPSHOT 4.0.0 From d9e5e67eb1bca8dc01f5b27d0f429655f76410cf Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Mon, 14 Jan 2019 17:25:55 +0800 Subject: [PATCH 05/10] Update io.shardingsphere version to [3.0.0,) (#1933) --- .../apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 7b3356f8686b..851527e8e258 100644 --- 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 @@ -45,19 +45,19 @@ io.shardingsphere sharding-core - [3.0.0,4.0.0) + [3.0.0,) provided io.shardingsphere sharding-jdbc-core - [3.0.0,4.0.0) + [3.0.0,) provided io.shardingsphere sharding-proxy - [3.0.0,4.0.0) + [3.0.0,) provided From ab5538ed41f8c3b1432b845027cdb9376fc04e66 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Mon, 14 Jan 2019 20:42:34 +0800 Subject: [PATCH 06/10] Update io.shardingsphere version to [3.0.0,4.0.0) (#1933) --- .../apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 851527e8e258..7b3356f8686b 100644 --- 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 @@ -45,19 +45,19 @@ io.shardingsphere sharding-core - [3.0.0,) + [3.0.0,4.0.0) provided io.shardingsphere sharding-jdbc-core - [3.0.0,) + [3.0.0,4.0.0) provided io.shardingsphere sharding-proxy - [3.0.0,) + [3.0.0,4.0.0) provided From f3215078259c987b796173754dcad28424fee7df Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Thu, 11 Apr 2019 12:35:54 +0800 Subject: [PATCH 07/10] Resolve conflict (#1933) --- .../apm/network/trace/component/ComponentsDefine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 273d9fb42b0e..e9a09b12bf31 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 @@ -114,7 +114,7 @@ public class ComponentsDefine { public static final OfficialComponent ZOOKEEPER = new OfficialComponent(58, "Zookeeper"); - public static final OfficialComponent SHARDING_SPHERE = new OfficialComponent(57, "ShardingSphere"); + public static final OfficialComponent SHARDING_SPHERE = new OfficialComponent(59, "ShardingSphere"); private static ComponentsDefine INSTANCE = new ComponentsDefine(); From 96fd0a004436a5ed77931ba0d8ce3521f5fba61e Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Thu, 11 Apr 2019 12:52:07 +0800 Subject: [PATCH 08/10] Update to 6.1.0-SNAPSHOT (#1933) --- apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 7b3356f8686b..6c73539ae8be 100644 --- 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 @@ -21,7 +21,7 @@ apm-sdk-plugin org.apache.skywalking - 6.0.0-GA-SNAPSHOT + 6.1.0-SNAPSHOT 4.0.0 From 45cc03821e55369d909a291c94d32fdf946e7198 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Tue, 30 Apr 2019 15:58:26 +0800 Subject: [PATCH 09/10] Update to 6.2.0-SNAPSHOT (#1933) --- apm-sniffer/apm-sdk-plugin/sharding-sphere-3.x-plugin/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6c73539ae8be..b488c9b91243 100644 --- 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 @@ -21,7 +21,7 @@ apm-sdk-plugin org.apache.skywalking - 6.1.0-SNAPSHOT + 6.2.0-SNAPSHOT 4.0.0 From 8bf4705c69f00303c63f35e29b301abb8eb74c29 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Wed, 15 May 2019 17:23:35 +0800 Subject: [PATCH 10/10] Update Supported-list --- docs/en/setup/service-agent/java-agent/Supported-list.md | 1 + 1 file changed, 1 insertion(+) 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 6621fbd941aa..2376cb79cabe 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