Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datadog.trace.instrumentation.jdbc;

import static datadog.trace.api.Config.DBM_PROPAGATION_MODE_FULL;
import static datadog.trace.api.Config.DBM_PROPAGATION_MODE_STATIC;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.DBM_TRACE_INJECTED;
import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.INSTRUMENTATION_TIME_MS;
Expand Down Expand Up @@ -58,9 +57,7 @@ public class JDBCDecorator extends DatabaseClientDecorator<DBInfo> {
private static final boolean DBM_INJECT_SQL_BASE_HASH = Config.get().isDbmInjectSqlBaseHash();
private static final boolean PROPAGATE_PROCESS_TAGS =
Config.get().isExperimentalPropagateProcessTagsEnabled();
public static final boolean INJECT_COMMENT =
DBM_PROPAGATION_MODE.equals(DBM_PROPAGATION_MODE_FULL)
|| DBM_PROPAGATION_MODE.equals(DBM_PROPAGATION_MODE_STATIC);
public static final boolean INJECT_COMMENT = Config.get().isDbmCommentInjectionEnabled();
private static final boolean INJECT_TRACE_CONTEXT =
DBM_PROPAGATION_MODE.equals(DBM_PROPAGATION_MODE_FULL);
public static final boolean DBM_TRACE_PREPARED_STATEMENTS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,36 @@ class DBMBaseHashInjectionForkedTest extends InjectionTest {
}
}
}

class DBMFingerprintInjectionForkedTest extends InjectionTest {

@Override
void configurePreAgent() {
super.configurePreAgent()
injectSysConfig(TraceInstrumentationConfig.DB_DBM_PROPAGATION_MODE_MODE, "fingerprint")
}

def "fingerprint mode injects base hash without trace context"() {
setup:
ProcessTags.reset()
BaseHash.updateBaseHash(123456789L)
def connection = new TestConnection(false)

when:
def statement = connection.createStatement() as TestStatement
statement.executeQuery(query)

then:
assert statement.sql == "/*${serviceInjection},ddsh='123456789'*/ ${query}"
assertTraces(1) {
trace(1) {
span {
spanType DDSpanTypes.SQL
tags(false) {
"$Tags.BASE_HASH" "123456789"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ class JDBCDecoratorServicePropagationForkedTest extends JDBCDecoratorTest {
}
}

class JDBCDecoratorFingerprintPropagationForkedTest extends JDBCDecoratorTest {
@Override
protected void setupPropagationMode() {
injectSysConfig(DB_DBM_PROPAGATION_MODE_MODE, "fingerprint")
}

@Override
protected boolean expectedFromConfig() {
return false
}
}

class JDBCDecoratorNoPropagationForkedTest extends JDBCDecoratorTest {
@Override
protected void setupPropagationMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ class SQLCommenterTest extends InstrumentationSpecification {
"/*ddsh='-3750763034362895579'*/ SELECT *" | true | 234563 | true | "" | "/*ddsh='-3750763034362895579'*/ SELECT *"
}

def "fingerprint propagation mode enables base hash injection"() {
setup:
injectSysConfig("dd.service", "")
injectSysConfig("dd.env", "")
injectSysConfig("dbm.propagation.mode", "fingerprint")
ProcessTags.reset()
BaseHash.updateBaseHash(234563L)

expect:
Config.get().isDbmCommentInjectionEnabled()
Config.get().isDbmInjectSqlBaseHash()
SQLCommenter.inject("SELECT *", "", "", "", "", null, false) == "/*ddsh='234563'*/ SELECT *"
}

def "test encode Sql Comment with peer service"() {
setup:
injectSysConfig("dd.service", "SqlCommenter")
Expand Down
10 changes: 8 additions & 2 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,11 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
configProvider.getBoolean(
DB_DBM_ALWAYS_APPEND_SQL_COMMENT, DEFAULT_DB_DBM_ALWAYS_APPEND_SQL_COMMENT);

dbmInjectSqlBaseHash = configProvider.getBoolean(DB_DBM_INJECT_SQL_BASEHASH, false);
boolean dbmFingerprintPropagationMode =
DBM_PROPAGATION_MODE_FINGERPRINT.equals(dbmPropagationMode);
dbmInjectSqlBaseHash =
dbmFingerprintPropagationMode
|| configProvider.getBoolean(DB_DBM_INJECT_SQL_BASEHASH, false);

splitByTags = tryMakeImmutableSet(configProvider.getList(SPLIT_BY_TAGS));

Expand Down Expand Up @@ -5648,14 +5652,16 @@ public String getDbmPropagationMode() {
// Database monitoring propagation mode constants
public static final String DBM_PROPAGATION_MODE_STATIC = "service";
public static final String DBM_PROPAGATION_MODE_FULL = "full";
public static final String DBM_PROPAGATION_MODE_FINGERPRINT = "fingerprint";

// Helper method to check if comment injection is enabled
public boolean isDbmCommentInjectionEnabled() {
if (dbmPropagationMode == null) {
return false;
}
return dbmPropagationMode.equals(DBM_PROPAGATION_MODE_FULL)
|| dbmPropagationMode.equals(DBM_PROPAGATION_MODE_STATIC);
|| dbmPropagationMode.equals(DBM_PROPAGATION_MODE_STATIC)
|| dbmPropagationMode.equals(DBM_PROPAGATION_MODE_FINGERPRINT);
}

private void logIgnoredSettingWarning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3347,6 +3347,30 @@ class ConfigTest extends DDSpecification {
"false" | "true" | false // sys prop takes precedence
}

def "dbm propagation mode #propagationMode enables base hash injection #expectedBaseHashInjection"() {
setup:
System.setProperty("dd.dbm.propagation.mode", propagationMode)
if (injectBaseHash != null) {
System.setProperty("dd.dbm.inject.sql.basehash", injectBaseHash)
}

when:
def config = new Config()

then:
config.isDbmCommentInjectionEnabled() == expectedCommentInjection
config.isDbmInjectSqlBaseHash() == expectedBaseHashInjection

where:
propagationMode | injectBaseHash | expectedCommentInjection | expectedBaseHashInjection
"disabled" | null | false | false
"service" | null | true | false
"full" | null | true | false
"fingerprint" | null | true | true
"fingerprint" | "false" | true | true
"service" | "true" | true | true
}

def "db client info fetching enabled with sys = #sys env = #env"() {
setup:
if (sys != null) {
Expand Down
Loading