From 75d8dc6f68b203a465a87c34e2f21ab356d07c7b Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sat, 2 Dec 2017 11:43:27 +0800 Subject: [PATCH] Change the weave type of postgresql plugin --- .../jdbc}/define/StatementEnhanceInfos.java | 2 +- .../CreateCallableStatementInterceptor.java | 2 +- .../CreatePreparedStatementInterceptor.java | 2 +- .../mysql/CreateStatementInterceptor.java | 2 +- .../StatementExecuteMethodsInterceptor.java | 2 +- ...tatementExecuteMethodsInterceptorTest.java | 2 +- .../CreateCallableStatementInterceptor.java | 54 ++++++++++++ .../CreatePreparedStatementInterceptor.java | 54 ++++++++++++ .../CreateStatementInterceptor.java | 57 +++++++++++++ .../StatementExecuteMethodsInterceptor.java | 75 ++++++++++++++++ ...AbstractJdbc2StatementInstrumentation.java | 85 +++++++++++++++++++ .../define/ConnectionInstrumentation.java | 12 +-- .../jdbc/postgresql/define/Constants.java | 31 +++++++ .../Jdbc3ConnectionInstrumentation.java | 15 ++-- .../Jdbc4ConnectionInstrumentation.java | 8 +- .../src/main/resources/skywalking-plugin.def | 1 + 16 files changed, 381 insertions(+), 23 deletions(-) rename apm-sniffer/apm-sdk-plugin/{mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql => jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc}/define/StatementEnhanceInfos.java (96%) create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/define/StatementEnhanceInfos.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java similarity index 96% rename from apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/define/StatementEnhanceInfos.java rename to apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java index 4a6b8fbc42..739c63e77f 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/define/StatementEnhanceInfos.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/skywalking/apm/plugin/jdbc/define/StatementEnhanceInfos.java @@ -16,7 +16,7 @@ * Project repository: https://github.com/OpenSkywalking/skywalking */ -package org.skywalking.apm.plugin.jdbc.mysql.define; +package org.skywalking.apm.plugin.jdbc.define; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateCallableStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateCallableStatementInterceptor.java index 5aaa176ab7..cf07f9d832 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateCallableStatementInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateCallableStatementInterceptor.java @@ -22,7 +22,7 @@ import java.lang.reflect.Method; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; /** diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreatePreparedStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreatePreparedStatementInterceptor.java index ca4c7deedb..2f41e3a5f6 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreatePreparedStatementInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreatePreparedStatementInterceptor.java @@ -22,7 +22,7 @@ import java.lang.reflect.Method; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; /** diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateStatementInterceptor.java index 564254b5cf..490c1d9c1e 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateStatementInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/CreateStatementInterceptor.java @@ -22,7 +22,7 @@ import java.lang.reflect.Method; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; /** diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java index 779ae38b0e..015a3da301 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptor.java @@ -26,7 +26,7 @@ import org.skywalking.apm.agent.core.context.trace.SpanLayer; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; -import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; /** diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java index 8c8425c6f0..86efbe7772 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/skywalking/apm/plugin/jdbc/mysql/StatementExecuteMethodsInterceptorTest.java @@ -37,7 +37,7 @@ import org.skywalking.apm.agent.test.tools.SegmentStoragePoint; import org.skywalking.apm.agent.test.tools.SpanAssert; import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; import org.skywalking.apm.network.trace.component.ComponentsDefine; -import org.skywalking.apm.plugin.jdbc.mysql.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; import static org.hamcrest.CoreMatchers.is; diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java new file mode 100644 index 0000000000..60f4d1512f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateCallableStatementInterceptor.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql; + +import java.lang.reflect.Method; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; + +/** + * {@link CreateStatementInterceptor} intercepts the {@link org.postgresql.jdbc.PgConnection#prepareCall} method in + * {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class. + * + * @author zhangxin + */ +public class CreateCallableStatementInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + if (ret instanceof EnhancedInstance) { + ((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), (String)allArguments[0], "CallableStatement")); + } + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java new file mode 100644 index 0000000000..ea8972fc66 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreatePreparedStatementInterceptor.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql; + +import java.lang.reflect.Method; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; + +/** + * {@link CreatePreparedStatementInterceptor} intercepts the {@link org.postgresql.jdbc.PgConnection#prepareStatement} method in + * {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class. + * + * @author zhangxin + */ +public class CreatePreparedStatementInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + if (ret instanceof EnhancedInstance) { + ((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), (String)allArguments[0], "PreparedStatement")); + } + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java new file mode 100644 index 0000000000..c26f7acf11 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/CreateStatementInterceptor.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql; + +import java.lang.reflect.Method; +import org.postgresql.jdbc.PgConnection; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; + +/** + * {@link CreateStatementInterceptor} intercepts the {@link PgConnection#createStatement} method in + * {@link org.postgresql.jdbc.PgConnection}, {@link org.postgresql.jdbc.PgConnection} or {@link org.postgresql.jdbc3.Jdbc3Connection} class. + * + * @author zhangxin + */ +public class CreateStatementInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) throws Throwable { + + if (ret instanceof EnhancedInstance) { + ((EnhancedInstance)ret).setSkyWalkingDynamicField(new StatementEnhanceInfos((ConnectionInfo)objInst.getSkyWalkingDynamicField(), "", "CallableStatement")); + } + + return ret; + } + + @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java new file mode 100644 index 0000000000..aab57d3955 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/StatementExecuteMethodsInterceptor.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql; + +import java.lang.reflect.Method; +import org.skywalking.apm.agent.core.context.ContextManager; +import org.skywalking.apm.agent.core.context.tag.Tags; +import org.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.skywalking.apm.plugin.jdbc.define.StatementEnhanceInfos; +import org.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; + +/** + * {@link StatementExecuteMethodsInterceptor} create the exit span when the client call the interceptor methods. + * + * @author zhangxin + */ +public class StatementExecuteMethodsInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField(); + ConnectionInfo connectInfo = cacheObject.getConnectionInfo(); + AbstractSpan span = ContextManager.createExitSpan(buildOperationName(connectInfo, method.getName(), cacheObject.getStatementName()), connectInfo.getDatabasePeer()); + Tags.DB_TYPE.set(span, "sql"); + Tags.DB_INSTANCE.set(span, connectInfo.getDatabaseName()); + Tags.DB_STATEMENT.set(span, cacheObject.getSql()); + span.setComponent(connectInfo.getComponent()); + + SpanLayer.asDB(span); + } + + @Override + public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + Object ret) throws Throwable { + StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField(); + if (cacheObject.getConnectionInfo() != null) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + StatementEnhanceInfos cacheObject = (StatementEnhanceInfos)objInst.getSkyWalkingDynamicField(); + if (cacheObject.getConnectionInfo() != null) { + ContextManager.activeSpan().errorOccurred().log(t); + } + } + + private String buildOperationName(ConnectionInfo connectionInfo, String methodName, String statementName) { + return connectionInfo.getDBType() + "/JDBI/" + statementName + "/" + methodName; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java new file mode 100644 index 0000000000..b46f90cb8e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java @@ -0,0 +1,85 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * {@link AbstractJdbc2StatementInstrumentation} intercept the following methods that the class which extend {@link + * org.postgresql.jdbc2.AbstractJdbc2Statement} by {@link org.skywalking.apm.plugin.jdbc.postgresql.StatementExecuteMethodsInterceptor}.
+ * 1. the execute with non parameter + * 2. the execute with one parameter + * 3. the executeBatch + * 4. the executeQuery with non parameter + * 5. the executeQuery with one parameter + * 6. the executeUpdate with non parameter + * 7. the executeUpdate with one parameter + * 8. the addBatch with non parameter + * 9. the addBatch with one parameter + * + * @author zhangxin + */ +public class AbstractJdbc2StatementInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "org.postgresql.jdbc2.AbstractJdbc2Statement"; + private static final String INTERCEPTOR_CLASS = "org.skywalking.apm.plugin.jdbc.postgresql.StatementExecuteMethodsInterceptor"; + + @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named("execute").and(takesArguments(0)) + .or(named("execute").and(takesArguments(1))) + .or(named("executeBatch")) + .or(named("executeQuery").and(takesArguments(0))) + .or(named("executeQuery").and(takesArguments(1))) + .or(named("executeUpdate").and(takesArguments(0))) + .or(named("executeUpdate").and(takesArguments(1))) + .or(named("addBatch").and(takesArguments(1))) + .or(named("addBatch").and(takesArguments(0))); + } + + @Override public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java index df520a9e8d..8c670cc151 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java @@ -31,15 +31,15 @@ import static org.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMat import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; import static org.skywalking.apm.plugin.jdbc.define.Constants.CLOSE_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.COMMIT_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.RELEASE_SAVE_POINT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.ROLLBACK_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.SERVICE_METHOD_INTERCEPT_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_STATEMENT_INTERCEPTOR_CLASS; /** * {@link ConnectionInstrumentation} intercept the following methods that the class which extend {@link @@ -74,7 +74,7 @@ public class ConnectionInstrumentation extends ClassInstanceMethodsEnhancePlugin } @Override public String getMethodsInterceptor() { - return PREPARE_STATEMENT_INTERCEPT_CLASS; + return CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -100,7 +100,7 @@ public class ConnectionInstrumentation extends ClassInstanceMethodsEnhancePlugin } @Override public String getMethodsInterceptor() { - return PREPARE_CALL_INTERCEPT_CLASS; + return CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -113,7 +113,7 @@ public class ConnectionInstrumentation extends ClassInstanceMethodsEnhancePlugin } @Override public String getMethodsInterceptor() { - return CREATE_STATEMENT_INTERCEPT_CLASS; + return CREATE_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java new file mode 100644 index 0000000000..78d3e70554 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Constants.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.postgresql.define; + +/** + * Interceptor class name constant variable + * + * @author zhangxin + */ +public class Constants { + public static final String CREATE_STATEMENT_INTERCEPTOR_CLASS = "org.skywalking.apm.plugin.jdbc.postgresql.CreateStatementInterceptor"; + public static final String CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS = "org.skywalking.apm.plugin.jdbc.postgresql.CreatePreparedStatementInterceptor"; + public static final String CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS = "org.skywalking.apm.plugin.jdbc.postgresql.CreateCallableStatementInterceptor"; + +} diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java index 191d8607b1..2c26d34368 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc3ConnectionInstrumentation.java @@ -30,15 +30,15 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; import static org.skywalking.apm.plugin.jdbc.define.Constants.CLOSE_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.COMMIT_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.RELEASE_SAVE_POINT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.ROLLBACK_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.SERVICE_METHOD_INTERCEPT_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_STATEMENT_INTERCEPTOR_CLASS; /** * {@link Jdbc3ConnectionInstrumentation} intercept the following methods that the class which extend {@link @@ -69,7 +69,7 @@ public class Jdbc3ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP } @Override public String getMethodsInterceptor() { - return PREPARE_STATEMENT_INTERCEPT_CLASS; + return CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -82,7 +82,7 @@ public class Jdbc3ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP } @Override public String getMethodsInterceptor() { - return PREPARE_CALL_INTERCEPT_CLASS; + return CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -95,7 +95,7 @@ public class Jdbc3ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP } @Override public String getMethodsInterceptor() { - return CREATE_STATEMENT_INTERCEPT_CLASS; + return CREATE_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -104,7 +104,8 @@ public class Jdbc3ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP }, new InstanceMethodsInterceptPoint() { @Override public ElementMatcher getMethodsMatcher() { - return named(COMMIT_METHOD_NAME).or(named(ROLLBACK_METHOD_NAME)).or(named(CLOSE_METHOD_NAME)).or(named(RELEASE_SAVE_POINT_METHOD_NAME)); + return named(COMMIT_METHOD_NAME).or(named(ROLLBACK_METHOD_NAME)).or(named(CLOSE_METHOD_NAME)) + .or(named(RELEASE_SAVE_POINT_METHOD_NAME)); } @Override public String getMethodsInterceptor() { diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java index 7be5b62f60..c3507bc437 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/postgresql/define/Jdbc4ConnectionInstrumentation.java @@ -32,13 +32,13 @@ import static org.skywalking.apm.plugin.jdbc.define.Constants.CLOSE_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.COMMIT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_METHOD_NAME; -import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_INTERCEPT_CLASS; import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.RELEASE_SAVE_POINT_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.ROLLBACK_METHOD_NAME; import static org.skywalking.apm.plugin.jdbc.define.Constants.SERVICE_METHOD_INTERCEPT_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; +import static org.skywalking.apm.plugin.jdbc.postgresql.define.Constants.CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; /** * {@link Jdbc4ConnectionInstrumentation} intercept the following methods that the class which extend {@link @@ -69,7 +69,7 @@ public class Jdbc4ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP } @Override public String getMethodsInterceptor() { - return PREPARE_STATEMENT_INTERCEPT_CLASS; + return CREATE_PREPARED_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { @@ -82,7 +82,7 @@ public class Jdbc4ConnectionInstrumentation extends ClassInstanceMethodsEnhanceP } @Override public String getMethodsInterceptor() { - return PREPARE_CALL_INTERCEPT_CLASS; + return CREATE_CALLABLE_STATEMENT_INTERCEPTOR_CLASS; } @Override public boolean isOverrideArgs() { diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def index 3fe0ad09bc..b069baff8a 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/resources/skywalking-plugin.def @@ -2,3 +2,4 @@ postgresql-8.x=org.skywalking.apm.plugin.jdbc.postgresql.define.DriverInstrument postgresql-8.x=org.skywalking.apm.plugin.jdbc.postgresql.define.Jdbc3ConnectionInstrumentation postgresql-8.x=org.skywalking.apm.plugin.jdbc.postgresql.define.Jdbc4ConnectionInstrumentation postgresql-8.x=org.skywalking.apm.plugin.jdbc.postgresql.define.ConnectionInstrumentation +postgresql-8.x=org.skywalking.apm.plugin.jdbc.postgresql.define.AbstractJdbc2StatementInstrumentation -- GitLab