Skip to content

Commit

Permalink
W-11859214: A.8.2 Respect semantic conventions of span generation in …
Browse files Browse the repository at this point in the history
…DB connector
  • Loading branch information
fsgonz committed May 16, 2023
1 parent 08f1a32 commit b5a819e
Show file tree
Hide file tree
Showing 20 changed files with 1,717 additions and 18 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<spring-context-version>5.3.2</spring-context-version>
<spring-core-version>5.3.2</spring-core-version>
<mule-spring-module-version>1.3.7</mule-spring-module-version>
<muleSdkCompatibilityApiVersion>1.0.0-SNAPSHOT</muleSdkCompatibilityApiVersion>
<muleSdkApiVersion>1.0.0-SNAPSHOT</muleSdkApiVersion>
</properties>

<dependencies>
Expand Down Expand Up @@ -174,6 +176,16 @@
<version>${xmlunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mule.sdk</groupId>
<artifactId>mule-sdk-compatibility-api</artifactId>
<version>${muleSdkCompatibilityApiVersion}</version>
</dependency>
<dependency>
<groupId>org.mule.sdk</groupId>
<artifactId>mule-sdk-api</artifactId>
<version>${muleSdkApiVersion}</version>
</dependency>

<!-- DB Drivers -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.extension.db.internal.domain.connection.datasource;

import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getHostFrom;
import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getProtocolFrom;
import static org.mule.extension.db.internal.domain.connection.datasource.NoConnectionMetadata.getMetadata;
import static org.slf4j.LoggerFactory.getLogger;

import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;

import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Optional;

import org.slf4j.Logger;

/**
* A {@link DbConnectionTracingMetadata} based on the connection metadata
*/
public class ConnectionBasedDbConnectionTracingMetadata implements DbConnectionTracingMetadata {

public static final String UNSET = "unset";
private static final Logger LOGGER = getLogger(ConnectionBasedDbConnectionTracingMetadata.class);
private final Connection connection;
private DatabaseMetaData metadata;

public ConnectionBasedDbConnectionTracingMetadata(Connection connection) {
this.connection = connection;
}

@Override
public String getDbSystem() {
try {
return getMetaData().getDatabaseProductName();
} catch (SQLException e) {
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
}

return UNSET;
}

private DatabaseMetaData getMetaData() throws SQLException {
// In case the information cannot be retrieved it will not be present.
if (metadata == null) {
try {
metadata = connection.getMetaData();
} catch (Throwable e) {
LOGGER.debug("Unable to retrieve database metadata from connection", e);
metadata = getMetadata();
}

}

return metadata;
}

@Override
public String getConnectionString() {
try {
return getMetaData().getURL();
} catch (SQLException e) {
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
}

return UNSET;
}

@Override
public String getUser() {
try {
return getMetaData().getUserName();
} catch (SQLException e) {
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
}

return UNSET;
}

@Override
public Optional<String> getPeerName() {
try {
return ofNullable(getHostFrom(getMetaData().getURL()));
} catch (SQLException e) {
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
}

return empty();
}

@Override
public Optional<String> getPeerTransport() {
try {
return ofNullable(getProtocolFrom(getMetaData().getURL()));
} catch (SQLException e) {
LOGGER.debug("Unable to retrieve connection metadata for tracing.", e);
}

return empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javax.sql.DataSource;

import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.datasource.DataSourceReferenceConnectionProvider;
import org.mule.db.commons.internal.domain.type.ResolvedDbType;
Expand Down Expand Up @@ -41,11 +42,12 @@ public class DbDataSourceReferenceConnectionProvider extends DataSourceReference

@Override
protected DbConnection createDbConnection(Connection connection) throws Exception {
DbConnectionTracingMetadata dbConnectionTracingMetadata = new ConnectionBasedDbConnectionTracingMetadata(connection);
if (isOracle(connection)) {
return new OracleDbConnection(connection, super.resolveCustomTypes(), resolvedDbTypesCache,
super.getCacheQueryTemplateSize());
super.getCacheQueryTemplateSize(), dbConnectionTracingMetadata);
} else {
return super.createDbConnection(connection);
return super.createDbConnection(connection, dbConnectionTracingMetadata);
}
}

Expand Down
Loading

0 comments on commit b5a819e

Please sign in to comment.