Skip to content

Commit aa5cf64

Browse files
committed
Merge branch 'pr-16251' into pr-16248
2 parents 11ce454 + 42f95ef commit aa5cf64

5 files changed

Lines changed: 128 additions & 47 deletions

File tree

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static <REQUEST, RESPONSE> void onStartCommon(
107107
}
108108
}
109109
if (emitOldDatabaseSemconv()) {
110-
attributes.put(DB_SYSTEM, getter.getDbSystemName(request));
110+
attributes.put(DB_SYSTEM, getter.getDbSystem(request));
111111
attributes.put(DB_USER, getter.getUser(request));
112112
attributes.put(DB_NAME, getter.getDbNamespace(request));
113113
attributes.put(DB_CONNECTION_STRING, getter.getConnectionString(request));

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesGetter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ default String getDbQuerySummary(REQUEST request) {
4141
// TODO: make this required to implement
4242
String getDbSystemName(REQUEST request);
4343

44+
/**
45+
* Returns the old semconv {@code db.system} value. Defaults to {@link #getDbSystemName} for
46+
* instrumentations where the old and new values are the same.
47+
*
48+
* @deprecated Use {@link #getDbSystemName} instead.
49+
*/
50+
@Deprecated // to be removed in 3.0
51+
default String getDbSystem(REQUEST request) {
52+
return getDbSystemName(request);
53+
}
54+
4455
@Nullable
4556
String getDbNamespace(REQUEST request);
4657

instrumentation/r2dbc-1.0/library/src/main/java/io/opentelemetry/instrumentation/r2dbc/v1_0/internal/DbExecution.java

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,51 @@
1818
import io.r2dbc.proxy.core.QueryInfo;
1919
import io.r2dbc.spi.Connection;
2020
import io.r2dbc.spi.ConnectionFactoryOptions;
21+
import java.util.HashMap;
2122
import java.util.Locale;
23+
import java.util.Map;
24+
import javax.annotation.Nullable;
2225

2326
/**
2427
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
2528
* any time.
2629
*/
2730
public final class DbExecution {
31+
// copied from DbAttributes.DbSystemNameValues
32+
private static final String POSTGRESQL = "postgresql";
33+
// copied from DbAttributes.DbSystemNameValues
34+
private static final String MYSQL = "mysql";
35+
// copied from DbAttributes.DbSystemNameValues
36+
private static final String MARIADB = "mariadb";
37+
// copied from DbAttributes.DbSystemNameValues
38+
private static final String MICROSOFT_SQL_SERVER = "microsoft.sql_server";
39+
// copied from DbIncubatingAttributes.DbSystemNameIncubatingValues
40+
private static final String ORACLE_DB = "oracle.db";
41+
// copied from DbIncubatingAttributes.DbSystemNameIncubatingValues
42+
private static final String H2DATABASE = "h2database";
2843
// copied from DbIncubatingAttributes.DbSystemNameIncubatingValues
2944
private static final String OTHER_SQL = "other_sql";
3045

46+
// R2DBC driver identifier → stable semconv db.system.name value
47+
private static final Map<String, String> DRIVER_TO_SYSTEM_NAME = buildDriverToSystemName();
48+
49+
private static Map<String, String> buildDriverToSystemName() {
50+
Map<String, String> map = new HashMap<>();
51+
map.put("postgresql", POSTGRESQL);
52+
map.put("mysql", MYSQL);
53+
map.put("mariadb", MARIADB);
54+
map.put("mssql", MICROSOFT_SQL_SERVER);
55+
map.put("oracle", ORACLE_DB);
56+
map.put("h2", H2DATABASE);
57+
return map;
58+
}
59+
60+
private final String systemName;
3161
private final String system;
3262
private final String user;
33-
private final String name;
34-
private final String host;
35-
private final Integer port;
63+
private final String namespace;
64+
private final String serverAddress;
65+
private final Integer serverPort;
3666
private final String connectionString;
3767
private final String rawQueryText;
3868
private final boolean parameterizedQuery;
@@ -50,23 +80,26 @@ public DbExecution(QueryExecutionInfo queryInfo, ConnectionFactoryOptions factor
5080
.split(" ")[0]
5181
: OTHER_SQL;
5282
this.user = factoryOptions.hasOption(USER) ? (String) factoryOptions.getValue(USER) : null;
53-
this.name =
83+
this.namespace =
5484
factoryOptions.hasOption(DATABASE)
5585
? ((String) factoryOptions.getValue(DATABASE)).toLowerCase(Locale.ROOT)
5686
: null;
5787
String driver =
5888
factoryOptions.hasOption(DRIVER) ? (String) factoryOptions.getValue(DRIVER) : null;
5989
String protocol =
6090
factoryOptions.hasOption(PROTOCOL) ? (String) factoryOptions.getValue(PROTOCOL) : null;
61-
this.host = factoryOptions.hasOption(HOST) ? (String) factoryOptions.getValue(HOST) : null;
62-
this.port = factoryOptions.hasOption(PORT) ? (Integer) factoryOptions.getValue(PORT) : null;
91+
this.systemName = resolveDbSystemName(driver, protocol);
92+
this.serverAddress =
93+
factoryOptions.hasOption(HOST) ? (String) factoryOptions.getValue(HOST) : null;
94+
this.serverPort =
95+
factoryOptions.hasOption(PORT) ? (Integer) factoryOptions.getValue(PORT) : null;
6396
this.connectionString =
6497
String.format(
6598
"%s%s:%s%s",
6699
driver != null ? driver : "",
67100
protocol != null ? ":" + protocol : "",
68-
host != null ? "//" + host : "",
69-
port != null ? ":" + port : "");
101+
serverAddress != null ? "//" + serverAddress : "",
102+
serverPort != null ? ":" + serverPort : "");
70103
this.rawQueryText =
71104
queryInfo.getQueries().stream()
72105
.map(QueryInfo::getQuery)
@@ -80,14 +113,20 @@ public DbExecution(QueryExecutionInfo queryInfo, ConnectionFactoryOptions factor
80113
R2dbcSqlCommenterUtil.clearQueries(queryInfo.getConnectionInfo());
81114
}
82115

83-
public Integer getPort() {
84-
return port;
116+
public String getServerAddress() {
117+
return serverAddress;
85118
}
86119

87-
public String getHost() {
88-
return host;
120+
@Nullable
121+
public Integer getServerPort() {
122+
return serverPort;
89123
}
90124

125+
public String getSystemName() {
126+
return systemName;
127+
}
128+
129+
@Deprecated // to be removed in 3.0
91130
public String getSystem() {
92131
return system;
93132
}
@@ -96,8 +135,8 @@ public String getUser() {
96135
return user;
97136
}
98137

99-
public String getName() {
100-
return name;
138+
public String getNamespace() {
139+
return namespace;
101140
}
102141

103142
public String getConnectionString() {
@@ -120,31 +159,11 @@ public void setContext(Context context) {
120159
this.context = context;
121160
}
122161

123-
@Override
124-
public String toString() {
125-
return "DbExecution{"
126-
+ "system='"
127-
+ system
128-
+ '\''
129-
+ ", user='"
130-
+ user
131-
+ '\''
132-
+ ", name='"
133-
+ name
134-
+ '\''
135-
+ ", host='"
136-
+ host
137-
+ '\''
138-
+ ", port="
139-
+ port
140-
+ ", connectionString='"
141-
+ connectionString
142-
+ '\''
143-
+ ", rawQueryText='"
144-
+ rawQueryText
145-
+ '\''
146-
+ ", context="
147-
+ context
148-
+ '}';
162+
private static String resolveDbSystemName(@Nullable String driver, @Nullable String protocol) {
163+
164+
// Use PROTOCOL when DRIVER is "pool" (r2dbc-pool wraps the real driver in PROTOCOL),
165+
// otherwise use DRIVER directly.
166+
String rawDriver = "pool".equals(driver) && protocol != null ? protocol : driver;
167+
return rawDriver != null ? DRIVER_TO_SYSTEM_NAME.getOrDefault(rawDriver, OTHER_SQL) : OTHER_SQL;
149168
}
150169
}

instrumentation/r2dbc-1.0/library/src/main/java/io/opentelemetry/instrumentation/r2dbc/v1_0/internal/R2dbcSqlAttributesGetter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public enum R2dbcSqlAttributesGetter implements SqlClientAttributesGetter<DbExec
2121

2222
@Override
2323
public String getDbSystemName(DbExecution request) {
24+
return request.getSystemName();
25+
}
26+
27+
@Deprecated // to be removed in 3.0
28+
@Override
29+
public String getDbSystem(DbExecution request) {
2430
return request.getSystem();
2531
}
2632

@@ -34,7 +40,7 @@ public String getUser(DbExecution request) {
3440
@Override
3541
@Nullable
3642
public String getDbNamespace(DbExecution request) {
37-
return request.getName();
43+
return request.getNamespace();
3844
}
3945

4046
@Deprecated // to be removed in 3.0
@@ -61,13 +67,13 @@ public String getDbResponseStatusCode(@Nullable Void response, @Nullable Throwab
6167
@Nullable
6268
@Override
6369
public String getServerAddress(DbExecution request) {
64-
return request.getHost();
70+
return request.getServerAddress();
6571
}
6672

6773
@Nullable
6874
@Override
6975
public Integer getServerPort(DbExecution request) {
70-
return request.getPort();
76+
return request.getServerPort();
7177
}
7278

7379
@Override

instrumentation/r2dbc-1.0/library/src/test/java/io/opentelemetry/instrumentation/r2dbc/v1_0/DbExecutionTest.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.r2dbc.spi.ConnectionMetadata;
1919
import org.junit.jupiter.api.Test;
2020
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.CsvSource;
2123
import org.mockito.Mock;
2224
import org.mockito.junit.jupiter.MockitoExtension;
2325

@@ -27,6 +29,7 @@ class DbExecutionTest {
2729
@Mock Connection connection;
2830
@Mock ConnectionMetadata metadata;
2931

32+
@SuppressWarnings("deprecation") // testing deprecated semconv
3033
@Test
3134
void dbExecution() {
3235
when(connection.getMetadata()).thenReturn(metadata);
@@ -39,13 +42,55 @@ void dbExecution() {
3942
ConnectionFactoryOptions factoryOptions =
4043
ConnectionFactoryOptions.parse("r2dbc:mariadb://root:root@localhost:3306/db");
4144
DbExecution dbExecution = new DbExecution(queryExecutionInfo, factoryOptions);
45+
assertThat(dbExecution.getSystemName()).isEqualTo("mariadb");
4246
assertThat(dbExecution.getSystem()).isEqualTo("testdb");
4347
assertThat(dbExecution.getUser()).isEqualTo("root");
44-
assertThat(dbExecution.getName()).isEqualTo("db");
45-
assertThat(dbExecution.getHost()).isEqualTo("localhost");
46-
assertThat(dbExecution.getPort()).isEqualTo(3306);
48+
assertThat(dbExecution.getNamespace()).isEqualTo("db");
49+
assertThat(dbExecution.getServerAddress()).isEqualTo("localhost");
50+
assertThat(dbExecution.getServerPort()).isEqualTo(3306);
4751
assertThat(dbExecution.getConnectionString()).isEqualTo("mariadb://localhost:3306");
4852
assertThat(dbExecution.getRawQueryText())
4953
.isEqualTo("SELECT * from person where last_name = 'tom'");
5054
}
55+
56+
@SuppressWarnings("deprecation") // testing deprecated semconv
57+
@Test
58+
void dbExecutionWithPool() {
59+
QueryExecutionInfo queryExecutionInfo =
60+
MockQueryExecutionInfo.builder()
61+
.queryInfo(new QueryInfo("SELECT 1"))
62+
.connectionInfo(MockConnectionInfo.builder().build())
63+
.build();
64+
ConnectionFactoryOptions factoryOptions =
65+
ConnectionFactoryOptions.parse("r2dbc:pool:postgresql://user:pass@dbhost:5432/mydb");
66+
DbExecution dbExecution = new DbExecution(queryExecutionInfo, factoryOptions);
67+
assertThat(dbExecution.getSystemName()).isEqualTo("postgresql");
68+
assertThat(dbExecution.getSystem()).isEqualTo("other_sql");
69+
assertThat(dbExecution.getUser()).isEqualTo("user");
70+
assertThat(dbExecution.getNamespace()).isEqualTo("mydb");
71+
assertThat(dbExecution.getServerAddress()).isEqualTo("dbhost");
72+
assertThat(dbExecution.getServerPort()).isEqualTo(5432);
73+
assertThat(dbExecution.getConnectionString()).isEqualTo("pool:postgresql://dbhost:5432");
74+
}
75+
76+
@ParameterizedTest
77+
@CsvSource({
78+
"r2dbc:postgresql://localhost/db, postgresql",
79+
"r2dbc:mysql://localhost/db, mysql",
80+
"r2dbc:mariadb://localhost/db, mariadb",
81+
"r2dbc:mssql://localhost/db, microsoft.sql_server",
82+
"r2dbc:oracle://localhost/db, oracle.db",
83+
"r2dbc:h2:mem:///testdb, h2database",
84+
"r2dbc:unknown://localhost/db, other_sql",
85+
})
86+
void dbSystemName(String url, String expectedSystemName) {
87+
QueryExecutionInfo queryExecutionInfo =
88+
MockQueryExecutionInfo.builder()
89+
.queryInfo(new QueryInfo("SELECT 1"))
90+
.connectionInfo(MockConnectionInfo.builder().build())
91+
.build();
92+
ConnectionFactoryOptions factoryOptions = ConnectionFactoryOptions.parse(url);
93+
DbExecution dbExecution = new DbExecution(queryExecutionInfo, factoryOptions);
94+
assertThat(dbExecution.getSystemName()).isEqualTo(expectedSystemName);
95+
}
5196
}

0 commit comments

Comments
 (0)