From 46d99c5d69d55ccecb69eba886e36aa39ec7aa18 Mon Sep 17 00:00:00 2001 From: xu1009 <2933250475@qq.com> Date: Tue, 12 Apr 2022 22:30:19 +0800 Subject: [PATCH] Support default database(not set through JDBC URL) in mysql-5.x plugin (#154) --- CHANGES.md | 1 + .../jdbc/mysql/v5/ConnectionCreate5xInterceptor.java | 4 +++- .../v5/ConnectionImplCreateInterceptorTest.java | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 9d24594bc..5e97c4172 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -37,6 +37,7 @@ Release Notes. * Fix the bug that maybe causing memory leak and repeated traceId when use gateway-2.1.x-plugin or gateway-3.x-plugin. * Fix Grpc 1.x plugin could leak context due to gRPC cancelled. * Add JDK ThreadPoolExecutor Plugin. +* Support default database(not set through JDBC URL) in mysql-5.x plugin. #### Documentation diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java index 2438c4ad3..0528b3c4e 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java @@ -18,6 +18,7 @@ package org.apache.skywalking.apm.plugin.jdbc.mysql.v5; +import java.util.Objects; 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; @@ -42,7 +43,8 @@ public class ConnectionCreate5xInterceptor implements StaticMethodsAroundInterce public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, Object ret) { if (ret instanceof EnhancedInstance) { - ConnectionInfo connectionInfo = ConnectionCache.get(allArguments[0].toString(), allArguments[1].toString(), allArguments[3].toString()); + String database = Objects.isNull(allArguments[3]) ? "" : allArguments[3].toString(); + ConnectionInfo connectionInfo = ConnectionCache.get(allArguments[0].toString(), allArguments[1].toString(), database); if (connectionInfo == null) { connectionInfo = URLParser.parser(allArguments[4].toString()); } diff --git a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionImplCreateInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionImplCreateInterceptorTest.java index 6a9e155c2..f752dabd9 100644 --- a/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionImplCreateInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/mysql-5.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v5/ConnectionImplCreateInterceptorTest.java @@ -51,4 +51,16 @@ public class ConnectionImplCreateInterceptorTest { }, null, objectInstance); verify(objectInstance).setSkyWalkingDynamicField(Matchers.any()); } + + @Test + public void testResultIsEnhanceInstanceWithNoDatabase() throws Throwable { + interceptor.afterMethod(null, null, new Object[] { + "localhost", + 3360, + null, + null, + "jdbc:mysql:replication://localhost:3360,localhost:3360,localhost:3360/test?useUnicode=true&characterEncoding=utf8&useSSL=false&roundRobinLoadBalance=true" + }, null, objectInstance); + verify(objectInstance).setSkyWalkingDynamicField(Matchers.any()); + } } \ No newline at end of file