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

Intern db info #4263

Merged
merged 1 commit into from
Oct 4, 2021
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 @@ -58,7 +58,7 @@ public static void addDbInfo(
return;
}
DbInfo dbInfo = JdbcConnectionUrlParser.parse(url, props);
JdbcData.connectionInfo.set(connection, dbInfo);
JdbcData.connectionInfo.set(connection, JdbcData.intern(dbInfo));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,42 @@
package io.opentelemetry.instrumentation.jdbc.internal;

import io.opentelemetry.instrumentation.api.field.VirtualField;
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Map;
import java.util.WeakHashMap;

/** Holds info associated with JDBC connections and prepared statements. */
public final class JdbcData {

private static final Map<DbInfo, WeakReference<DbInfo>> dbInfos = new WeakHashMap<>();
public static VirtualField<Connection, DbInfo> connectionInfo =
VirtualField.find(Connection.class, DbInfo.class);
public static VirtualField<PreparedStatement, String> preparedStatement =
VirtualField.find(PreparedStatement.class, String.class);

private JdbcData() {}

/**
* Returns canonical representation of db info.
*
* @param dbInfo db info to canonicalize
* @return db info with same content as input db info. If two equal inputs are given to this
* method, both calls will return the same instance. This method may return one instance now
* and a different instance later if the original interned instance was garbage collected.
*/
public static DbInfo intern(DbInfo dbInfo) {
synchronized (dbInfos) {
WeakReference<DbInfo> reference = dbInfos.get(dbInfo);
if (reference != null) {
DbInfo result = reference.get();
if (result != null) {
return result;
}
}
dbInfos.put(dbInfo, new WeakReference<>(dbInfo));
return dbInfo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static DbInfo extractDbInfo(Connection connection) {
DbInfo dbInfo = JdbcData.connectionInfo.get(connection);
if (dbInfo == null) {
dbInfo = computeDbInfo(connection);
JdbcData.connectionInfo.set(connection, dbInfo);
JdbcData.connectionInfo.set(connection, JdbcData.intern(dbInfo));
}
return dbInfo;
}
Expand Down