Skip to content

Commit f58e9d4

Browse files
committed
Unify jdbc tests
1 parent d69f077 commit f58e9d4

File tree

21 files changed

+3187
-2941
lines changed

21 files changed

+3187
-2941
lines changed

instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/JdbcInstrumentationTest.java

Lines changed: 5 additions & 1836 deletions
Large diffs are not rendered by default.

instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/PreparedStatementParametersTest.java

Lines changed: 5 additions & 593 deletions
Large diffs are not rendered by default.

instrumentation/jdbc/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/SqlCommenterTest.java

Lines changed: 5 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -5,175 +5,18 @@
55

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

8-
import static org.assertj.core.api.Assertions.assertThat;
9-
10-
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
11-
import io.opentelemetry.instrumentation.jdbc.TestConnection;
12-
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
8+
import io.opentelemetry.instrumentation.jdbc.testing.AbstractSqlCommenterTest;
139
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1410
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
15-
import java.sql.Connection;
16-
import java.sql.PreparedStatement;
17-
import java.sql.SQLException;
18-
import java.sql.Statement;
19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import org.junit.jupiter.api.Test;
2211
import org.junit.jupiter.api.extension.RegisterExtension;
23-
import org.junit.jupiter.params.ParameterizedTest;
24-
import org.junit.jupiter.params.provider.ValueSource;
2512

26-
class SqlCommenterTest {
27-
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
13+
class SqlCommenterTest extends AbstractSqlCommenterTest {
2814

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

32-
@Test
33-
void testSqlCommenterStatement() throws SQLException {
34-
List<String> executedSql = new ArrayList<>();
35-
Connection connection = new TestConnection(executedSql::add);
36-
Statement statement = connection.createStatement();
37-
38-
cleanup.deferCleanup(statement);
39-
cleanup.deferCleanup(connection);
40-
41-
String query = "SELECT 1";
42-
testing.runWithSpan("parent", () -> statement.execute(query));
43-
44-
testing.waitAndAssertTraces(
45-
trace ->
46-
trace.hasSpansSatisfyingExactly(
47-
span -> span.hasName("parent").hasNoParent(),
48-
span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0))));
49-
50-
assertThat(executedSql).hasSize(1);
51-
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
52-
}
53-
54-
@ParameterizedTest
55-
@ValueSource(booleans = {true, false})
56-
void testSqlCommenterStatementUpdate(boolean largeUpdate) throws SQLException {
57-
List<String> executedSql = new ArrayList<>();
58-
Connection connection = new TestConnection(executedSql::add);
59-
Statement statement = connection.createStatement();
60-
61-
cleanup.deferCleanup(statement);
62-
cleanup.deferCleanup(connection);
63-
64-
String query = "INSERT INTO test VALUES(1)";
65-
testing.runWithSpan(
66-
"parent",
67-
() -> {
68-
if (largeUpdate) {
69-
statement.executeLargeUpdate(query);
70-
} else {
71-
statement.execute(query);
72-
}
73-
});
74-
75-
testing.waitAndAssertTraces(
76-
trace ->
77-
trace.hasSpansSatisfyingExactly(
78-
span -> span.hasName("parent").hasNoParent(),
79-
span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0))));
80-
81-
assertThat(executedSql).hasSize(1);
82-
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
83-
}
84-
85-
@ParameterizedTest
86-
@ValueSource(booleans = {true, false})
87-
void testSqlCommenterStatementBatch(boolean largeUpdate) throws SQLException {
88-
List<String> executedSql = new ArrayList<>();
89-
Connection connection = new TestConnection(executedSql::add);
90-
Statement statement = connection.createStatement();
91-
92-
cleanup.deferCleanup(statement);
93-
cleanup.deferCleanup(connection);
94-
95-
testing.runWithSpan(
96-
"parent",
97-
() -> {
98-
statement.addBatch("INSERT INTO test VALUES(1)");
99-
statement.addBatch("INSERT INTO test VALUES(2)");
100-
if (largeUpdate) {
101-
statement.executeLargeBatch();
102-
} else {
103-
statement.executeBatch();
104-
}
105-
});
106-
107-
testing.waitAndAssertTraces(
108-
trace ->
109-
trace.hasSpansSatisfyingExactly(
110-
span -> span.hasName("parent").hasNoParent(),
111-
span ->
112-
span.hasName(
113-
SemconvStability.emitStableDatabaseSemconv()
114-
? "BATCH INSERT dbname.test"
115-
: "dbname")
116-
.hasParent(trace.getSpan(0))));
117-
118-
assertThat(executedSql).hasSize(2);
119-
assertThat(executedSql.get(0)).contains("INSERT INTO test VALUES(1)").contains("traceparent");
120-
assertThat(executedSql.get(1)).contains("INSERT INTO test VALUES(2)").contains("traceparent");
121-
}
122-
123-
@Test
124-
void testSqlCommenterPreparedStatement() throws SQLException {
125-
List<String> executedSql = new ArrayList<>();
126-
Connection connection = new TestConnection(executedSql::add);
127-
128-
String query = "SELECT 1";
129-
testing.runWithSpan(
130-
"parent",
131-
() -> {
132-
PreparedStatement statement = connection.prepareStatement(query);
133-
cleanup.deferCleanup(statement);
134-
cleanup.deferCleanup(connection);
135-
136-
statement.execute();
137-
});
138-
139-
testing.waitAndAssertTraces(
140-
trace ->
141-
trace.hasSpansSatisfyingExactly(
142-
span -> span.hasName("parent").hasNoParent(),
143-
span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0))));
144-
145-
assertThat(executedSql).hasSize(1);
146-
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
147-
}
148-
149-
@ParameterizedTest
150-
@ValueSource(booleans = {true, false})
151-
void testSqlCommenterPreparedStatementUpdate(boolean largeUpdate) throws SQLException {
152-
List<String> executedSql = new ArrayList<>();
153-
Connection connection = new TestConnection(executedSql::add);
154-
155-
String query = "INSERT INTO test VALUES(1)";
156-
testing.runWithSpan(
157-
"parent",
158-
() -> {
159-
PreparedStatement statement = connection.prepareStatement(query);
160-
cleanup.deferCleanup(statement);
161-
cleanup.deferCleanup(connection);
162-
163-
if (largeUpdate) {
164-
statement.executeLargeUpdate();
165-
} else {
166-
statement.executeUpdate();
167-
}
168-
});
169-
170-
testing.waitAndAssertTraces(
171-
trace ->
172-
trace.hasSpansSatisfyingExactly(
173-
span -> span.hasName("parent").hasNoParent(),
174-
span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0))));
175-
176-
assertThat(executedSql).hasSize(1);
177-
assertThat(executedSql.get(0)).contains(query).contains("traceparent");
18+
@Override
19+
protected InstrumentationExtension testing() {
20+
return testing;
17821
}
17922
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/OpenTelemetryPreparedStatement.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,21 @@ public void setBinaryStream(int parameterIndex, InputStream x) throws SQLExcepti
244244
@Override
245245
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
246246
delegate.setObject(parameterIndex, x, targetSqlType);
247+
putParameter(parameterIndex, x);
247248
}
248249

