Skip to content

Commit a8b77e1

Browse files
pepeshoreotelbot[bot]laurit
authored
Add JDBC URL parsing support for OceanBase, PolarDB and Lindorm (#14790)
Co-authored-by: otelbot <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 58b9003 commit a8b77e1

File tree

2 files changed

+214
-1
lines changed

2 files changed

+214
-1
lines changed

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcConnectionUrlParser.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,84 @@ DbInfo.Builder doParse(String jdbcUrl, DbInfo.Builder builder) {
911911
}
912912
return GENERIC_URL_LIKE.doParse(clickhouseUrl, builder);
913913
}
914+
},
915+
/**
916+
* Sample urls:
917+
*
918+
* <ul>
919+
* <li>jdbc:oceanbase://host:port/dbname
920+
* <li>jdbc:oceanbase:oracle://host:port/dbname
921+
* </ul>
922+
*/
923+
OCEANBASE("oceanbase") {
924+
@Override
925+
DbInfo.Builder doParse(String jdbcUrl, DbInfo.Builder builder) {
926+
int protoLoc = jdbcUrl.indexOf("://");
927+
int typeEndLoc = jdbcUrl.indexOf(':');
928+
if (protoLoc > typeEndLoc) {
929+
String subtype = jdbcUrl.substring(typeEndLoc + 1, protoLoc);
930+
builder.subtype(subtype);
931+
if (subtype.equals(DbSystemValues.ORACLE)) {
932+
builder.system(DbSystemValues.ORACLE);
933+
}
934+
return MODIFIED_URL_LIKE.doParse(jdbcUrl, builder);
935+
} else {
936+
return GENERIC_URL_LIKE.doParse(jdbcUrl, builder);
937+
}
938+
}
939+
},
940+
/**
941+
* <a href="https://www.alibabacloud.com/help/en/lindorm/user-guide/view-endpoints">Driver
942+
* configuration doc</a>
943+
*
944+
* <p>Sample urls:
945+
*
946+
* <ul>
947+
* <li>jdbc:lindorm:table:url=http//server_name:30060/test
948+
* <li>jdbc:lindorm:tsdb:url=http://server_name:8242/test
949+
* <li>jabc:lindorm:search:url=http://server_name:30070/test
950+
* </ul>
951+
*/
952+
LINDORM("lindorm") {
953+
private static final String DEFAULT_HOST = "localhost";
954+
private static final int DEFAULT_PORT = 30060;
955+
956+
@Override
957+
DbInfo.Builder doParse(String jdbcUrl, DbInfo.Builder builder) {
958+
String lindormUrl = jdbcUrl.substring("lindorm:".length());
959+
DbInfo dbInfo = builder.build();
960+
if (dbInfo.getHost() == null) {
961+
builder.host(DEFAULT_HOST);
962+
}
963+
if (dbInfo.getPort() == null) {
964+
builder.port(DEFAULT_PORT);
965+
}
966+
967+
int urlIndex = lindormUrl.indexOf(":url=");
968+
if (urlIndex < 0) {
969+
return builder;
970+
}
971+
builder.subtype(lindormUrl.substring(0, urlIndex));
972+
String realUrl = lindormUrl.substring(urlIndex + 5);
973+
return GENERIC_URL_LIKE.doParse(realUrl, builder);
974+
}
975+
},
976+
/** Sample url: jdbc:polardb://server_name:1901/dbname */
977+
POLARDB("polardb") {
978+
private static final int DEFAULT_PORT = 1521;
979+
private static final String DEFAULT_HOST = "localhost";
980+
981+
@Override
982+
DbInfo.Builder doParse(String jdbcUrl, DbInfo.Builder builder) {
983+
DbInfo dbInfo = builder.build();
984+
if (dbInfo.getHost() == null) {
985+
builder.host(DEFAULT_HOST);
986+
}
987+
if (dbInfo.getPort() == null) {
988+
builder.port(DEFAULT_PORT);
989+
}
990+
return GENERIC_URL_LIKE.doParse(jdbcUrl, builder);
991+
}
914992
};
915993

