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 Apr 19, 2023
1 parent 08f1a32 commit da85ba1
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 26 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<groupId>org.mule.connectors</groupId>
<artifactId>mule-db-connector</artifactId>
<packaging>mule-extension</packaging>
<version>2.0.0-SNAPSHOT</version>
<version>2.0.3-SNAPSHOT</version>

<name>Database Connector</name>
<description>A Mule extension that provides functionality for connecting to Databases through the JDBC standard</description>
Expand Down 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
Expand Up @@ -12,6 +12,7 @@

import javax.sql.DataSource;

import org.mule.db.commons.internal.domain.connection.DataSourceConfigDbConnectionTracingMetadata;
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 @@ -43,7 +44,8 @@ public class DbDataSourceReferenceConnectionProvider extends DataSourceReference
protected DbConnection createDbConnection(Connection connection) throws Exception {
if (isOracle(connection)) {
return new OracleDbConnection(connection, super.resolveCustomTypes(), resolvedDbTypesCache,
super.getCacheQueryTemplateSize());
super.getCacheQueryTemplateSize(),
new DataSourceConfigDbConnectionTracingMetadata(getDataSourceConfig().get()));
} else {
return super.createDbConnection(connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.mule.db.commons.internal.domain.connection.DefaultDbConnection;
import org.mule.db.commons.internal.domain.type.DbType;
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;

import java.sql.Connection;
import java.util.List;
Expand All @@ -19,8 +20,9 @@
*/
public class DerbyConnection extends DefaultDbConnection {

DerbyConnection(Connection connection, List<DbType> dbTypes, long cacheQueryTemplateSize) {
super(connection, dbTypes, cacheQueryTemplateSize);
DerbyConnection(Connection connection, List<DbType> dbTypes, long cacheQueryTemplateSize,
DbConnectionTracingMetadata dbConnectionTracingMetadata) {
super(connection, dbTypes, cacheQueryTemplateSize, dbConnectionTracingMetadata);
}

// We are disabling content streaming for Derby because of a incompatibility between the connector logic and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.mule.db.commons.api.exception.connection.DbError;
import org.mule.db.commons.internal.domain.connection.DataSourceConfig;
import org.mule.db.commons.internal.domain.connection.DataSourceConfigDbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.DbConnectionProvider;
import org.mule.runtime.extension.api.annotation.Alias;
Expand Down Expand Up @@ -64,7 +65,8 @@ public java.util.Optional<DataSourceConfig> getDataSourceConfig() {

@Override
protected DbConnection createDbConnection(Connection connection) throws Exception {
return new DerbyConnection(connection, resolveCustomTypes(), super.getCacheQueryTemplateSize());
return new DerbyConnection(connection, resolveCustomTypes(), super.getCacheQueryTemplateSize(),
new DataSourceConfigDbConnectionTracingMetadata(getDataSourceConfig().get()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mule.runtime.api.meta.ExternalLibraryType.JAR;
import static org.mule.db.commons.internal.domain.connection.DbConnectionProvider.DRIVER_FILE_NAME_PATTERN;

import org.mule.db.commons.internal.domain.connection.DataSourceConfigDbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.generic.GenericConnectionProvider;
import org.mule.db.commons.internal.domain.type.ResolvedDbType;
Expand Down Expand Up @@ -39,7 +40,8 @@ public class DbGenericConnectionProvider extends GenericConnectionProvider {
@Override
protected DbConnection createDbConnection(Connection connection) throws Exception {
if (isOracle(connection)) {
return new OracleDbConnection(connection, resolveCustomTypes(), resolvedDbTypesCache, super.getCacheQueryTemplateSize());
return new OracleDbConnection(connection, resolveCustomTypes(), resolvedDbTypesCache, super.getCacheQueryTemplateSize(),
new DataSourceConfigDbConnectionTracingMetadata(getDataSourceConfig().get()));
} else {
return super.createDbConnection(connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.mule.db.commons.api.exception.connection.DbError;
import org.mule.db.commons.internal.domain.connection.DataSourceConfig;
import org.mule.db.commons.internal.domain.connection.DbConnectionProvider;
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;
import org.mule.runtime.extension.api.annotation.Alias;
import org.mule.runtime.extension.api.annotation.ExternalLib;
import org.mule.runtime.extension.api.annotation.param.ParameterGroup;
Expand Down Expand Up @@ -72,4 +73,8 @@ public java.util.Optional<DbError> getDbVendorErrorType(SQLException e) {
return empty();
}

@Override
protected DbConnectionTracingMetadata getDbConnectionTracingMetadataFrom() {
return new MySqlConnectionTracingMetadata(mySqlParameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.mysql;

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

import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getPeerNameFrom;
import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getPeerTransportFrom;

import static java.util.Optional.ofNullable;

import java.util.Optional;

public class MySqlConnectionTracingMetadata implements DbConnectionTracingMetadata {

public static final String MYSQL = "mysql";
private final MySqlConnectionParameters mySqlParameter;

public MySqlConnectionTracingMetadata(MySqlConnectionParameters mySqlParameters) {
this.mySqlParameter = mySqlParameters;
}

@Override
public String getDbSystem() {
return MYSQL;
}

@Override
public String getConnectionString() {
return mySqlParameter.getUrl();
}

@Override
public String getUser() {
return mySqlParameter.getUser();
}

@Override
public Optional<String> getPeerName() {
return ofNullable(getPeerNameFrom(mySqlParameter.getUrl()));
}

@Override
public Optional<String> getPeerTransport() {
return ofNullable(getPeerTransportFrom(mySqlParameter.getUrl()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.mule.extension.db.internal.domain.connection.oracle.types.OracleSQLXMLType;
import org.mule.extension.db.internal.domain.connection.oracle.types.OracleSYSXMLType;
import org.mule.extension.db.internal.domain.connection.oracle.types.OracleXMLType;
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;

import java.sql.Array;
import java.sql.Connection;
Expand Down Expand Up @@ -79,8 +80,9 @@ public class OracleDbConnection extends DefaultDbConnection {


public OracleDbConnection(Connection jdbcConnection, List<DbType> customDataTypes,
Map<String, Map<Integer, ResolvedDbType>> resolvedDbTypesCache, long cacheQueryTemplateSize) {
super(jdbcConnection, customDataTypes, cacheQueryTemplateSize);
Map<String, Map<Integer, ResolvedDbType>> resolvedDbTypesCache, long cacheQueryTemplateSize,
DbConnectionTracingMetadata dbConnectionTracingMetadata) {
super(jdbcConnection, customDataTypes, cacheQueryTemplateSize, dbConnectionTracingMetadata);
this.resolvedDbTypesCache = resolvedDbTypesCache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

package org.mule.extension.db.internal.domain.connection.oracle;

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

import static org.mule.db.commons.api.exception.connection.DbError.CANNOT_REACH;
import static org.mule.db.commons.api.exception.connection.DbError.INVALID_CREDENTIALS;
import static org.mule.db.commons.api.exception.connection.DbError.INVALID_DATABASE;
Expand All @@ -21,6 +17,10 @@
import static org.mule.runtime.extension.api.annotation.param.ParameterGroup.CONNECTION;
import static org.mule.extension.db.internal.util.MigrationUtils.mapDataSourceConfig;

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

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -30,6 +30,7 @@

import org.mule.db.commons.api.exception.connection.DbError;
import org.mule.db.commons.internal.domain.connection.DataSourceConfig;
import org.mule.db.commons.internal.domain.connection.DataSourceConfigDbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.DbConnectionProvider;
import org.mule.db.commons.internal.domain.type.ResolvedDbType;
Expand Down Expand Up @@ -79,7 +80,8 @@ public java.util.Optional<DataSourceConfig> getDataSourceConfig() {
@Override
protected DbConnection createDbConnection(Connection connection) throws Exception {
return new OracleDbConnection(connection, super.resolveCustomTypes(), resolvedDbTypesCache,
super.getCacheQueryTemplateSize());
super.getCacheQueryTemplateSize(),
new DataSourceConfigDbConnectionTracingMetadata(getDataSourceConfig().get()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.mule.extension.db.internal.domain.connection.sqlserver.types.SqlServerBinaryDbType;
import org.mule.extension.db.internal.domain.connection.sqlserver.types.SqlServerVarBinaryDbType;
import org.mule.db.commons.internal.domain.type.DbType;
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;

import java.sql.Connection;
import java.util.ArrayList;
Expand All @@ -23,8 +24,9 @@
*/
public class SqlServerConnection extends DefaultDbConnection {

SqlServerConnection(Connection jdbcConnection, List<DbType> customDataTypes, long cacheQueryTemplateSize) {
super(jdbcConnection, customDataTypes, cacheQueryTemplateSize);
SqlServerConnection(Connection jdbcConnection, List<DbType> customDataTypes, long cacheQueryTemplateSize,
DbConnectionTracingMetadata dbConnectionTracingMetadata) {
super(jdbcConnection, customDataTypes, cacheQueryTemplateSize, dbConnectionTracingMetadata);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

import org.mule.db.commons.api.exception.connection.DbError;
import org.mule.db.commons.internal.domain.connection.DataSourceConfig;
import org.mule.db.commons.internal.domain.connection.DataSourceConfigDbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.DbConnectionProvider;
import org.mule.runtime.extension.api.annotation.Alias;
Expand Down Expand Up @@ -56,7 +58,8 @@ public class SqlServerConnectionProvider extends DbConnectionProvider {

@Override
protected DbConnection createDbConnection(Connection connection) throws Exception {
return new SqlServerConnection(connection, super.resolveCustomTypes(), super.getCacheQueryTemplateSize());
return new SqlServerConnection(connection, super.resolveCustomTypes(), super.getCacheQueryTemplateSize(),
new DataSourceConfigDbConnectionTracingMetadata(getDataSourceConfig().get()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.mule.db.commons.AbstractDbConnector;
import org.mule.db.commons.internal.domain.connection.DbConnection;
import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata;
import org.mule.db.commons.internal.domain.metadata.DbInputMetadataResolver;
import org.mule.db.commons.internal.operation.BulkOperations;
import org.mule.db.commons.internal.operation.OperationErrorTypeProvider;
Expand All @@ -25,13 +26,20 @@
import org.mule.extension.db.api.param.BulkQueryDefinition;
import org.mule.extension.db.api.param.BulkScript;
import org.mule.extension.db.api.param.QuerySettings;
import org.mule.sdk.api.runtime.parameter.CorrelationInfo;
import org.mule.sdk.compatibility.api.utils.ForwardCompatibilityHelper;

import javax.inject.Inject;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import static org.mule.db.commons.internal.domain.query.QueryType.DELETE;
import static org.mule.db.commons.internal.domain.query.QueryType.INSERT;
import static org.mule.db.commons.internal.domain.query.QueryType.UPDATE;
import static org.mule.db.commons.internal.operation.BaseDbOperations.QUERY_GROUP;
import static org.mule.db.commons.internal.operation.BaseDbOperations.QUERY_SETTINGS;
import static org.mule.extension.db.internal.operation.tracing.TracingUtils.setAttributesForDbClientOperation;
import static org.mule.extension.db.internal.util.MigrationUtils.mapBulkQueryDefinition;
import static org.mule.extension.db.internal.util.MigrationUtils.mapBulkScript;
import static org.mule.extension.db.internal.util.MigrationUtils.mapQuerySettings;
Expand All @@ -47,6 +55,9 @@ public class DbBulkOperations implements Initialisable {

private BulkOperations bulkOperations;

@Inject
private java.util.Optional<ForwardCompatibilityHelper> forwardCompatibilityHelper;

@Override
public void initialise() throws InitialisationException {
this.bulkOperations = new BulkOperations.Builder().build();
Expand All @@ -70,8 +81,16 @@ public int[] bulkInsert(@DisplayName("Input Parameters") @Content @Placement(
@ParameterGroup(name = QUERY_GROUP) BulkQueryDefinition query,
@Config AbstractDbConnector connector,
@Connection DbConnection connection,
CorrelationInfo correlationInfo,
StreamingHelper streamingHelper)
throws SQLException {
forwardCompatibilityHelper.ifPresent(fwh -> {
DbConnectionTracingMetadata dbConnectionTracingMetadata = connection.getDbConnectionTracingMetadata();
setAttributesForDbClientOperation(dbConnectionTracingMetadata,
dbConnectionTracingMetadata.getDbSystem(),
fwh.getDistributedTraceContextManager(correlationInfo),
query.getSql());
});
return bulkOperations.bulkInsert(bulkInputParameters, mapBulkQueryDefinition(query), connector, connection, streamingHelper);
}

Expand All @@ -94,8 +113,16 @@ public int[] bulkUpdate(@DisplayName("Input Parameters") @Content @Placement(
@ParameterGroup(name = QUERY_GROUP) BulkQueryDefinition query,
@Config AbstractDbConnector connector,
@Connection DbConnection connection,
CorrelationInfo correlationInfo,
StreamingHelper streamingHelper)
throws SQLException {
forwardCompatibilityHelper.ifPresent(fwh -> {
DbConnectionTracingMetadata dbConnectionTracingMetadata = connection.getDbConnectionTracingMetadata();
setAttributesForDbClientOperation(dbConnectionTracingMetadata,
UPDATE.name() + " " + dbConnectionTracingMetadata.getDbSystem(),
fwh.getDistributedTraceContextManager(correlationInfo),
query.getSql());
});
return bulkOperations.bulkUpdate(bulkInputParameters, mapBulkQueryDefinition(query), connector, connection, streamingHelper);
}

Expand All @@ -117,8 +144,16 @@ public int[] bulkDelete(@DisplayName("Input Parameters") @Content @Placement(
@ParameterGroup(name = QUERY_GROUP) BulkQueryDefinition query,
@Config AbstractDbConnector connector,
@Connection DbConnection connection,
CorrelationInfo correlationInfo,
StreamingHelper streamingHelper)
throws SQLException {
forwardCompatibilityHelper.ifPresent(fwh -> {
DbConnectionTracingMetadata dbConnectionTracingMetadata = connection.getDbConnectionTracingMetadata();
setAttributesForDbClientOperation(dbConnectionTracingMetadata,
DELETE.name() + " " + dbConnectionTracingMetadata.getDbSystem(),
fwh.getDistributedTraceContextManager(correlationInfo),
query.getSql());
});
return bulkOperations.bulkDelete(bulkInputParameters, mapBulkQueryDefinition(query), connector, connection, streamingHelper);
}

Expand All @@ -135,8 +170,16 @@ public int[] bulkDelete(@DisplayName("Input Parameters") @Content @Placement(
*/
public int[] executeScript(@ParameterGroup(name = QUERY_GROUP) BulkScript script,
@ParameterGroup(name = QUERY_SETTINGS) QuerySettings settings,
@Connection DbConnection connection)
@Connection DbConnection connection,
CorrelationInfo correlationInfo)
throws SQLException {
forwardCompatibilityHelper.ifPresent(fwh -> {
DbConnectionTracingMetadata dbConnectionTracingMetadata = connection.getDbConnectionTracingMetadata();
setAttributesForDbClientOperation(dbConnectionTracingMetadata,
dbConnectionTracingMetadata.getDbSystem(),
fwh.getDistributedTraceContextManager(correlationInfo),
"<script>");
});
return bulkOperations.executeScript(mapBulkScript(script), mapQuerySettings(settings), connection);
}

Expand Down
Loading

0 comments on commit da85ba1

Please sign in to comment.