Skip to content

Commit efe0b05

Browse files
traskCopilot
andauthored
Support db.system.name in JDBC instrumentation (open-telemetry#16277)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 308dab6 commit efe0b05

33 files changed

Lines changed: 3042 additions & 1307 deletions

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientSpanNameExtractorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ void shouldUseQuerySummaryWhenAvailable() {
173173
// given
174174
DbRequest dbRequest = new DbRequest();
175175

176-
// Needs to be lenient because not called during this test under old semconv mode
177176
if (emitStableDatabaseSemconv()) {
178177
when(dbAttributesGetter.getDbQuerySummary(dbRequest)).thenReturn("SELECT users");
179178
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,14 @@ public void onEnd(
4343
return;
4444
}
4545
if (emitStableDatabaseSemconv()) {
46-
attributes.put(DB_NAMESPACE, getName(dbInfo));
47-
attributes.put(DB_SYSTEM_NAME, SemconvStability.stableDbSystemName(dbInfo.getSystem()));
46+
attributes.put(DB_NAMESPACE, dbInfo.getDbNamespace());
47+
attributes.put(DB_SYSTEM_NAME, SemconvStability.stableDbSystemName(dbInfo.getDbSystemName()));
4848
}
4949
if (emitOldDatabaseSemconv()) {
50-
attributes.put(DB_USER, dbInfo.getUser());
51-
attributes.put(DB_NAME, getName(dbInfo));
52-
attributes.put(DB_CONNECTION_STRING, dbInfo.getShortUrl());
53-
attributes.put(DB_SYSTEM, dbInfo.getSystem());
50+
attributes.put(DB_USER, dbInfo.getDbUser());
51+
attributes.put(DB_NAME, dbInfo.getDbName());
52+
attributes.put(DB_CONNECTION_STRING, dbInfo.getDbConnectionString());
53+
attributes.put(DB_SYSTEM, dbInfo.getDbSystem());
5454
}
5555
}
56-
57-
private static String getName(DbInfo dbInfo) {
58-
return dbInfo.getName();
59-
}
6056
}

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public final class JdbcAttributesGetter implements SqlClientAttributesGetter<DbRequest, Void> {
2626

2727
// Databases where double quotes are exclusively identifiers and cannot be string literals.
28-
private static final Set<String> DOUBLE_QUOTES_FOR_IDENTIFIERS_SYSTEMS =
28+
private static final Set<String> DOUBLE_QUOTES_FOR_IDENTIFIERS_SYSTEM_NAMES =
2929
new HashSet<>(
3030
asList(
3131
// "A string constant in SQL is an arbitrary sequence of characters
@@ -35,11 +35,11 @@ public final class JdbcAttributesGetter implements SqlClientAttributesGetter<DbR
3535
// "Text, character, and string literals are always surrounded
3636
// by single quotation marks."
3737
// https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Literals.html
38-
"oracle",
38+
"oracle.db",
3939
// "A sequence of characters that starts and ends with a string delimiter,
4040
// which is an apostrophe (')"
4141
// https://www.ibm.com/docs/en/db2/12.1?topic=elements-constants
42-
"db2",
42+
"ibm.db2",
4343
// "Single quotation marks delimit character strings."
4444
// "Double quotation marks delimit special identifiers"
4545
// https://db.apache.org/derby/docs/10.17/ref/rrefsqlj28468.html
@@ -51,44 +51,56 @@ public final class JdbcAttributesGetter implements SqlClientAttributesGetter<DbR
5151
// <string_literal> ::= <single_quote>[<any_character>...]<single_quote>
5252
// <special_identifier> ::= <double_quotes><any_character>...<double_quotes>
5353
// https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/sql-notation-conventions
54-
"hanadb",
54+
"sap.hana",
5555
// "String literals must be enclosed in single quotes.
5656
// Double quotes are not supported."
5757
// https://clickhouse.com/docs/en/sql-reference/syntax#string
5858
"clickhouse",
5959
// PostgreSQL-compatible fork, inherits PG string literal rules
6060
"polardb"));
6161

62-
@Nullable
6362
@Override
6463
public String getDbSystemName(DbRequest request) {
65-
return request.getDbInfo().getSystem();
64+
return request.getDbInfo().getDbSystemName();
65+
}
66+
67+
@Deprecated // to be removed in 3.0
68+
@Override
69+
public String getDbSystem(DbRequest request) {
70+
return request.getDbInfo().getDbSystem();
6671
}
6772

6873
@Deprecated // to be removed in 3.0
6974
@Nullable
7075
@Override
7176
public String getUser(DbRequest request) {
72-
return request.getDbInfo().getUser();
77+
return request.getDbInfo().getDbUser();
7378
}
7479

7580
@Nullable
7681
@Override
7782
public String getDbNamespace(DbRequest request) {
78-
return request.getDbInfo().getName();
83+
return request.getDbInfo().getDbNamespace();
84+
}
85+
86+
@Deprecated // to be removed in 3.0
87+
@Nullable
88+
@Override
89+
public String getDbName(DbRequest request) {
90+
return request.getDbInfo().getDbName();
7991
}
8092

8193
@Deprecated // to be removed in 3.0
8294
@Nullable
8395
@Override
8496
public String getConnectionString(DbRequest request) {
85-
return request.getDbInfo().getShortUrl();
97+
return request.getDbInfo().getDbConnectionString();
8698
}
8799

88100
@Override
89101
public SqlDialect getSqlDialect(DbRequest request) {
90-
String system = request.getDbInfo().getSystem();
91-
if (system != null && DOUBLE_QUOTES_FOR_IDENTIFIERS_SYSTEMS.contains(system)) {
102+
String systemName = request.getDbInfo().getDbSystemName();
103+
if (systemName != null && DOUBLE_QUOTES_FOR_IDENTIFIERS_SYSTEM_NAMES.contains(systemName)) {
92104
return DOUBLE_QUOTES_ARE_IDENTIFIERS;
93105
}
94106
// default to treating double-quoted tokens as string literals for safety, ensuring that
@@ -129,12 +141,12 @@ public boolean isParameterizedQuery(DbRequest request) {
129141
@Nullable
130142
@Override
131143
public String getServerAddress(DbRequest request) {
132-
return request.getDbInfo().getHost();
144+
return request.getDbInfo().getServerAddress();
133145
}
134146

135147
@Nullable
136148
@Override
137149
public Integer getServerPort(DbRequest request) {
138-
return request.getDbInfo().getPort();
150+
return request.getDbInfo().getServerPort();
139151
}
140152
}

0 commit comments

Comments
 (0)