Skip to content

Commit b5a819e

Browse files
committed
W-11859214: A.8.2 Respect semantic conventions of span generation in DB connector
1 parent 08f1a32 commit b5a819e

20 files changed

+1717
-18
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<spring-context-version>5.3.2</spring-context-version>
6363
<spring-core-version>5.3.2</spring-core-version>
6464
<mule-spring-module-version>1.3.7</mule-spring-module-version>
65+
<muleSdkCompatibilityApiVersion>1.0.0-SNAPSHOT</muleSdkCompatibilityApiVersion>
66+
<muleSdkApiVersion>1.0.0-SNAPSHOT</muleSdkApiVersion>
6567
</properties>
6668

6769
<dependencies>
@@ -174,6 +176,16 @@
174176
<version>${xmlunit.version}</version>
175177
<scope>test</scope>
176178
</dependency>
179+
<dependency>
180+
<groupId>org.mule.sdk</groupId>
181+
<artifactId>mule-sdk-compatibility-api</artifactId>
182+
<version>${muleSdkCompatibilityApiVersion}</version>
183+
</dependency>
184+
<dependency>
185+
<groupId>org.mule.sdk</groupId>
186+
<artifactId>mule-sdk-api</artifactId>
187+
<version>${muleSdkApiVersion}</version>
188+
</dependency>
177189

178190
<!-- DB Drivers -->
179191
<dependency>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
3+
* The software in this package is published under the terms of the CPAL v1.0
4+
* license, a copy of which has been included with this distribution in the
5+
* LICENSE.txt file.
6+
*/
7+
package org.mule.extension.db.internal.domain.connection.datasource;
8+
9+
import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getHostFrom;
10+
import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getProtocolFrom;
11+
import static org.mule.extension.db.internal.domain.connection.datasource.NoConnectionMetadata.getMetadata;
12+
import static org.slf4j.LoggerFactory.getLogger;
13+
14+
import static java.util.Optional.empty;
15+
import static java.util.Optional.ofNullable;
16+
17+
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;
18+
19+
import java.sql.Connection;
20+
import java.sql.DatabaseMetaData;
21+
import java.sql.SQLException;
22+
import java.util.Optional;
23+
24+
import org.slf4j.Logger;
25+
26+
/**
27+
* A {@link DbConnectionTracingMetadata} based on the connection metadata
28+
*/
29+
public class ConnectionBasedDbConnectionTracingMetadata implements DbConnectionTracingMetadata {
30+
31+
public static final String UNSET = "unset";
32+
private static final Logger LOGGER = getLogger(ConnectionBasedDbConnectionTracingMetadata.class);
33+
private final Connection connection;
34+
private DatabaseMetaData metadata;
35+
36+
public ConnectionBasedDbConnectionTracingMetadata(Connection connection) {
37+
this.connection = connection;
38+
}
39+
40+
@Override
41+
public String getDbSystem() {
42+
try {
43+
return getMetaData().getDatabaseProductName();
44+
} catch (SQLException e) {
45+
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
46+
}
47+
48+
return UNSET;
49+
}
50+
51+
private DatabaseMetaData getMetaData() throws SQLException {
52+
// In case the information cannot be retrieved it will not be present.
53+
if (metadata == null) {
54+
try {
55+
metadata = connection.getMetaData();
56+
} catch (Throwable e) {
57+
LOGGER.debug("Unable to retrieve database metadata from connection", e);
58+
metadata = getMetadata();
59+
}
60+
61+
}
62+
63+
return metadata;
64+
}
65+
66+
@Override
67+
public String getConnectionString() {
68+
try {
69+
return getMetaData().getURL();
70+
} catch (SQLException e) {
71+
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
72+
}
73+
74+
return UNSET;
75+
}
76+
77+
@Override
78+
public String getUser() {
79+
try {
80+
return getMetaData().getUserName();
81+
} catch (SQLException e) {
82+
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
83+
}
84+
85+
return UNSET;
86+
}
87+
88+
@Override
89+
public Optional<String> getPeerName() {
90+
try {
91+
return ofNullable(getHostFrom(getMetaData().getURL()));
92+
} catch (SQLException e) {
93+
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
94+
}
95+
96+
return empty();
97+
}
98+
99+
@Override
100+
public Optional<String> getPeerTransport() {
101+
try {
102+
return ofNullable(getProtocolFrom(getMetaData().getURL()));
103+
} catch (SQLException e) {
104+
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
105+
}
106+
107+
return empty();
108+
}
109+
}

src/main/java/org/mule/extension/db/internal/domain/connection/datasource/DbDataSourceReferenceConnectionProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import javax.sql.DataSource;
1414

15+
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;
1516
import org.mule.db.commons.internal.domain.connection.DbConnection;
1617
import org.mule.db.commons.internal.domain.connection.datasource.DataSourceReferenceConnectionProvider;
1718
import org.mule.db.commons.internal.domain.type.ResolvedDbType;
@@ -41,11 +42,12 @@ public class DbDataSourceReferenceConnectionProvider extends DataSourceReference
4142

4243
@Override
4344
protected DbConnection createDbConnection(Connection connection) throws Exception {
45+
DbConnectionTracingMetadata dbConnectionTracingMetadata = new ConnectionBasedDbConnectionTracingMetadata(connection);
4446
if (isOracle(connection)) {
4547
return new OracleDbConnection(connection, super.resolveCustomTypes(), resolvedDbTypesCache,
46-
super.getCacheQueryTemplateSize());
48+
super.getCacheQueryTemplateSize(), dbConnectionTracingMetadata);
4749
} else {
48-
return super.createDbConnection(connection);
50+
return super.createDbConnection(connection, dbConnectionTracingMetadata);
4951
}
5052
}
5153

0 commit comments

Comments
 (0)