249250
@SuppressWarnings("UngroupedOverloads")
250251
@Override
251252
public void setObject(int parameterIndex, Object x) throws SQLException {
252253
delegate.setObject(parameterIndex, x);
254+
putParameter(parameterIndex, x);
253255
}
254256

255257
@Override
256258
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
257259
throws SQLException {
258260
delegate.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
261+
putParameter(parameterIndex, x);
259262
}
260263

261264
@Override
@@ -419,15 +422,26 @@ private <T, E extends Exception> T wrapBatchCall(ThrowingSupplier<T, E> callable
419422
public void setObject(int parameterIndex, Object x, SQLType targetSqlType, int scaleOrLength)
420423
throws SQLException {
421424
delegate.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
425+
putParameter(parameterIndex, x);
422426
}
423427

424428
@Override
425429
public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throws SQLException {
426430
delegate.setObject(parameterIndex, x, targetSqlType);
431+
putParameter(parameterIndex, x);
427432
}
428433

429434
@Override
430435
public long executeLargeUpdate() throws SQLException {
431-
return wrapCall(query, delegate::executeLargeUpdate);
436+
return wrapCall(
437+
query,
438+
() -> {
439+
try {
440+
return delegate.executeLargeUpdate();
441+
} catch (UnsupportedOperationException ignored) {
442+
// Fallback for drivers that only implement executeUpdate
443+
return (long) delegate.executeUpdate();
444+
}
445+
});
432446
}
433447
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/OpenTelemetryStatement.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.sql.Connection;
2929
import java.sql.ResultSet;
3030
import java.sql.SQLException;
31+
import java.sql.SQLFeatureNotSupportedException;
3132
import java.sql.SQLWarning;
3233
import java.sql.Statement;
3334
import java.util.ArrayList;
@@ -329,7 +330,22 @@ public long getLargeMaxRows() throws SQLException {
329330

330331
@Override
331332
public long[] executeLargeBatch() throws SQLException {
332-
return wrapBatchCall(delegate::executeLargeBatch);
333+
return wrapBatchCall(
334+
() -> {
335+
try {
336+
return delegate.executeLargeBatch();
337+
} catch (UnsupportedOperationException
338+
| SQLFeatureNotSupportedException
339+
| AbstractMethodError ignored) {
340+
// Older drivers rely on the default executeLargeBatch implementation, which throws.
341+
int[] batchResult = delegate.executeBatch();
342+
long[] converted = new long[batchResult.length];
343+
for (int index = 0; index < batchResult.length; index++) {
344+
converted[index] = batchResult[index];
345+
}
346+
return converted;
347+
}
348+
});
333349
}
334350

335351
@Override

0 commit comments

Comments
 (0)