Skip to content

Commit bba254d

Browse files
committed
Fix duplicate trace injection for SQL Server and Oracle
1 parent 7b6259d commit bba254d

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public static AgentScope onEnter(
123123
span.setTag(DBM_TRACE_INJECTED, true);
124124
}
125125
}
126+
// For SQL Server and Oracle, trace context is propagated via context_info and v$session.action
127+
// respectively, so we should not also inject it into SQL comments to avoid duplication
128+
final boolean injectTraceInComment = injectTraceContext && !isSqlServer && !isOracle;
126129
sql =
127130
SQLCommenter.inject(
128131
sql,
@@ -131,7 +134,7 @@ public static AgentScope onEnter(
131134
dbInfo.getHost(),
132135
dbInfo.getDb(),
133136
traceParent,
134-
injectTraceContext,
137+
injectTraceInComment,
135138
appendComment);
136139
}
137140
DECORATE.onStatement(span, copy);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import datadog.trace.agent.test.AgentTestRunner
2+
import datadog.trace.api.config.TraceInstrumentationConfig
3+
import test.TestConnection
4+
import test.TestDatabaseMetaData
5+
import test.TestStatement
6+
7+
class OracleInjectionForkedTest extends AgentTestRunner {
8+
9+
@Override
10+
void configurePreAgent() {
11+
super.configurePreAgent()
12+
13+
injectSysConfig(TraceInstrumentationConfig.DB_DBM_PROPAGATION_MODE_MODE, "full")
14+
injectSysConfig("service.name", "my_service_name")
15+
}
16+
17+
static query = "SELECT 1 FROM DUAL"
18+
static serviceInjection = "ddps='my_service_name',dddbs='oracle',ddh='localhost',dddb='testdb'"
19+
20+
def "Oracle no trace injection with full propagation mode"() {
21+
setup:
22+
def connection = new TestConnection(false)
23+
def metadata = new TestDatabaseMetaData()
24+
metadata.setURL("jdbc:oracle:thin:@localhost:1521:testdb")
25+
connection.setMetaData(metadata)
26+
27+
when:
28+
def statement = connection.createStatement() as TestStatement
29+
statement.executeQuery(query)
30+
31+
then:
32+
// Should only have service metadata, not traceparent, because Oracle uses V$SESSION.ACTION
33+
assert statement.sql == "/*${serviceInjection}*/ ${query}"
34+
// Verify that the SQL does NOT contain traceparent
35+
assert !statement.sql.contains("traceparent")
36+
}
37+
}

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)