916994
private static final Logger logger = Logger.getLogger(JdbcConnectionUrlParser.class.getName());
@@ -943,8 +1021,13 @@ public static DbInfo parse(String connectionUrl, Properties props) {
9431021
connectionUrl = connectionUrl.toLowerCase(Locale.ROOT);
9441022

9451023
String jdbcUrl;
946-
if (connectionUrl.startsWith("jdbc:")) {
1024+
if (connectionUrl.startsWith("jdbc:tracing:")) {
1025+
// see https://github.com/opentracing-contrib/java-jdbc
1026+
jdbcUrl = connectionUrl.substring("jdbc:tracing:".length());
1027+
} else if (connectionUrl.startsWith("jdbc:")) {
9471028
jdbcUrl = connectionUrl.substring("jdbc:".length());
1029+
} else if (connectionUrl.startsWith("jdbc-secretsmanager:tracing:")) {
1030+
jdbcUrl = connectionUrl.substring("jdbc-secretsmanager:tracing:".length());
9481031
} else if (connectionUrl.startsWith("jdbc-secretsmanager:")) {
9491032
jdbcUrl = connectionUrl.substring("jdbc-secretsmanager:".length());
9501033
} else {
@@ -1100,6 +1183,12 @@ private static String toDbSystem(String type) {
11001183
return DbSystemValues.HANADB;
11011184
case "clickhouse": // ClickHouse
11021185
return DbSystemValues.CLICKHOUSE;
1186+
case "oceanbase": // Oceanbase
1187+
return DbSystemValues.OCEANBASE;
1188+
case "polardb": // PolarDB
1189+
return DbSystemValues.POLARDB;
1190+
case "lindorm": // Lindorm
1191+
return DbSystemValues.LINDORM;
11031192
default:
11041193
return DbSystemValues.OTHER_SQL; // Unknown DBMS
11051194
}
@@ -1120,6 +1209,9 @@ private static final class DbSystemValues {
11201209
static final String MARIADB = "mariadb";
11211210
static final String H2 = "h2";
11221211
static final String CLICKHOUSE = "clickhouse";
1212+
static final String OCEANBASE = "oceanbase";
1213+
static final String POLARDB = "polardb";
1214+
static final String LINDORM = "lindorm";
11231215

11241216
private DbSystemValues() {}
11251217
}

instrumentation/jdbc/library/src/test/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcConnectionUrlParserTest.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,127 @@ void testSecretsManagerParsing(ParseTestArgument argument) {
13001300
testVerifySystemSubtypeParsingOfUrl(argument);
13011301
}
13021302

1303+
private static Stream<Arguments> openTracingArguments() {
1304+
return args(
1305+
// https://github.com/opentracing-contrib/java-jdbc
1306+
arg("jdbc:tracing:mysql://example.com:50000")
1307+
.setShortUrl("mysql://example.com:50000")
1308+
.setSystem("mysql")
1309+
.setHost("example.com")
1310+
.setPort(50000)
1311+
.build(),
1312+
arg("jdbc:tracing:postgresql://example.com:50000/dbname")
1313+
.setShortUrl("postgresql://example.com:50000")
1314+
.setSystem("postgresql")
1315+
.setHost("example.com")
1316+
.setPort(50000)
1317+
.setDb("dbname")
1318+
.build(),
1319+
arg("jdbc:tracing:oracle:thin:@example.com:50000/ORCL")
1320+
.setShortUrl("oracle:thin://example.com:50000")
1321+
.setSystem("oracle")
1322+
.setSubtype("thin")
1323+
.setHost("example.com")
1324+
.setPort(50000)
1325+
.setName("orcl")
1326+
.build(),
1327+
arg("jdbc:tracing:sqlserver://example.com:50000")
1328+
.setShortUrl("sqlserver://example.com:50000")
1329+
.setSystem("mssql")
1330+
.setHost("example.com")
1331+
.setPort(50000)
1332+
.build());
1333+
}
1334+
1335+
@ParameterizedTest(name = "{index}: {0}")
1336+
@MethodSource("openTracingArguments")
1337+
void testOpenTracingParsing(ParseTestArgument argument) {
1338+
testVerifySystemSubtypeParsingOfUrl(argument);
1339+
}
1340+
1341+
private static Stream<Arguments> oceanbaseArguments() {
1342+
return args(
1343+
// https://en.oceanbase.com/
1344+
arg("jdbc:oceanbase://host:3306/test")
1345+
.setShortUrl("oceanbase://host:3306")
1346+
.setSystem("oceanbase")
1347+
.setHost("host")
1348+
.setPort(3306)
1349+
.setDb("test")
1350+
.build(),
1351+
arg("jdbc:oceanbase:oracle://host:1521")
1352+
.setShortUrl("oceanbase:oracle://host:1521")
1353+
.setSystem("oracle")
1354+
.setSubtype("oracle")
1355+
.setHost("host")
1356+
.setPort(1521)
1357+
.build());
1358+
}
1359+
1360+
@ParameterizedTest(name = "{index}: {0}")
1361+
@MethodSource("oceanbaseArguments")
1362+
void testOceasbaseParsing(ParseTestArgument argument) {
1363+
testVerifySystemSubtypeParsingOfUrl(argument);
1364+
}
1365+
1366+
private static Stream<Arguments> lindormArguments() {
1367+
return args(
1368+
// https://www.alibabacloud.com/help/en/lindorm/user-guide/view-endpoints
1369+
arg("jdbc:lindorm:table:url=http://host:30060/test")
1370+
.setShortUrl("lindorm:table://host:30060")
1371+
.setSystem("lindorm")
1372+
.setSubtype("table")
1373+
.setHost("host")
1374+
.setDb("test")
1375+
.setPort(30060)
1376+
.build(),
1377+
arg("jdbc:lindorm:tsdb:url=http://host:8242/test")
1378+
.setShortUrl("lindorm:tsdb://host:8242")
1379+
.setSystem("lindorm")
1380+
.setSubtype("tsdb")
1381+
.setHost("host")
1382+
.setDb("test")
1383+
.setPort(8242)
1384+
.setDb("test")
1385+
.build(),
1386+
arg("jdbc:lindorm:search:url=http://host:30070/test")
1387+
.setShortUrl("lindorm:search://host:30070")
1388+
.setSystem("lindorm")
1389+
.setSubtype("search")
1390+
.setHost("host")
1391+
.setDb("test")
1392+
.setPort(30070)
1393+
.build());
1394+
}
1395+
1396+
@ParameterizedTest(name = "{index}: {0}")
1397+
@MethodSource("lindormArguments")
1398+
void testLindormManagerParsing(ParseTestArgument argument) {
1399+
testVerifySystemSubtypeParsingOfUrl(argument);
1400+
}
1401+
1402+
private static Stream<Arguments> polardbArguments() {
1403+
return args(
1404+
arg("jdbc:polardb://example.com:1901")
1405+
.setShortUrl("polardb://example.com:1901")
1406+
.setSystem("polardb")
1407+
.setHost("example.com")
1408+
.setPort(1901)
1409+
.build(),
1410+
arg("jdbc:polardb://example.com")
1411+
.setShortUrl("polardb://example.com:1521")
1412+
.setSystem("polardb")
1413+
.setHost("example.com")
1414+
.setPort(1521)
1415+
.build());
1416+
}
1417+
1418+
@ParameterizedTest(name = "{index}: {0}")
1419+
@MethodSource("polardbArguments")
1420+
void testPolardbParsing(ParseTestArgument argument) {
1421+
testVerifySystemSubtypeParsingOfUrl(argument);
1422+
}
1423+
13031424
private static void testVerifySystemSubtypeParsingOfUrl(ParseTestArgument argument) {
13041425
DbInfo info = parse(argument.url, argument.properties);
13051426
DbInfo expected = argument.dbInfo;

0 commit comments

Comments
 (0)