From 58dce444fc56d5e5bbf7849050df823c879e7ba4 Mon Sep 17 00:00:00 2001 From: Ax1an Date: Thu, 8 Jul 2021 17:55:00 +0800 Subject: [PATCH] Enhance the compatibility of `mysql-8.x-plugin` plugin. (#7261) hostInfo.getDatabaseUrl() result in version 8.0.19 or above: jdbc:mysql:loadbalance://**internally_generated**1620805591623** hostInfo.getDatabaseUrl() result in version 8.0.18 or below: jdbc:mysql:replication://127.0.0.1:3306,127.0.0.1:3306,127.0.0.1:3306/mer_goods_admin_sit?useSSL=true&useUnicode=true&autoReconnect=true&rewriteBatchedStatements=TRUE&allowMultiQueries=true&serverTimezone=GMT%2B8 --- CHANGES.md | 3 +- .../mysql/v8/ConnectionCreateInterceptor.java | 4 +- .../v8/define/CacheIpsInstrumentation.java | 66 +++++++++++++++++++ .../src/main/resources/skywalking-plugin.def | 1 + .../plugin/jdbc/mysql/ConnectionCache.java | 8 ++- .../mysql-scenario/support-version.list | 1 + 6 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/define/CacheIpsInstrumentation.java diff --git a/CHANGES.md b/CHANGES.md index 21d6cb0de4..368d722cbb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,7 +29,8 @@ Release Notes. * Fix async finish repeatedly in `spring-webflux-5.x-webclient` plugin. * Add agent plugin to support Sentinel. * Move `ehcache-2.x` plugin as an optional plugin. -* Support `guava-cache` plugin +* Support `guava-cache` plugin. +* Enhance the compatibility of `mysql-8.x-plugin` plugin. #### OAP-Backend diff --git a/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/ConnectionCreateInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/ConnectionCreateInterceptor.java index 762dbc1e59..6e3b804a84 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/ConnectionCreateInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/ConnectionCreateInterceptor.java @@ -22,7 +22,7 @@ import com.mysql.cj.conf.HostInfo; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; -import org.apache.skywalking.apm.plugin.jdbc.connectionurl.parser.URLParser; +import org.apache.skywalking.apm.plugin.jdbc.mysql.ConnectionCache; import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; import java.lang.reflect.Method; @@ -40,7 +40,7 @@ public class ConnectionCreateInterceptor implements StaticMethodsAroundIntercept Object ret) { if (ret instanceof EnhancedInstance) { final HostInfo hostInfo = (HostInfo) allArguments[0]; - ConnectionInfo connectionInfo = URLParser.parser(hostInfo.getDatabaseUrl()); + ConnectionInfo connectionInfo = ConnectionCache.get(hostInfo.getHostPortPair()); ((EnhancedInstance) ret).setSkyWalkingDynamicField(connectionInfo); } return ret; diff --git a/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/define/CacheIpsInstrumentation.java b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/define/CacheIpsInstrumentation.java new file mode 100644 index 0000000000..028985fc0c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v8/define/CacheIpsInstrumentation.java @@ -0,0 +1,66 @@ +/* + * 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.mysql.v8.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.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; +import static org.apache.skywalking.apm.plugin.jdbc.mysql.Constants.DRIVER_CONNECT_INTERCEPTOR; + +public class CacheIpsInstrumentation extends AbstractMysqlInstrumentation { + + private static final String ENHANCE_CLASS_NON_REG = "com.mysql.cj.jdbc.NonRegisteringDriver"; + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("connect"); + } + + @Override + public String getMethodsInterceptor() { + return DRIVER_CONNECT_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byMultiClassMatch(ENHANCE_CLASS_NON_REG); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/resources/skywalking-plugin.def index 11db6da6e9..9c456171c4 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/resources/skywalking-plugin.def @@ -22,3 +22,4 @@ mysql-8.x=org.apache.skywalking.apm.plugin.jdbc.mysql.v8.define.StatementInstrum mysql-8.x=org.apache.skywalking.apm.plugin.jdbc.mysql.v8.define.PreparedStatementSetterInstrumentation mysql-8.x=org.apache.skywalking.apm.plugin.jdbc.mysql.v8.define.PreparedStatementNullSetterInstrumentation mysql-8.x=org.apache.skywalking.apm.plugin.jdbc.mysql.v8.define.PreparedStatementIgnoredSetterInstrumentation +mysql-8.x=org.apache.skywalking.apm.plugin.jdbc.mysql.v8.define.CacheIpsInstrumentation \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/ConnectionCache.java b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/ConnectionCache.java index 7aebd558c6..055e09dd5e 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/ConnectionCache.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-common/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/ConnectionCache.java @@ -29,8 +29,12 @@ public class ConnectionCache { private static final String CONNECTION_SPLIT_STR = ","; public static ConnectionInfo get(String host, String port) { - final String connStr = String.format("%s:%s", host, port); - return CONNECTIONS_MAP.get(connStr); + final String hostPortPair = String.format("%s:%s", host, port); + return CONNECTIONS_MAP.get(hostPortPair); + } + + public static ConnectionInfo get(String hostPortPair) { + return CONNECTIONS_MAP.get(hostPortPair); } public static void save(ConnectionInfo connectionInfo) { diff --git a/test/plugin/scenarios/mysql-scenario/support-version.list b/test/plugin/scenarios/mysql-scenario/support-version.list index a6885c3010..746fa92395 100644 --- a/test/plugin/scenarios/mysql-scenario/support-version.list +++ b/test/plugin/scenarios/mysql-scenario/support-version.list @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +8.0.19 8.0.15 6.0.6 5.1.44