From 00ae9dfba4d0d94c6ad86c0d164fa1f1f78a3cb0 Mon Sep 17 00:00:00 2001 From: Eden Gorevoy Date: Wed, 21 May 2025 14:53:26 -0400 Subject: [PATCH 1/2] Append comment if there's a pg plan hint --- .../instrumentation/jdbc/SQLCommenter.java | 20 ++++++++++++++++--- .../src/test/groovy/SQLCommenterTest.groovy | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java b/dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java index 979a08e7232..075ccbbbcc7 100644 --- a/dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java +++ b/dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java @@ -80,9 +80,11 @@ public static String inject( if (dbType != null) { final String firstWord = getFirstWord(sql); - // The Postgres JDBC parser doesn't allow SQL comments anywhere in a JDBC callable statements + // The Postgres JDBC parser doesn't allow SQL comments anywhere in a JDBC + // callable statements // https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/core/Parser.java#L1038 - // TODO: Could we inject the comment after the JDBC has been converted to standard SQL? + // TODO: Could we inject the comment after the JDBC has been converted to + // standard SQL? if (firstWord.startsWith("{") && dbType.startsWith("postgres")) { return sql; } @@ -92,11 +94,17 @@ public static String inject( appendComment = true; } - // Both Postgres and MySQL are unhappy with anything before CALL in a stored procedure + // Both Postgres and MySQL are unhappy with anything before CALL in a stored + // procedure // invocation but they seem ok with it after so we force append mode if (firstWord.equalsIgnoreCase("call")) { appendComment = true; } + + // Append the comment in the case of a pg_hint_plan extension + if (dbType.startsWith("postgres") && containsPgHint(sql)) { + appendComment = true; + } } AgentSpan currSpan = activeSpan(); @@ -295,4 +303,10 @@ private static int computeInitialCapacity() { + CLOSE_COMMENT.length(); // two quotes, one equals & one comma * 5 + \* */ return tagKeysLen + extraCharsLen; } + + // pg_hint_plan extension works by checking the first block comment + // we'll have to append the traced comment if there is a pghint + private static boolean containsPgHint(String sql) { + return sql.indexOf("/*+") > 0; + } } diff --git a/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy b/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy index a91f4b1d386..dddfa4e2fcd 100644 --- a/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy +++ b/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy @@ -108,6 +108,7 @@ class SQLCommenterTest extends AgentTestRunner { "/*traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-01'*/ SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-01'*/ SELECT * FROM foo" "/*customer-comment*/ SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*customer-comment*/ SELECT * FROM foo" "/*traceparent" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*traceparent" + "SELECT /*+ SeqScan(foo) */ * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT /*+ SeqScan(foo) */ * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/" } def "test encode Sql Comment with peer service"() { From e4c3c62d9bcc1ff4051f3eeaea082af913270ea6 Mon Sep 17 00:00:00 2001 From: Eden Gorevoy Date: Wed, 21 May 2025 15:10:29 -0400 Subject: [PATCH 2/2] Another test for comment placement --- .../instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy | 1 + 1 file changed, 1 insertion(+) diff --git a/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy b/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy index dddfa4e2fcd..79afb02602c 100644 --- a/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy +++ b/dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy @@ -109,6 +109,7 @@ class SQLCommenterTest extends AgentTestRunner { "/*customer-comment*/ SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*customer-comment*/ SELECT * FROM foo" "/*traceparent" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*traceparent" "SELECT /*+ SeqScan(foo) */ * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT /*+ SeqScan(foo) */ * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/" + "/*+ SeqScan(foo) */ SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "/*+ SeqScan(foo) */ SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/" } def "test encode Sql Comment with peer service"() {