Fix connection pool timeouts unit
And add a test that actually verifies it
MODIFY
instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/metrics/db/DbConnectionPoolMetrics.java
instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/metrics/db/DbConnectionPoolMetrics.java
@@ -108,7 +108,7 @@ public final class DbConnectionPoolMetrics {
public LongCounter connectionTimeouts() {
return meter
.counterBuilder("db.client.connections.timeouts")
- .setUnit("timeouts")
+ .setUnit("{timeouts}")
.setDescription(
"The number of connection timeouts that have occurred trying to obtain a connection from the pool.")
.build();

MODIFY
instrumentation/hikaricp-3.0/testing/src/main/java/io/opentelemetry/instrumentation/hikaricp/AbstractHikariInstrumentationTest.java
instrumentation/hikaricp-3.0/testing/src/main/java/io/opentelemetry/instrumentation/hikaricp/AbstractHikariInstrumentationTest.java
package io.opentelemetry.instrumentation.hikaricp;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchException;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
@@ -53,6 +55,8 @@ public abstract class AbstractHikariInstrumentationTest {
hikariDataSource.setDataSource(dataSourceMock);
configure(hikariDataSource, null);
+ cleanup.deferCleanup(hikariDataSource);
+
// when
Connection hikariConnection = hikariDataSource.getConnection();
TimeUnit.MILLISECONDS.sleep(100);
@@ -126,4 +130,38 @@ public abstract class AbstractHikariInstrumentationTest {
verify(userMetricsMock, atLeastOnce()).recordConnectionAcquiredNanos(anyLong());
verify(userMetricsMock, atLeastOnce()).recordConnectionUsageMillis(anyLong());
}
+
+ @Test
+ void shouldReportTimeouts() throws SQLException {
+ // given
+ when(dataSourceMock.getConnection())
+ .then(
+ invocation -> {
+ TimeUnit.MILLISECONDS.sleep(2_000);
+ throw new SQLException("timed out!");
+ });
+
+ HikariDataSource hikariDataSource = new HikariDataSource();
+ hikariDataSource.setPoolName("timingOutPool");
+ hikariDataSource.setDataSource(dataSourceMock);
+ hikariDataSource.setConnectionTimeout(250 /* millis */);
+ // start the pool without initializing connections
+ hikariDataSource.setInitializationFailTimeout(-1);
+ configure(hikariDataSource, null);
+
+ cleanup.deferCleanup(hikariDataSource);
+
+ // when
+ Exception thrown = catchException(hikariDataSource::getConnection);
+
+ // then
+ assertThat(thrown).isNotNull();
+
+ DbConnectionPoolMetricsAssertions.create(
+ testing(), "io.opentelemetry.hikaricp-3.0", "timingOutPool")
+ .disableMaxIdleConnections()
+ // the connection is not even acquired
+ .disableUseTime()
+ .assertConnectionPoolEmitsMetrics();
+ }
}

Fix connection pool timeouts unit (#7404)

And add a test that actually verifies it