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/25 12:59:56 UTC

[skywalking-java] branch main updated: fix NumberFormat exception in MysqlURLParser (#36)

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 1b90f68  fix NumberFormat exception in MysqlURLParser (#36)
1b90f68 is described below

commit 1b90f6892b696218bddfe7d88354cf644082c8c3
Author: wuguanyu <80...@qq.com>
AuthorDate: Sat Sep 25 20:59:49 2021 +0800

    fix NumberFormat exception in MysqlURLParser (#36)
    
    Delegate @ascrutae 's approval, his GitHub account has an issue for now.
---
 CHANGES.md                                                  |  1 +
 .../plugin/jdbc/connectionurl/parser/MysqlURLParser.java    | 13 ++++++++++++-
 .../apm/plugin/jdbc/connectionurl/parser/URLParserTest.java |  8 ++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index a719792..3172147 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -21,6 +21,7 @@ Release Notes.
 * Add benchmark result for `exception-ignore` plugin and polish plugin guide.
 * Provide Alibaba Druid database connection pool plugin.
 * Provide HikariCP database connection pool plugin.
+* Fix NumberFormat exception in jdbc-commons plugin when MysqlURLParser parser jdbcurl
 * Provide Alibaba Fastjson parser/generator plugin.
 * Fix a tracing context leak of SpringMVC plugin, when an internal exception throws due to response can't be found.
 * Make GRPC log reporter sharing GRPC channel with other reporters of agent. Remove config items of `agent.conf`, `plugin.toolkit.log.grpc.reporter.server_host`, `plugin.toolkit.log.grpc.reporter.server_port`, and `plugin.toolkit.log.grpc.reporter.upstream_timeout`.
diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/MysqlURLParser.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/MysqlURLParser.java
index d4ca1b8..9b47afa 100644
--- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/MysqlURLParser.java
+++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/MysqlURLParser.java
@@ -45,8 +45,15 @@ public class MysqlURLParser extends AbstractURLParser {
     protected URLLocation fetchDatabaseHostsIndexRange() {
         int hostLabelStartIndex = url.indexOf("//");
         int hostLabelEndIndex = url.indexOf("/", hostLabelStartIndex + 2);
+        int hostLabelEndIndexWithParameter = url.indexOf("?", hostLabelStartIndex + 2);
         if (hostLabelEndIndex == -1) {
-            hostLabelEndIndex = url.indexOf("?", hostLabelStartIndex + 2);
+            hostLabelEndIndex = hostLabelEndIndexWithParameter;
+        }
+        if (hostLabelEndIndexWithParameter < hostLabelEndIndex && hostLabelEndIndexWithParameter != -1) {
+            hostLabelEndIndex = hostLabelEndIndexWithParameter;
+        }
+        if (hostLabelEndIndex == -1) {
+            hostLabelEndIndex = url.length();
         }
         return new URLLocation(hostLabelStartIndex + 2, hostLabelEndIndex);
     }
@@ -61,6 +68,10 @@ public class MysqlURLParser extends AbstractURLParser {
 
     protected URLLocation fetchDatabaseNameIndexRange(int startSize) {
         int databaseStartTag = url.indexOf("/", startSize);
+        int parameterStartTag = url.indexOf("?", startSize);
+        if (parameterStartTag < databaseStartTag && parameterStartTag != -1) {
+            return null;
+        }
         if (databaseStartTag == -1) {
             return null;
         }
diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java
index b832b45..b9120ae 100644
--- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java
+++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java
@@ -58,6 +58,14 @@ public class URLParserTest {
     }
 
     @Test
+    public void testParseMysqlJDBCURLWitOutDatabase() {
+        ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql//primaryhost:3307?profileSQL=true");
+        assertThat(connectionInfo.getDBType(), is("Mysql"));
+        assertThat(connectionInfo.getDatabaseName(), is(""));
+        assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3307"));
+    }
+
+    @Test
     public void testParseMysqlJDBCURLWithConnectorJs() {
         ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql:replication://master,slave1,slave2,slave3/test");
         assertThat(connectionInfo.getDBType(), is("Mysql"));