Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection pool timeouts unit #7404

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public BatchCallback batchCallback(
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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;
Expand Down Expand Up @@ -53,6 +55,8 @@ void shouldReportMetrics() throws SQLException, InterruptedException {
hikariDataSource.setDataSource(dataSourceMock);
configure(hikariDataSource, null);

cleanup.deferCleanup(hikariDataSource);

// when
Connection hikariConnection = hikariDataSource.getConnection();
TimeUnit.MILLISECONDS.sleep(100);
Expand Down Expand Up @@ -126,4 +130,38 @@ void shouldNotBreakCustomUserMetrics() throws SQLException, InterruptedException
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();
}
}