Skip to content

Commit 4914d8a

Browse files
committed
use static final variable to decide if we should append SQL comment
1 parent e99adf5 commit 4914d8a

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static String onEnter(
127127
traceConfig(activeSpan()).getServiceMapping().getOrDefault(dbService, dbService);
128128
}
129129

130-
boolean append = DECORATE.shouldAppendSqlComment() || "sqlserver".equals(dbInfo.getType());
130+
boolean append = DECORATE.DBM_ALWAYS_APPEND_SQL_COMMENT || "sqlserver".equals(dbInfo.getType());
131131
sql =
132132
SQLCommenter.inject(
133133
sql, dbService, dbInfo.getType(), dbInfo.getHost(), dbInfo.getDb(), null, append);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class JDBCDecorator extends DatabaseClientDecorator<DBInfo> {
5959
DBM_PROPAGATION_MODE.equals(DBM_PROPAGATION_MODE_FULL);
6060
public static final boolean DBM_TRACE_PREPARED_STATEMENTS =
6161
Config.get().isDbmTracePreparedStatements();
62+
public static final boolean DBM_ALWAYS_APPEND_SQL_COMMENT =
63+
Config.get().isDbmAlwaysAppendSqlComment();
6264

6365
private volatile boolean warnedAboutDBMPropagationMode = false; // to log a warning only once
6466

@@ -423,8 +425,4 @@ public boolean shouldInjectSQLComment() {
423425
return Config.get().getDbmPropagationMode().equals(DBM_PROPAGATION_MODE_FULL)
424426
|| Config.get().getDbmPropagationMode().equals(DBM_PROPAGATION_MODE_STATIC);
425427
}
426-
427-
public boolean shouldAppendSqlComment() {
428-
return Config.get().isDbmAlwaysAppendSqlComment();
429-
}
430428
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static AgentScope onEnter(
127127
final boolean injectTraceInComment = injectTraceContext && !isSqlServer && !isOracle;
128128

129129
// prepend mode will prepend the SQL comment to the raw sql query
130-
boolean appendComment = DECORATE.shouldAppendSqlComment();
130+
boolean appendComment = DECORATE.DBM_ALWAYS_APPEND_SQL_COMMENT;
131131

132132
// There is a bug in the SQL Server JDBC driver that prevents
133133
// the generated keys from being returned when the

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import test.TestConnection
55
import test.TestPreparedStatement
66
import test.TestStatement
77

8-
class DBMInjectionForkedTest extends InstrumentationSpecification {
9-
8+
abstract class InjectionTest extends InstrumentationSpecification {
109
@Override
1110
void configurePreAgent() {
1211
super.configurePreAgent()
@@ -21,7 +20,9 @@ class DBMInjectionForkedTest extends InstrumentationSpecification {
2120

2221
static query = "SELECT 1"
2322
static serviceInjection = "ddps='my_service_name',dddbs='remapped_testdb',ddh='localhost'"
23+
}
2424

25+
class DBMInjectionForkedTest extends InjectionTest {
2526
def "prepared stmt"() {
2627
setup:
2728
def connection = new TestConnection(false)
@@ -35,30 +36,32 @@ class DBMInjectionForkedTest extends InstrumentationSpecification {
3536
assert statement.sql == "/*${serviceInjection}*/ ${query}"
3637
}
3738

38-
def "append comment on prepared stmt"() {
39+
def "single query"() {
3940
setup:
40-
injectSysConfig(TraceInstrumentationConfig.DB_DBM_ALWAYS_APPEND_SQL_COMMENT, "true")
4141
def connection = new TestConnection(false)
4242

4343
when:
44-
def statement = connection.prepareStatement(query) as TestPreparedStatement
45-
statement.execute()
44+
def statement = connection.createStatement() as TestStatement
45+
statement.executeQuery(query)
4646

4747
then:
48-
// even in full propagation mode, we cannot inject trace info in prepared statements
49-
assert statement.sql == "${query} /*${serviceInjection}*/"
48+
assert statement.sql == "/*${serviceInjection},traceparent='00-00000000000000000000000000000004-0000000000000003-01'*/ ${query}"
5049
}
50+
}
5151

52-
def "single query"() {
52+
class DBMAppendInjectionForkedTest extends InjectionTest {
53+
def "append comment on prepared stmt"() {
5354
setup:
55+
injectSysConfig(TraceInstrumentationConfig.DB_DBM_ALWAYS_APPEND_SQL_COMMENT, "true")
5456
def connection = new TestConnection(false)
5557

5658
when:
57-
def statement = connection.createStatement() as TestStatement
58-
statement.executeQuery(query)
59+
def statement = connection.prepareStatement(query) as TestPreparedStatement
60+
statement.execute()
5961

6062
then:
61-
assert statement.sql == "/*${serviceInjection},traceparent='00-00000000000000000000000000000006-0000000000000005-01'*/ ${query}"
63+
// even in full propagation mode, we cannot inject trace info in prepared statements
64+
assert statement.sql == "${query} /*${serviceInjection}*/"
6265
}
6366

6467
def "append comment on single query"() {
@@ -71,6 +74,6 @@ class DBMInjectionForkedTest extends InstrumentationSpecification {
7174
statement.executeQuery(query)
7275

7376
then:
74-
assert statement.sql == "${query} /*${serviceInjection},traceparent='00-00000000000000000000000000000008-0000000000000007-01'*/"
77+
assert statement.sql == "${query} /*${serviceInjection},traceparent='00-00000000000000000000000000000004-0000000000000003-01'*/"
7578
}
7679
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
2-
31
import datadog.trace.agent.test.InstrumentationSpecification
42
import datadog.trace.api.config.TraceInstrumentationConfig
53
import test.TestConnection
64
import test.TestDatabaseMetaData
75
import test.TestStatement
86

9-
class SQLServerInjectionForkedTest extends InstrumentationSpecification {
10-
7+
abstract class InjectionForkedTest extends InstrumentationSpecification {
118
@Override
129
void configurePreAgent() {
1310
super.configurePreAgent()
@@ -18,7 +15,9 @@ class SQLServerInjectionForkedTest extends InstrumentationSpecification {
1815

1916
static query = "SELECT 1"
2017
static serviceInjection = "ddps='my_service_name',dddbs='sqlserver',ddh='localhost',dddb='testdb'"
18+
}
2119

20+
class SQLServerInjectionForkedTest extends InjectionForkedTest {
2221
def "SQL Server no trace injection with full propagation mode"() {
2322
setup:
2423
def connection = new TestConnection(false)
@@ -51,7 +50,9 @@ class SQLServerInjectionForkedTest extends InstrumentationSpecification {
5150
then:
5251
assert statement.sql == "${query} /*${serviceInjection}*/"
5352
}
53+
}
5454

55+
class SQLServerAppendInjectionForkedTest extends InjectionForkedTest {
5556
def "SQL Server append comment when configured to do so"() {
5657
setup:
5758
injectSysConfig(TraceInstrumentationConfig.DB_DBM_ALWAYS_APPEND_SQL_COMMENT, "true")

0 commit comments

Comments
 (0)