Skip to content

Commit 3d41cc5

Browse files
authored
Fix duplicate trace injection for SQL Server and Oracle DBM full propagation mode (#9224)
* Fix duplicate trace injection for SQL Server and Oracle * fix format * remove unused tests
1 parent 4db0eb1 commit 3d41cc5

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public static AgentScope onEnter(
123123
span.setTag(DBM_TRACE_INJECTED, true);
124124
}
125125
}
126+
// For SQL Server and Oracle, trace context is propagated via
127+
// context_info and v$session.action respectively.
128+
// we should not also inject it into SQL comments to avoid duplication
129+
final boolean injectTraceInComment = injectTraceContext && !isSqlServer && !isOracle;
126130
sql =
127131
SQLCommenter.inject(
128132
sql,
@@ -131,7 +135,7 @@ public static AgentScope onEnter(
131135
dbInfo.getHost(),
132136
dbInfo.getDb(),
133137
traceParent,
134-
injectTraceContext,
138+
injectTraceInComment,
135139
appendComment);
136140
}
137141
DECORATE.onStatement(span, copy);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class SQLCommenterTest extends AgentTestRunner {
7070
"SELECT * from FOO -- test query" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-01" | "SELECT * from FOO -- test query /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-01'*/"
7171
"SELECT /* customer-comment */ * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-01" | "SELECT /* customer-comment */ * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-01'*/"
7272
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | true | null | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/"
73+
"SELECT * FROM DUAL" | "SqlCommenter" | "Test" | "my-service" | "oracle" | "h" | "n" | "TestVersion" | false | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM DUAL /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/"
74+
"SELECT * FROM sys.tables" | "SqlCommenter" | "Test" | "my-service" | "sqlserver"| "h" | "n" | "TestVersion" | false | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM sys.tables /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/"
7375
"SELECT /* customer-comment */ * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | true | null | "SELECT /* customer-comment */ * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/"
7476
"SELECT * from FOO -- test query" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | true | null | "SELECT * from FOO -- test query /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/"
7577
"" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | ""

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SQLServerInjectionForkedTest extends AgentTestRunner {
1919
static query = "SELECT 1"
2020
static serviceInjection = "ddps='my_service_name',dddbs='sqlserver',ddh='localhost',dddb='testdb'"
2121

22-
def "SQL Server no trace injection with full"() {
22+
def "SQL Server no trace injection with full propagation mode"() {
2323
setup:
2424
def connection = new TestConnection(false)
2525
def metadata = new TestDatabaseMetaData()
@@ -31,6 +31,9 @@ class SQLServerInjectionForkedTest extends AgentTestRunner {
3131
statement.executeQuery(query)
3232

3333
then:
34+
// Should only have service metadata, not traceparent, because SQL Server uses CONTEXT_INFO
3435
assert statement.sql == "/*${serviceInjection}*/ ${query}"
36+
// Verify that the SQL does NOT contain traceparent
37+
assert !statement.sql.contains("traceparent")
3538
}
3639
}

0 commit comments

Comments
 (0)