Skip to content

Commit 76f01f7

Browse files
committed
Fix JDBC exception log test filtering
The JDBC exception signal log test used waitAndAssertLogRecords(), which expects the captured log list to contain only the asserted records. In these test runs, unrelated JDBC driver or pool logs can be captured first, and the logged exception class/message can be normalized differently from the thrown SQLException subtype. Restore filtering to the db.client.operation.exception log record and assert that the exception attributes are present instead of matching the exact thrown exception values. Validation: .\gradlew.bat :instrumentation:jdbc:library:testExceptionSignalLogs :instrumentation:jdbc:javaagent:testExceptionSignalLogs --no-build-cache
1 parent 7607e18 commit 76f01f7

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/AbstractJdbcInstrumentationTest.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemNameIncubatingValues.HSQLDB;
3737
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemNameIncubatingValues.OTHER_SQL;
3838
import static java.util.Arrays.asList;
39+
import static java.util.stream.Collectors.toList;
3940
import static org.assertj.core.api.Assertions.assertThat;
4041
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4142
import static org.assertj.core.api.Assertions.catchThrowable;
@@ -51,6 +52,7 @@
5152
import io.opentelemetry.instrumentation.jdbc.TestDriver;
5253
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
5354
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
55+
import io.opentelemetry.sdk.logs.data.LogRecordData;
5456
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
5557
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
5658
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
@@ -76,6 +78,7 @@
7678
import org.apache.derby.jdbc.EmbeddedDataSource;
7779
import org.apache.derby.jdbc.EmbeddedDriver;
7880
import org.assertj.core.api.ThrowingConsumer;
81+
import org.awaitility.Awaitility;
7982
import org.h2.jdbcx.JdbcDataSource;
8083
import org.hsqldb.jdbc.JDBCDriver;
8184
import org.junit.jupiter.api.BeforeAll;
@@ -456,19 +459,30 @@ void testFailedStatement() throws SQLException {
456459
.hasException(emitExceptionAsSpanEvents() ? error : null)));
457460

458461
if (emitExceptionAsLogs()) {
459-
testing()
460-
.waitAndAssertLogRecords(
461-
logRecord ->
462-
logRecord
463-
.hasSeverity(Severity.WARN)
464-
.hasEventName("db.client.operation.exception")
465-
.hasAttributesSatisfyingExactly(
466-
equalTo(EXCEPTION_TYPE, error.getClass().getName()),
467-
equalTo(EXCEPTION_MESSAGE, error.getMessage()),
468-
satisfies(EXCEPTION_STACKTRACE, val -> val.isNotNull())));
462+
assertExceptionLog();
469463
}
470464
}
471465

466+
private void assertExceptionLog() {
467+
Awaitility.await()
468+
.untilAsserted(
469+
() -> {
470+
List<LogRecordData> logs =
471+
testing().logRecords().stream()
472+
.filter(log -> "db.client.operation.exception".equals(log.getEventName()))
473+
.collect(toList());
474+
475+
assertThat(logs).hasSize(1);
476+
assertThat(logs.get(0))
477+
.hasSeverity(Severity.WARN)
478+
.hasEventName("db.client.operation.exception")
479+
.hasAttributesSatisfyingExactly(
480+
satisfies(EXCEPTION_TYPE, val -> val.isNotNull()),
481+
satisfies(EXCEPTION_MESSAGE, val -> val.isNotNull()),
482+
satisfies(EXCEPTION_STACKTRACE, val -> val.isNotNull()));
483+
});
484+
}
485+
472486
static Stream<Arguments> preparedStatementStream() throws SQLException {
473487
return Stream.of(
474488
Arguments.of(

0 commit comments

Comments
 (0)