Skip to content

Commit

Permalink
Fix for MySQL multi-host internally_generated connection URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ygree committed Mar 28, 2024
1 parent 6167d5e commit 0613d39
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ DBInfo.Builder doParse(final String jdbcUrl, final DBInfo.Builder builder) {
MARIA_SUBPROTO() {
@Override
DBInfo.Builder doParse(final String jdbcUrl, final DBInfo.Builder builder) {
if (jdbcUrl.startsWith("**internally_generated**")) {
// there is nothing to parse
builder.host(null);
builder.port(null);
return builder;
}
final int hostEndLoc;
final int clusterSepLoc = jdbcUrl.indexOf(',');
final int ipv6End = jdbcUrl.startsWith("[") ? jdbcUrl.indexOf(']') : -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,22 @@ public static void addDBInfo(
} catch (Throwable ignored) {
// use original values
}
Connection connWithContext = connection;
if (connectionUrl.startsWith("**internally_generated**", connectionUrl.indexOf("://") + 3)) {
// Treat MySql multi-host connections specifically.
// Use the original connection URL for parsing.
connectionUrl = url;
try {
// Find an underlying connection object to which to attach the DBInfo. Otherwise, it may
// be attached to a wrapper or a proxy connection object that is different from the one
// available in other parts of the instrumentation, resulting in a loss of context.
connWithContext = connection.unwrap(Connection.class);
} catch (Throwable t) {
// ignore
}
}
DBInfo dbInfo = JDBCConnectionUrlParser.extractDBInfo(connectionUrl, connectionProps);
InstrumentationContext.get(Connection.class, DBInfo.class).put(connection, dbInfo);
InstrumentationContext.get(Connection.class, DBInfo.class).put(connWithContext, dbInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class JDBCConnectionUrlParserTest extends AgentTestRunner {
"jdbc:mysql://my.host:22/mydb?user=myuser&password=PW" | null | "mysql" | null | "myuser" | "my.host" | 22 | null | "mydb"
"jdbc:mysql://127.0.0.1:22/mydb?user=myuser&password=PW" | stdProps | "mysql" | null | "myuser" | "127.0.0.1" | 22 | null | "mydb"

// accept **internally_generated**
"jdbc:mysql:loadbalance://**internally_generated**1711497942886**" | null | "mysql" | "loadbalance" | null | null | null | null | null

//https://github.com/awslabs/aws-mysql-jdbc#connection-url-descriptions
"jdbc:mysql:aws://my.host" | null | "mysql" | null | null | "my.host" | 3306 | null | null
"jdbc:mysql:aws://my.host:22/mydb" | null | "mysql" | null | null | "my.host" | 22 | null | "mydb"
Expand Down

0 comments on commit 0613d39

Please sign in to comment.