diff --git a/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfo.java b/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfo.java index 25a8f5834..13ef08140 100644 --- a/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfo.java +++ b/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfo.java @@ -28,6 +28,7 @@ @Builder @Data public class DatabaseInfo { + private String databaseType; private String database; private String host; private int port; @@ -36,14 +37,9 @@ public static DatabaseInfo getFromConnection(Connection connection) { try { String jdbcURL = connection.getMetaData().getURL(); URI url = URI.create(jdbcURL.substring(5)); // strip "jdbc:" - String remoteServiceName; + String remoteServiceName = url.getScheme(); // e.g. mysql, postgresql, oracle String databaseName = connection.getCatalog(); - if (databaseName != null && !databaseName.isEmpty()) { - remoteServiceName = databaseName; - } else { - remoteServiceName = ""; - } - return new DatabaseInfo(remoteServiceName, + return new DatabaseInfo(remoteServiceName, databaseName, StringUtils.isNotEmpty(url.getHost()) ? url.getHost() : "", url.getPort() == -1 ? 3306 : url.getPort()); } catch (SQLException ignored) { diff --git a/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptor.java b/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptor.java index 63ac9b0fd..c554789d1 100644 --- a/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptor.java +++ b/plugins/jdbc/src/main/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptor.java @@ -34,7 +34,6 @@ import com.megaease.easeagent.plugin.jdbc.JdbcTracingPlugin; import com.megaease.easeagent.plugin.jdbc.advice.JdbcStatementAdvice; import com.megaease.easeagent.plugin.jdbc.common.*; -import com.megaease.easeagent.plugin.utils.common.StringUtils; import org.apache.commons.codec.digest.DigestUtils; import java.sql.Connection; @@ -84,19 +83,20 @@ public void doBefore(MethodInfo methodInfo, Context context) { RedirectProcessor.setTagsIfRedirected(Redirect.DATABASE, span, url); DatabaseInfo databaseInfo = DatabaseInfo.getFromConnection(conn); if (databaseInfo != null) { - span.remoteServiceName(remoteServiceName(databaseInfo.getDatabase())); + span.remoteServiceName(remoteServiceName(databaseInfo)); span.remoteIpAndPort(databaseInfo.getHost(), databaseInfo.getPort()); } span.start(); context.put(SPAN_KEY, span); } - public String remoteServiceName(String database) { - if (StringUtils.isEmpty(database)) { - return "mysql"; - } else { - return "mysql-" + database; - } + public String remoteServiceName(DatabaseInfo info) { + return String.format("%s:%s", info.getDatabaseType(), info.getDatabase()); +// if (StringUtils.isEmpty(info.getServiceName())) { +// return "mysql"; +// } else { +// return "mysql-" + database; +// } } @Override diff --git a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/TestUtils.java b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/TestUtils.java index 86d2bceaa..aa05e5bb0 100644 --- a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/TestUtils.java +++ b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/TestUtils.java @@ -31,10 +31,11 @@ import static org.mockito.Mockito.when; public class TestUtils { + public static final String DATABASE_TYPE = "mysql"; public static final String DATABASE = "db_damo"; public static final String HOST = "192.168.1.14"; public static final int PORT = 1234; - public static final String URI = String.format("jdbc:mysql://%s:%s/%s", HOST, PORT, DATABASE); + public static final String URI = String.format("jdbc:%s://%s:%s/%s", DATABASE_TYPE, HOST, PORT, DATABASE); public static final String FULL_URI = URI + "?useUnicode=true&characterEncoding=utf-8&autoReconnectForPools=true&autoReconnect=true"; public static final String REDIRECT_USERNAME = "testUserName"; public static final String REDIRECT_PASSWORD = "testPassword"; diff --git a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfoTest.java b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfoTest.java index e377065ae..6749d9728 100644 --- a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfoTest.java +++ b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/common/DatabaseInfoTest.java @@ -33,6 +33,7 @@ public void getFromConnection() throws SQLException { Connection connection = TestUtils.mockConnection(); DatabaseInfo databaseInfo = DatabaseInfo.getFromConnection(connection); assertNotNull(databaseInfo); + assertEquals(TestUtils.DATABASE_TYPE, databaseInfo.getDatabaseType()); assertEquals(TestUtils.DATABASE, databaseInfo.getDatabase()); assertEquals(TestUtils.HOST, databaseInfo.getHost()); assertEquals(TestUtils.PORT, databaseInfo.getPort()); diff --git a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptorTest.java b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptorTest.java index daad2a1f1..6918c81ec 100644 --- a/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptorTest.java +++ b/plugins/jdbc/src/test/java/com/megaease/easeagent/plugin/jdbc/interceptor/tracing/JdbcStmTracingInterceptorTest.java @@ -82,7 +82,7 @@ public void doBefore() throws SQLException { assertEquals("database", reportSpan.tag(JdbcStmTracingInterceptor.SPAN_LOCAL_COMPONENT_TAG_NAME)); assertEquals(TestUtils.URI, reportSpan.tag(JdbcStmTracingInterceptor.SPAN_URL)); assertEquals(Type.DATABASE.getRemoteType(), reportSpan.tag(MiddlewareConstants.TYPE_TAG_NAME)); - assertEquals("mysql-" + TestUtils.DATABASE, reportSpan.remoteServiceName()); + assertEquals(TestUtils.DATABASE_TYPE + ":" + TestUtils.DATABASE, reportSpan.remoteServiceName()); assertEquals(TestUtils.HOST, reportSpan.remoteEndpoint().ipv4()); assertEquals(TestUtils.PORT, reportSpan.remoteEndpoint().port());