From 97b577864f3145046b791ab41feeba009a22eab3 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Thu, 11 Jan 2018 16:08:48 +0800 Subject: [PATCH 1/2] [Agent] Support jdbc4, jdbc3 and jdbc42 of postgresql driver --- .../define/ConnectionInstrumentation.java | 9 ++- .../define/MultiClassNameMatch.java | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java index 24cc182e0..bb0d70ee4 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java @@ -29,7 +29,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; -import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; +import static org.apache.skywalking.apm.plugin.jdbc.postgresql.define.MultiClassNameMatch.byMultiClassMatch; /** * {@link ConnectionInstrumentation} intercept the following methods that the class which extend {@link @@ -49,8 +49,11 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName public class ConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String PREPARE_STATEMENT_METHOD_WITH_STRING_ARRAY_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.jdbc.postgresql.JDBCPrepareStatementWithStringArrayInterceptor"; - public static final String ENHANCE_CLASS = "org.postgresql.jdbc.PgConnection"; + public static final String PG_CONNECTION_ENHANCE_CLASS = "org.postgresql.jdbc.PgConnection"; public static final String STRING_ARRAY_ARGUMENT_TYPE = "java.lang.String[]"; + public static final String PG_JDBC42_CONNECTION_ENHANCE_CLASS = "org.postgresql.jdbc42.Jdbc42Connection"; + public static final String PG_JDBC3_CONNECTION_ENHANCE_CLASS = "org.postgresql.jdbc3g.Jdbc3gConnection"; + public static final String PG_JDBC4_CONNECTION_ENHANCE_CLASS = "org.postgresql.jdbc4.Jdbc4Connection"; @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; @@ -127,6 +130,6 @@ public class ConnectionInstrumentation extends ClassInstanceMethodsEnhancePlugin } @Override protected ClassMatch enhanceClass() { - return byName(ENHANCE_CLASS); + return byMultiClassMatch(PG_CONNECTION_ENHANCE_CLASS, PG_JDBC42_CONNECTION_ENHANCE_CLASS, PG_JDBC3_CONNECTION_ENHANCE_CLASS, PG_JDBC4_CONNECTION_ENHANCE_CLASS); } } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java new file mode 100644 index 000000000..e36e27a1f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java @@ -0,0 +1,68 @@ +/* + * 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.jdbc.postgresql.define; + +import java.util.Arrays; +import java.util.List; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.IndirectMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * Match class with a given set of classes. + * + * @author zhangxin + */ +public class MultiClassNameMatch implements IndirectMatch { + + private List matchClassNames; + + private MultiClassNameMatch(String[] classNames) { + if (classNames == null || classNames.length == 0) { + throw new IllegalArgumentException("match class names is null"); + } + this.matchClassNames = Arrays.asList(classNames); + } + + @Override + public ElementMatcher.Junction buildJunction() { + ElementMatcher.Junction junction = null; + for (String name : matchClassNames) { + if (junction == null) { + junction = named(name); + } else { + junction = junction.or(named(name)); + } + } + return junction; + } + + @Override + public boolean isMatch(TypeDescription typeDescription) { + return matchClassNames.contains(typeDescription.getTypeName()); + } + + public static ClassMatch byMultiClassMatch(String... classNames) { + return new MultiClassNameMatch(classNames); + } +} From c6e593f6f40fee4501675893c7102361c2e64f39 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Thu, 11 Jan 2018 16:45:10 +0800 Subject: [PATCH 2/2] refactory MultiClassNameMatch code --- .../plugin/match}/MultiClassNameMatch.java | 4 +- .../mysql/define/CallableInstrumentation.java | 2 +- .../mysql/define/DriverInstrumentation.java | 2 +- .../Mysql5xConnectionInstrumentation.java | 2 +- .../PreparedStatementInstrumentation.java | 2 +- .../define/StatementInstrumentation.java | 2 +- ...AbstractJdbc2StatementInstrumentation.java | 22 ++++-- .../define/ConnectionInstrumentation.java | 2 +- .../define/MultiClassNameMatch.java | 68 ------------------- 9 files changed, 24 insertions(+), 82 deletions(-) rename apm-sniffer/{apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define => apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/match}/MultiClassNameMatch.java (91%) delete mode 100644 apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/MultiClassNameMatch.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/match/MultiClassNameMatch.java similarity index 91% rename from apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/MultiClassNameMatch.java rename to apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/match/MultiClassNameMatch.java index 66b2e8840..6453facdf 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/MultiClassNameMatch.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/match/MultiClassNameMatch.java @@ -17,14 +17,12 @@ */ -package org.apache.skywalking.apm.plugin.jdbc.mysql.define; +package org.apache.skywalking.apm.agent.core.plugin.match; import java.util.Arrays; import java.util.List; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; -import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; -import org.apache.skywalking.apm.agent.core.plugin.match.IndirectMatch; import static net.bytebuddy.matcher.ElementMatchers.named; diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/CallableInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/CallableInstrumentation.java index e8605a35d..4afeb7f0c 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/CallableInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/CallableInstrumentation.java @@ -27,7 +27,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInst import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; -import static org.apache.skywalking.apm.plugin.jdbc.mysql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link CallableInstrumentation} define that the mysql-2.x plugin intercepts the following methods in the {@link diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/DriverInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/DriverInstrumentation.java index 27b38e174..f17d3da04 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/DriverInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/DriverInstrumentation.java @@ -22,7 +22,7 @@ package org.apache.skywalking.apm.plugin.jdbc.mysql.define; import org.apache.skywalking.apm.plugin.jdbc.define.AbstractDriverInstrumentation; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; -import static org.apache.skywalking.apm.plugin.jdbc.mysql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link DriverInstrumentation} presents that skywalking intercepts {@link com.mysql.jdbc.Driver}. diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/Mysql5xConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/Mysql5xConnectionInstrumentation.java index 512b0c8ee..6c4860176 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/Mysql5xConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/Mysql5xConnectionInstrumentation.java @@ -21,7 +21,7 @@ package org.apache.skywalking.apm.plugin.jdbc.mysql.define; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; -import static org.apache.skywalking.apm.plugin.jdbc.mysql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link Mysql5xConnectionInstrumentation } interceptor {@link com.mysql.cj.jdbc.ConnectionImpl} and diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/PreparedStatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/PreparedStatementInstrumentation.java index 6997cb7a5..dc268942a 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/PreparedStatementInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/PreparedStatementInstrumentation.java @@ -27,7 +27,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterc import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; -import static org.apache.skywalking.apm.plugin.jdbc.mysql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link PreparedStatementInstrumentation} define that the mysql-2.x plugin intercepts the following methods in the diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/StatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/StatementInstrumentation.java index 947e6b12c..a5078cad3 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/StatementInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/define/StatementInstrumentation.java @@ -27,7 +27,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterc import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; -import static org.apache.skywalking.apm.plugin.jdbc.mysql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link StatementInstrumentation} intercepts the following methods in the {@link diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java index ebea4068f..1e4e6107b 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/AbstractJdbc2StatementInstrumentation.java @@ -31,6 +31,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; import static org.apache.skywalking.apm.plugin.jdbc.postgresql.Variables.PG_PREPARED_STATEMENT_EXECUTE_METHOD_INTERCEPTOR; +import static org.apache.skywalking.apm.plugin.jdbc.postgresql.Variables.PG_STATEMENT_EXECUTE_METHOD_INTERCEPTOR; /** * {@link AbstractJdbc2StatementInstrumentation} intercept the following methods that the class which extend {@link @@ -61,18 +62,29 @@ public class AbstractJdbc2StatementInstrumentation extends ClassInstanceMethodsE 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("executeUpdate").and(takesArguments(0))); } @Override public String getMethodsInterceptor() { return PG_PREPARED_STATEMENT_EXECUTE_METHOD_INTERCEPTOR; } + @Override public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named("execute").and(takesArguments(1)) + .or(named("executeQuery").and(takesArguments(1))) + .or(named("executeUpdate").and(takesArguments(1))); + } + + @Override public String getMethodsInterceptor() { + return PG_STATEMENT_EXECUTE_METHOD_INTERCEPTOR; + } + @Override public boolean isOverrideArgs() { return false; } diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java index bb0d70ee4..64250bc0e 100644 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/ConnectionInstrumentation.java @@ -29,7 +29,7 @@ import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; -import static org.apache.skywalking.apm.plugin.jdbc.postgresql.define.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; /** * {@link ConnectionInstrumentation} intercept the following methods that the class which extend {@link diff --git a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java b/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java deleted file mode 100644 index e36e27a1f..000000000 --- a/apm-sniffer/apm-sdk-plugin/postgresql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/postgresql/define/MultiClassNameMatch.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.jdbc.postgresql.define; - -import java.util.Arrays; -import java.util.List; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; -import org.apache.skywalking.apm.agent.core.plugin.match.IndirectMatch; - -import static net.bytebuddy.matcher.ElementMatchers.named; - -/** - * Match class with a given set of classes. - * - * @author zhangxin - */ -public class MultiClassNameMatch implements IndirectMatch { - - private List matchClassNames; - - private MultiClassNameMatch(String[] classNames) { - if (classNames == null || classNames.length == 0) { - throw new IllegalArgumentException("match class names is null"); - } - this.matchClassNames = Arrays.asList(classNames); - } - - @Override - public ElementMatcher.Junction buildJunction() { - ElementMatcher.Junction junction = null; - for (String name : matchClassNames) { - if (junction == null) { - junction = named(name); - } else { - junction = junction.or(named(name)); - } - } - return junction; - } - - @Override - public boolean isMatch(TypeDescription typeDescription) { - return matchClassNames.contains(typeDescription.getTypeName()); - } - - public static ClassMatch byMultiClassMatch(String... classNames) { - return new MultiClassNameMatch(classNames); - } -}