Skip to content

Commit 1b4a8c1

Browse files
authored
Merge branch 'master' into ygree/servicetalk-concurrent-instrumentation-0.42.56
2 parents 0b6f63a + d68f8f1 commit 1b4a8c1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ public static String inject(
8080
if (dbType != null) {
8181
final String firstWord = getFirstWord(sql);
8282

83-
// The Postgres JDBC parser doesn't allow SQL comments anywhere in a JDBC callable statements
83+
// The Postgres JDBC parser doesn't allow SQL comments anywhere in a JDBC
84+
// callable statements
8485
// https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/core/Parser.java#L1038
85-
// TODO: Could we inject the comment after the JDBC has been converted to standard SQL?
86+
// TODO: Could we inject the comment after the JDBC has been converted to
87+
// standard SQL?
8688
if (firstWord.startsWith("{") && dbType.startsWith("postgres")) {
8789
return sql;
8890
}
@@ -92,11 +94,17 @@ public static String inject(
9294
appendComment = true;
9395
}
9496

95-
// Both Postgres and MySQL are unhappy with anything before CALL in a stored procedure
97+
// Both Postgres and MySQL are unhappy with anything before CALL in a stored
98+
// procedure
9699
// invocation but they seem ok with it after so we force append mode
97100
if (firstWord.equalsIgnoreCase("call")) {
98101
appendComment = true;
99102
}
103+
104+
// Append the comment in the case of a pg_hint_plan extension
105+
if (dbType.startsWith("postgres") && containsPgHint(sql)) {
106+
appendComment = true;
107+
}
100108
}
101109

102110
AgentSpan currSpan = activeSpan();
@@ -295,4 +303,10 @@ private static int computeInitialCapacity() {
295303
+ CLOSE_COMMENT.length(); // two quotes, one equals & one comma * 5 + \* */
296304
return tagKeysLen + extraCharsLen;
297305
}
306+
307+
// pg_hint_plan extension works by checking the first block comment
308+
// we'll have to append the traced comment if there is a pghint
309+
private static boolean containsPgHint(String sql) {
310+
return sql.indexOf("/*+") > 0;
311+
}
298312
}

dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class SQLCommenterTest extends AgentTestRunner {
108108
"/*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"
109109
"/*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"
110110
"/*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"
111+
"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'*/"
112+
"/*+ 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'*/"
111113
}
112114

113115
def "test encode Sql Comment with peer service"() {

0 commit comments

Comments
 (0)