Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,175 +5,18 @@

package io.opentelemetry.javaagent.instrumentation.jdbc.test;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.jdbc.TestConnection;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.jdbc.testing.AbstractSqlCommenterTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class SqlCommenterTest {
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
class SqlCommenterTest extends AbstractSqlCommenterTest {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Test
void testSqlCommenterStatement() throws SQLException {
List<String> executedSql = new ArrayList<>();
Connection connection = new TestConnection(executedSql::add);
Statement statement = connection.createStatement();

cleanup.deferCleanup(statement);
cleanup.deferCleanup(connection);

String query = "SELECT 1";
testing.runWithSpan("parent", () -> statement.execute(query));

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasNoParent(),
span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0))));

assertThat(executedSql).hasSize(1);
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void testSqlCommenterStatementUpdate(boolean largeUpdate) throws SQLException {
List<String> executedSql = new ArrayList<>();
Connection connection = new TestConnection(executedSql::add);
Statement statement = connection.createStatement();

cleanup.deferCleanup(statement);
cleanup.deferCleanup(connection);

String query = "INSERT INTO test VALUES(1)";
testing.runWithSpan(
"parent",
() -> {
if (largeUpdate) {
statement.executeLargeUpdate(query);
} else {
statement.execute(query);
}
});

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasNoParent(),
span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0))));

assertThat(executedSql).hasSize(1);
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void testSqlCommenterStatementBatch(boolean largeUpdate) throws SQLException {
List<String> executedSql = new ArrayList<>();
Connection connection = new TestConnection(executedSql::add);
Statement statement = connection.createStatement();

cleanup.deferCleanup(statement);
cleanup.deferCleanup(connection);

testing.runWithSpan(
"parent",
() -> {
statement.addBatch("INSERT INTO test VALUES(1)");
statement.addBatch("INSERT INTO test VALUES(2)");
if (largeUpdate) {
statement.executeLargeBatch();
} else {
statement.executeBatch();
}
});

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasNoParent(),
span ->
span.hasName(
SemconvStability.emitStableDatabaseSemconv()
? "BATCH INSERT dbname.test"
: "dbname")
.hasParent(trace.getSpan(0))));

assertThat(executedSql).hasSize(2);
assertThat(executedSql.get(0)).contains("INSERT INTO test VALUES(1)").contains("traceparent");
assertThat(executedSql.get(1)).contains("INSERT INTO test VALUES(2)").contains("traceparent");
}

@Test
void testSqlCommenterPreparedStatement() throws SQLException {
List<String> executedSql = new ArrayList<>();
Connection connection = new TestConnection(executedSql::add);

String query = "SELECT 1";
testing.runWithSpan(
"parent",
() -> {
PreparedStatement statement = connection.prepareStatement(query);
cleanup.deferCleanup(statement);
cleanup.deferCleanup(connection);

statement.execute();
});

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasNoParent(),
span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0))));

assertThat(executedSql).hasSize(1);
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void testSqlCommenterPreparedStatementUpdate(boolean largeUpdate) throws SQLException {
List<String> executedSql = new ArrayList<>();
Connection connection = new TestConnection(executedSql::add);

String query = "INSERT INTO test VALUES(1)";
testing.runWithSpan(
"parent",
() -> {
PreparedStatement statement = connection.prepareStatement(query);
cleanup.deferCleanup(statement);
cleanup.deferCleanup(connection);

if (largeUpdate) {
statement.executeLargeUpdate();
} else {
statement.executeUpdate();
}
});

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasNoParent(),
span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0))));

assertThat(executedSql).hasSize(1);
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,21 @@ public void setBinaryStream(int parameterIndex, InputStream x) throws SQLExcepti
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
delegate.setObject(parameterIndex, x, targetSqlType);
putParameter(parameterIndex, x);
}

@SuppressWarnings("UngroupedOverloads")
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
delegate.setObject(parameterIndex, x);
putParameter(parameterIndex, x);
}

@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
throws SQLException {
delegate.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
putParameter(parameterIndex, x);
}

@Override
Expand Down Expand Up @@ -419,15 +422,26 @@ private <T, E extends Exception> T wrapBatchCall(ThrowingSupplier<T, E> callable
public void setObject(int parameterIndex, Object x, SQLType targetSqlType, int scaleOrLength)
throws SQLException {
delegate.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
putParameter(parameterIndex, x);
}

@Override
public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throws SQLException {
delegate.setObject(parameterIndex, x, targetSqlType);
putParameter(parameterIndex, x);
}

@Override
public long executeLargeUpdate() throws SQLException {
return wrapCall(query, delegate::executeLargeUpdate);
return wrapCall(
query,
() -> {
try {
return delegate.executeLargeUpdate();
} catch (UnsupportedOperationException ignored) {
// Fallback for drivers that only implement executeUpdate
return (long) delegate.executeUpdate();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -329,7 +330,22 @@ public long getLargeMaxRows() throws SQLException {

@Override
public long[] executeLargeBatch() throws SQLException {
return wrapBatchCall(delegate::executeLargeBatch);
return wrapBatchCall(
() -> {
try {
return delegate.executeLargeBatch();
} catch (UnsupportedOperationException
| SQLFeatureNotSupportedException
| AbstractMethodError ignored) {
// Older drivers rely on the default executeLargeBatch implementation, which throws.
int[] batchResult = delegate.executeBatch();
long[] converted = new long[batchResult.length];
for (int index = 0; index < batchResult.length; index++) {
converted[index] = batchResult[index];
}
return converted;
}
});
}

@Override
Expand Down
Loading
Loading