diff --git a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/OpenTelemetryDataSource.java b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/OpenTelemetryDataSource.java index 8fc427ce2a..d32995f53d 100644 --- a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/OpenTelemetryDataSource.java +++ b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/OpenTelemetryDataSource.java @@ -47,6 +47,7 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable { private final DataSource delegate; private final Instrumenter dataSourceInstrumenter; private final Instrumenter 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; + } }