Skip to content

Commit

Permalink
fix: Add support for JDBC plugin with remoteServiceName for other dat…
Browse files Browse the repository at this point in the history
…abases
  • Loading branch information
lanxenet committed Aug 18, 2023
1 parent 3944584 commit 6d54607
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@Builder
@Data
public class DatabaseInfo {
private String databaseType;
private String database;
private String host;
private int port;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 6d54607

Please sign in to comment.