Skip to content

Commit a9db901

Browse files
committed
review
1 parent dae5979 commit a9db901

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/parser/PostgresqlUrlParser.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.instrumentation.jdbc.internal.parser.UrlParsingUtils.splitQuery;
99

1010
import java.util.Map;
11+
import java.util.Properties;
1112

1213
/**
1314
* Parses PostgreSQL JDBC connection strings.
@@ -51,8 +52,10 @@ public void parse(String jdbcUrl, ParseContext ctx) {
5152
// Delegate to generic parser for standard URL parsing
5253
GenericUrlParser.INSTANCE.parse(jdbcUrl, ctx);
5354

54-
// Extract schema from currentSchema URL parameter for namespace formatting
5555
String schema = extractCurrentSchema(jdbcUrl);
56+
if (schema == null) {
57+
schema = extractCurrentSchema(ctx.props());
58+
}
5659
if (schema == null && ctx.user() != null) {
5760
// Fall back to user as schema if no currentSchema param
5861
schema = ctx.user();
@@ -73,4 +76,12 @@ private static String extractCurrentSchema(String jdbcUrl) {
7376
Map<String, String> params = splitQuery(jdbcUrl.substring(queryIndex + 1), "&");
7477
return params.get("currentschema");
7578
}
79+
80+
private static String extractCurrentSchema(Properties props) {
81+
if (props == null) {
82+
return null;
83+
}
84+
String currentSchema = props.getProperty("currentSchema");
85+
return currentSchema == null || currentSchema.isEmpty() ? null : currentSchema;
86+
}
7687
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ private static Properties stdProps() {
4444
return prop;
4545
}
4646

47+
private static Properties postgresProps(String user, String currentSchema) {
48+
Properties prop = new Properties();
49+
prop.setProperty("user", user);
50+
prop.setProperty("currentSchema", currentSchema);
51+
return prop;
52+
}
53+
4754
@ParameterizedTest
4855
@ValueSource(strings = {"", "jdbc:", "jdbc::", "bogus:string"})
4956
void testInvalidUrlReturnsDefault(String url) {
@@ -273,6 +280,28 @@ private static Stream<Arguments> postgresArguments() {
273280
.setNamespace("pgdb|myschema")
274281
.setName("pgdb")
275282
.build(),
283+
// currentSchema from connection properties is used when the URL does not specify it
284+
arg("jdbc:postgresql://pg.host/pgdb")
285+
.setProperties(postgresProps("pguser", "propertyschema"))
286+
.setShortUrl("postgresql://pg.host:5432")
287+
.setSystem("postgresql")
288+
.setUser("pguser")
289+
.setHost("pg.host")
290+
.setPort(5432)
291+
.setNamespace("pgdb|propertyschema")
292+
.setName("pgdb")
293+
.build(),
294+
// currentSchema URL param takes precedence over currentSchema property
295+
arg("jdbc:postgresql://pg.host/pgdb?currentSchema=urlschema")
296+
.setProperties(postgresProps("pguser", "propertyschema"))
297+
.setShortUrl("postgresql://pg.host:5432")
298+
.setSystem("postgresql")
299+
.setUser("pguser")
300+
.setHost("pg.host")
301+
.setPort(5432)
302+
.setNamespace("pgdb|urlschema")
303+
.setName("pgdb")
304+
.build(),
276305
// database only, no schema or user — namespace falls back to database name
277306
arg("jdbc:postgresql://pg.host/pgdb")
278307
.setShortUrl("postgresql://pg.host:5432")

0 commit comments

Comments
 (0)