Cache DbInfo in OpenTelemetryDataSource (#8025)
Resolves https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/7984
This commit is contained in:
parent
288c407d12
commit
c6adaa35de
|
@ -47,6 +47,7 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
|
||||||
private final DataSource delegate;
|
private final DataSource delegate;
|
||||||
private final Instrumenter<DataSource, Void> dataSourceInstrumenter;
|
private final Instrumenter<DataSource, Void> dataSourceInstrumenter;
|
||||||
private final Instrumenter<DbRequest, Void> statementInstrumenter;
|
private final Instrumenter<DbRequest, Void> statementInstrumenter;
|
||||||
|
private volatile DbInfo cachedDbInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a OpenTelemetry DataSource wrapping another DataSource.
|
* Create a OpenTelemetry DataSource wrapping another DataSource.
|
||||||
|
@ -74,14 +75,14 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
Connection connection = wrapCall(delegate::getConnection);
|
Connection connection = wrapCall(delegate::getConnection);
|
||||||
DbInfo dbInfo = computeDbInfo(connection);
|
DbInfo dbInfo = getDbInfo(connection);
|
||||||
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
|
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection(String username, String password) throws SQLException {
|
public Connection getConnection(String username, String password) throws SQLException {
|
||||||
Connection connection = wrapCall(() -> delegate.getConnection(username, password));
|
Connection connection = wrapCall(() -> delegate.getConnection(username, password));
|
||||||
DbInfo dbInfo = computeDbInfo(connection);
|
DbInfo dbInfo = getDbInfo(connection);
|
||||||
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
|
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,4 +148,11 @@ public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
|
||||||
this.dataSourceInstrumenter.end(context, delegate, null, null);
|
this.dataSourceInstrumenter.end(context, delegate, null, null);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DbInfo getDbInfo(Connection connection) {
|
||||||
|
if (cachedDbInfo == null) {
|
||||||
|
cachedDbInfo = computeDbInfo(connection);
|
||||||
|
}
|
||||||
|
return cachedDbInfo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue