Cache DbInfo in OpenTelemetryDataSource (#8025)

Resolves
https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/7984
This commit is contained in:
Lauri Tulmin 2023-03-13 14:10:11 +02:00 committed by GitHub
parent 288c407d12
commit c6adaa35de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -47,6 +47,7 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
private final DataSource delegate;
private final Instrumenter<DataSource, Void> dataSourceInstrumenter;
private final Instrumenter<DbRequest, Void> statementInstrumenter;
private volatile DbInfo cachedDbInfo;
/**
* Create a OpenTelemetry DataSource wrapping another DataSource.
@ -74,14 +75,14 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
@Override
public Connection getConnection() throws SQLException {
Connection connection = wrapCall(delegate::getConnection);
DbInfo dbInfo = computeDbInfo(connection);
DbInfo dbInfo = getDbInfo(connection);
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
Connection connection = wrapCall(() -> delegate.getConnection(username, password));
DbInfo dbInfo = computeDbInfo(connection);
DbInfo dbInfo = getDbInfo(connection);
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
}
@ -147,4 +148,11 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
this.dataSourceInstrumenter.end(context, delegate, null, null);
return result;
}
private DbInfo getDbInfo(Connection connection) {
if (cachedDbInfo == null) {
cachedDbInfo = computeDbInfo(connection);
}
return cachedDbInfo;
}
}