-
Notifications
You must be signed in to change notification settings - Fork 350
[reference] eval: blind regeneration of PostgreSQL JDBC driver (toolkit output) #11997
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
Draft
jordan-wong
wants to merge
3
commits into
master
Choose a base branch
from
eval/postgresql-jdbc-db-blind-regen-20260719
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
dd-java-agent/instrumentation/postgresql/postgresql-42.0/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| muzzle { | ||
| pass { | ||
| group = "org.postgresql" | ||
| module = "postgresql" | ||
| versions = "[42.0.0,)" | ||
| } | ||
| } | ||
|
|
||
| apply from: "$rootDir/gradle/java.gradle" | ||
|
|
||
| addTestSuiteForDir('latestDepTest', 'test') | ||
|
|
||
| dependencies { | ||
| compileOnly group: 'org.postgresql', name: 'postgresql', version: '42.0.0' | ||
|
|
||
| testImplementation group: 'org.postgresql', name: 'postgresql', version: '42.0.0' | ||
| testImplementation group: 'org.testcontainers', name: 'postgresql', version: libs.versions.testcontainers.get() | ||
|
|
||
| latestDepTestImplementation group: 'org.postgresql', name: 'postgresql', version: '42.+' | ||
| } | ||
|
|
||
| tasks.withType(Test).configureEach { | ||
| usesService(testcontainersLimit) | ||
| environment 'TESTCONTAINERS_RYUK_DISABLED', 'true' | ||
| jvmArgs '-Dtestcontainers.reuse.enable=false' | ||
| } |
97 changes: 97 additions & 0 deletions
97
...2.0/src/main/java/datadog/trace/instrumentation/postgresql/PgPreparedStatementAdvice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package datadog.trace.instrumentation.postgresql; | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.JAVA_POSTGRESQL; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.POSTGRESQL_QUERY; | ||
|
|
||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import datadog.trace.bootstrap.instrumentation.jdbc.DBInfo; | ||
| import datadog.trace.bootstrap.instrumentation.jdbc.DBQueryInfo; | ||
| import java.sql.Statement; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.WeakHashMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class PgPreparedStatementAdvice { | ||
|
|
||
| public static final Map<Statement, String> PREPARED_SQL = | ||
| Collections.synchronizedMap(new WeakHashMap<Statement, String>()); | ||
|
|
||
| public static final class ConstructorAdvice { | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.This final Statement statement, @Advice.Argument(1) final Object query) { | ||
| // In PostgreSQL JDBC 42.0+, argument[1] is CachedQuery, use toString() to get SQL | ||
| if (query != null) { | ||
| PREPARED_SQL.put(statement, query.toString()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static final class ExecuteAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope onEnter(@Advice.This final Statement statement) { | ||
| final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class); | ||
| if (callDepth > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| final AgentSpan span = startSpan(JAVA_POSTGRESQL.toString(), POSTGRESQL_QUERY); | ||
| DECORATE.afterStart(span); | ||
|
|
||
| DBInfo dbInfo = InstrumentationContext.get(Statement.class, DBInfo.class).get(statement); | ||
| if (dbInfo == null) { | ||
| dbInfo = PgStatementAdvice.ExecuteQueryAdvice.extractDbInfo(statement); | ||
| if (dbInfo != null) { | ||
| InstrumentationContext.get(Statement.class, DBInfo.class).put(statement, dbInfo); | ||
| } | ||
| } | ||
| if (dbInfo != null) { | ||
| DECORATE.onConnection(span, dbInfo); | ||
| if (dbInfo.getPort() != null) { | ||
| DECORATE.setPeerPort(span, dbInfo.getPort()); | ||
| } | ||
| } | ||
|
|
||
| final String sql = PREPARED_SQL.get(statement); | ||
| if (sql != null) { | ||
| final DBQueryInfo dbQueryInfo = DBQueryInfo.ofPreparedStatement(sql); | ||
| DECORATE.onStatement(span, dbQueryInfo.getSql()); | ||
| span.setTag(Tags.DB_OPERATION, dbQueryInfo.getOperation()); | ||
|
|
||
| // DBM: inject SQL comment with trace context for prepared statements | ||
| // For prepared statements, we inject via modifying the stored SQL that will | ||
| // be sent to the server. The comment is only added to spans' context, | ||
| // the actual SQL modification for prepared statements happens at connection level. | ||
| PostgreSQLSQLCommenter.inject(sql, span, dbInfo); | ||
| } | ||
|
|
||
| return activateSpan(span); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| final AgentSpan span = scope.span(); | ||
| if (throwable != null) { | ||
| DECORATE.onError(span, throwable); | ||
| } | ||
| DECORATE.beforeFinish(span); | ||
| scope.close(); | ||
| span.finish(); | ||
| CallDepthThreadLocalMap.reset(Statement.class); | ||
| } | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
...ain/java/datadog/trace/instrumentation/postgresql/PgPreparedStatementInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package datadog.trace.instrumentation.postgresql; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import datadog.trace.agent.tooling.Instrumenter; | ||
|
|
||
| public final class PgPreparedStatementInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.postgresql.jdbc.PgPreparedStatement"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isConstructor(), | ||
| "datadog.trace.instrumentation.postgresql.PgPreparedStatementAdvice$ConstructorAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(isPublic()).and(named("executeQuery")).and(takesArguments(0)), | ||
| "datadog.trace.instrumentation.postgresql.PgPreparedStatementAdvice$ExecuteAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(isPublic()).and(named("executeUpdate")).and(takesArguments(0)), | ||
| "datadog.trace.instrumentation.postgresql.PgPreparedStatementAdvice$ExecuteAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(isPublic()).and(named("execute")).and(takesArguments(0)), | ||
| "datadog.trace.instrumentation.postgresql.PgPreparedStatementAdvice$ExecuteAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(isPublic()).and(named("executeBatch")).and(takesArguments(0)), | ||
| "datadog.trace.instrumentation.postgresql.PgPreparedStatementAdvice$ExecuteAdvice"); | ||
| } | ||
| } |
165 changes: 165 additions & 0 deletions
165
...gresql-42.0/src/main/java/datadog/trace/instrumentation/postgresql/PgStatementAdvice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package datadog.trace.instrumentation.postgresql; | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.JAVA_POSTGRESQL; | ||
| import static datadog.trace.instrumentation.postgresql.PostgreSQLDecorator.POSTGRESQL_QUERY; | ||
|
|
||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import datadog.trace.bootstrap.instrumentation.jdbc.DBInfo; | ||
| import datadog.trace.bootstrap.instrumentation.jdbc.DBQueryInfo; | ||
| import datadog.trace.bootstrap.instrumentation.jdbc.JDBCConnectionUrlParser; | ||
| import java.sql.Connection; | ||
| import java.sql.DatabaseMetaData; | ||
| import java.sql.Statement; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.WeakHashMap; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class PgStatementAdvice { | ||
|
|
||
| public static final Map<Statement, String> BATCH_SQL = | ||
| Collections.synchronizedMap(new WeakHashMap<Statement, String>()); | ||
|
|
||
| public static final class ExecuteQueryAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope onEnter( | ||
| @Advice.This final Statement statement, | ||
| @Advice.Argument(value = 0, readOnly = false) String sql) { | ||
| final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class); | ||
| if (callDepth > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| final AgentSpan span = startSpan(JAVA_POSTGRESQL.toString(), POSTGRESQL_QUERY); | ||
| DECORATE.afterStart(span); | ||
|
|
||
| DBInfo dbInfo = InstrumentationContext.get(Statement.class, DBInfo.class).get(statement); | ||
| if (dbInfo == null) { | ||
| dbInfo = extractDbInfo(statement); | ||
| if (dbInfo != null) { | ||
| InstrumentationContext.get(Statement.class, DBInfo.class).put(statement, dbInfo); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid the same consecutive call, the ContextStore could be kept in the local variable. |
||
| } | ||
| } | ||
| if (dbInfo != null) { | ||
| DECORATE.onConnection(span, dbInfo); | ||
| if (dbInfo.getPort() != null) { | ||
| DECORATE.setPeerPort(span, dbInfo.getPort()); | ||
| } | ||
| } | ||
|
|
||
| final DBQueryInfo dbQueryInfo = DBQueryInfo.ofStatement(sql); | ||
| DECORATE.onStatement(span, dbQueryInfo.getSql()); | ||
| span.setTag(Tags.DB_OPERATION, dbQueryInfo.getOperation()); | ||
|
|
||
| // DBM: inject SQL comment with trace context | ||
| sql = PostgreSQLSQLCommenter.inject(sql, span, dbInfo); | ||
|
|
||
| return activateSpan(span); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| final AgentSpan span = scope.span(); | ||
| if (throwable != null) { | ||
| DECORATE.onError(span, throwable); | ||
| } | ||
| DECORATE.beforeFinish(span); | ||
| scope.close(); | ||
| span.finish(); | ||
| CallDepthThreadLocalMap.reset(Statement.class); | ||
| } | ||
|
|
||
| public static DBInfo extractDbInfo(final Statement statement) { | ||
| try { | ||
| final Connection connection = statement.getConnection(); | ||
| if (connection != null) { | ||
| final DatabaseMetaData metaData = connection.getMetaData(); | ||
| final String url = metaData.getURL(); | ||
| final String user = metaData.getUserName(); | ||
| DBInfo dbInfo = JDBCConnectionUrlParser.parse(url, null); | ||
| if (user != null && !user.isEmpty()) { | ||
| dbInfo = dbInfo.toBuilder().user(user).build(); | ||
| } | ||
| return dbInfo; | ||
| } | ||
| } catch (final Exception ignored) { | ||
| // Unable to extract connection info | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static final class AddBatchAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter( | ||
| @Advice.This final Statement statement, @Advice.Argument(0) final String sql) { | ||
| BATCH_SQL.put(statement, sql); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use ContextStore instead of a global map. |
||
| } | ||
| } | ||
|
|
||
| public static final class ExecuteBatchAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static AgentScope onEnter(@Advice.This final Statement statement) { | ||
| final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class); | ||
| if (callDepth > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| final AgentSpan span = startSpan(JAVA_POSTGRESQL.toString(), POSTGRESQL_QUERY); | ||
| DECORATE.afterStart(span); | ||
|
|
||
| DBInfo dbInfo = InstrumentationContext.get(Statement.class, DBInfo.class).get(statement); | ||
| if (dbInfo == null) { | ||
| dbInfo = ExecuteQueryAdvice.extractDbInfo(statement); | ||
| if (dbInfo != null) { | ||
| InstrumentationContext.get(Statement.class, DBInfo.class).put(statement, dbInfo); | ||
| } | ||
| } | ||
| if (dbInfo != null) { | ||
| DECORATE.onConnection(span, dbInfo); | ||
| if (dbInfo.getPort() != null) { | ||
| DECORATE.setPeerPort(span, dbInfo.getPort()); | ||
| } | ||
| } | ||
|
|
||
| final String batchSql = BATCH_SQL.get(statement); | ||
| if (batchSql != null) { | ||
| final DBQueryInfo dbQueryInfo = DBQueryInfo.ofStatement(batchSql); | ||
| DECORATE.onStatement(span, dbQueryInfo.getSql()); | ||
| span.setTag(Tags.DB_OPERATION, dbQueryInfo.getOperation()); | ||
| } | ||
|
|
||
| return activateSpan(span); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { | ||
| if (scope == null) { | ||
| return; | ||
| } | ||
| final AgentSpan span = scope.span(); | ||
| if (throwable != null) { | ||
| DECORATE.onError(span, throwable); | ||
| } | ||
| DECORATE.beforeFinish(span); | ||
| scope.close(); | ||
| span.finish(); | ||
| CallDepthThreadLocalMap.reset(Statement.class); | ||
| } | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
....0/src/main/java/datadog/trace/instrumentation/postgresql/PgStatementInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package datadog.trace.instrumentation.postgresql; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import datadog.trace.agent.tooling.Instrumenter; | ||
|
|
||
| public final class PgStatementInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.postgresql.jdbc.PgStatement"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(named("executeQuery")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, String.class)), | ||
| "datadog.trace.instrumentation.postgresql.PgStatementAdvice$ExecuteQueryAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(named("executeUpdate")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, String.class)), | ||
| "datadog.trace.instrumentation.postgresql.PgStatementAdvice$ExecuteQueryAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(named("execute")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, String.class)), | ||
| "datadog.trace.instrumentation.postgresql.PgStatementAdvice$ExecuteQueryAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(named("addBatch")) | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, String.class)), | ||
| "datadog.trace.instrumentation.postgresql.PgStatementAdvice$AddBatchAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod().and(isPublic()).and(named("executeBatch")).and(takesArguments(0)), | ||
| "datadog.trace.instrumentation.postgresql.PgStatementAdvice$ExecuteBatchAdvice"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use ContextStore instead of a static shared map.