You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/04/12 14:30:24 UTC

[skywalking-java] branch main updated: Support default database(not set through JDBC URL) in mysql-5.x plugin (#154)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 46d99c5d6 Support default database(not set through JDBC URL) in mysql-5.x plugin (#154)
46d99c5d6 is described below

commit 46d99c5d69d55ccecb69eba886e36aa39ec7aa18
Author: xu1009 <29...@qq.com>
AuthorDate: Tue Apr 12 22:30:19 2022 +0800

    Support default database(not set through JDBC URL) in mysql-5.x plugin (#154)
---
 CHANGES.md                                                   |  1 +
 .../plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java  |  4 +++-
 .../jdbc/mysql/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