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 2021/09/04 14:03:32 UTC

[skywalking-java] branch main updated: fix multi-db-instance on same host port (#18)

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 f191953  fix multi-db-instance on same host port (#18)
f191953 is described below

commit f19195368916e9514d19084eb1a29737c74bd25b
Author: lpcy <70...@users.noreply.github.com>
AuthorDate: Sat Sep 4 22:03:25 2021 +0800

    fix multi-db-instance on same host port (#18)
---
 CHANGES.md                                                     |  1 +
 .../plugin/jdbc/mysql/v5/ConnectionCreate5xInterceptor.java    |  2 +-
 .../plugin/jdbc/mysql/v6/ConnectionCreateOldInterceptor.java   |  7 ++++++-
 .../apm/plugin/jdbc/mysql/v8/ConnectionCreateInterceptor.java  |  2 +-
 .../skywalking/apm/plugin/jdbc/mysql/ConnectionCache.java      | 10 +++++-----
 5 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 5e07b61..a393f32 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -16,6 +16,7 @@ Release Notes.
 * Advanced Kafka Producer configuration enhancement.
 * Support mTLS for gRPC channel.
 * fix the bug that plugin record wrong time elapse for lettuce plugin
+* fix the bug that the wrong db.instance value displayed on Skywalking-UI when existing multi-database-instance on same host port pair.
 
 #### 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 11226b8..2438c4a 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
@@ -42,7 +42,7 @@ 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());
+            ConnectionInfo connectionInfo = ConnectionCache.get(allArguments[0].toString(), allArguments[1].toString(), allArguments[3].toString());
             if (connectionInfo == null) {
                 connectionInfo = URLParser.parser(allArguments[4].toString());
             }
diff --git a/apm-sniffer/apm-sdk-plugin/mysql-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v6/ConnectionCreateOldInterceptor.java b/apm-sniffer/apm-sdk-plugin/mysql-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v6/ConnectionCreateOldInterceptor.java
index 8ab7126..db405f0 100644
--- a/apm-sniffer/apm-sdk-plugin/mysql-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v6/ConnectionCreateOldInterceptor.java
+++ b/apm-sniffer/apm-sdk-plugin/mysql-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/mysql/v6/ConnectionCreateOldInterceptor.java
@@ -41,7 +41,12 @@ public class ConnectionCreateOldInterceptor implements StaticMethodsAroundInterc
     public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
         Object ret) {
         if (ret instanceof EnhancedInstance) {
-            ConnectionInfo connectionInfo = ConnectionCache.get(allArguments[1].toString(), allArguments[2].toString());
+            String database = "";
+            try {
+                Method getDbMethod = parameterTypes[0].getDeclaredMethod("getDatabase");
+                database = (String) getDbMethod.invoke(allArguments[0]);
+            } catch (Throwable t) { }
+            ConnectionInfo connectionInfo = ConnectionCache.get(allArguments[1].toString(), allArguments[2].toString(), database);
             ((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/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 6e3b804..61ab259 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
@@ -40,7 +40,7 @@ public class ConnectionCreateInterceptor implements StaticMethodsAroundIntercept
         Object ret) {
         if (ret instanceof EnhancedInstance) {
             final HostInfo hostInfo = (HostInfo) allArguments[0];
-            ConnectionInfo connectionInfo = ConnectionCache.get(hostInfo.getHostPortPair());
+            ConnectionInfo connectionInfo = ConnectionCache.get(hostInfo.getHostPortPair(), hostInfo.getDatabase());
             ((EnhancedInstance) ret).setSkyWalkingDynamicField(connectionInfo);
         }
         return ret;
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 055e09d..890af81 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
@@ -28,19 +28,19 @@ public class ConnectionCache {
 
     private static final String CONNECTION_SPLIT_STR = ",";
 
-    public static ConnectionInfo get(String host, String port) {
-        final String hostPortPair = String.format("%s:%s", host, port);
+    public static ConnectionInfo get(String host, String port, String databaseName) {
+        final String hostPortPair = String.format("%s:%s/%s", host, port, databaseName);
         return CONNECTIONS_MAP.get(hostPortPair);
     }
 
-    public static ConnectionInfo get(String hostPortPair) {
-        return CONNECTIONS_MAP.get(hostPortPair);
+    public static ConnectionInfo get(String hostPortPair, String databaseName) {
+        return CONNECTIONS_MAP.get(hostPortPair + "/" + databaseName);
     }
 
     public static void save(ConnectionInfo connectionInfo) {
         for (String conn : connectionInfo.getDatabasePeer().split(CONNECTION_SPLIT_STR)) {
             if (!StringUtil.isEmpty(conn)) {
-                CONNECTIONS_MAP.putIfAbsent(conn, connectionInfo);
+                CONNECTIONS_MAP.putIfAbsent(conn + "/" + connectionInfo.getDatabaseName(), connectionInfo);
             }
         }
     }