Skip to content

Container hash tags propagation #9282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions dd-java-agent/instrumentation/jdbc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ addTestSuiteExtendingForDir('latestDepJava11Test', 'latestDepTest', 'test')
dependencies {
compileOnly group: 'com.zaxxer', name: 'HikariCP', version: '2.4.0'
testImplementation(testFixtures(project(':dd-java-agent:agent-iast')))
implementation project(':utils:container-utils')

// jdbc unit testing
testImplementation group: 'com.h2database', name: 'h2', version: '[1.3.168,1.3.169]'// first jdk 1.6 compatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SQLCommenterBenchmark {
private static final String parentService = "parent";
private static final String env = "env";
private static final String version = "version";
private static final String serviceHash = "service-hash";
private static final boolean injectTrace = true;

@Benchmark
Expand All @@ -36,6 +37,7 @@ public void testToComment() {
null,
env,
version,
traceParent);
traceParent,
serviceHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static String onEnter(
dbService =
traceConfig(activeSpan()).getServiceMapping().getOrDefault(dbService, dbService);
}
if (dbInfo.getType().equals("sqlserver")) {
if (dbInfo.getType().equals("sqlserver")) { // TODO why it's decided here?
sql =
SQLCommenter.append(
sql, dbService, dbInfo.getType(), dbInfo.getHost(), dbInfo.getDb());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;

import datadog.common.container.ContainerInfo;
import datadog.trace.api.Config;
import datadog.trace.api.ServiceHash;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import java.io.UnsupportedEncodingException;
Expand All @@ -22,6 +24,7 @@ public class SQLCommenter {
private static final String DD_PEER_SERVICE = "ddprs";
private static final String DD_ENV = encode("dde");
private static final String DD_VERSION = encode("ddpv");
private static final String DD_SERVICE_HASH = encode("ddsh");
private static final String TRACEPARENT = encode("traceparent");
private static final char EQUALS = '=';
private static final char COMMA = ',';
Expand Down Expand Up @@ -117,6 +120,10 @@ public static String inject(
final String parentService = config.getServiceName();
final String env = config.getEnv();
final String version = config.getVersion();
String containerTagsHash = ContainerInfo.get().getContainerTagsHash();
final String serviceHash =
Long.toString(ServiceHash.getBaseHash(parentService, env, containerTagsHash));
// config.isDbDbmInjectServiceHash() ; // TODO && baseHash != null
final int commentSize = capacity(traceParent, parentService, dbService, env, version);
StringBuilder sb = new StringBuilder(sql.length() + commentSize);
boolean commentAdded = false;
Expand All @@ -137,7 +144,8 @@ public static String inject(
peerService,
env,
version,
traceParent);
traceParent,
serviceHash);
sb.append(CLOSE_COMMENT);
} else {
sb.append(OPEN_COMMENT);
Expand All @@ -152,7 +160,8 @@ public static String inject(
peerService,
env,
version,
traceParent);
traceParent,
serviceHash);

sb.append(CLOSE_COMMENT);
sb.append(SPACE);
Expand Down Expand Up @@ -226,9 +235,9 @@ protected static boolean toComment(
final String peerService,
final String env,
final String version,
final String traceparent) {
final String traceparent,
final String serviceHash) {
int emptySize = sb.length();

append(sb, PARENT_SERVICE, parentService, false);
append(sb, DATABASE_SERVICE, dbService, sb.length() > emptySize);
append(sb, DD_HOSTNAME, hostname, sb.length() > emptySize);
Expand All @@ -241,6 +250,8 @@ protected static boolean toComment(
if (injectTrace) {
append(sb, TRACEPARENT, traceparent, sb.length() > emptySize);
}
// TODO only if DB_DBM_INJECT_SERVICE_HASH_ENABLED
append(sb, DD_SERVICE_HASH, serviceHash, sb.length() > emptySize);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inject the base hash into the SQL comment when the service mode is enabled.

return sb.length() > emptySize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.Tags

import datadog.trace.instrumentation.jdbc.SQLCommenter

import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace

class SQLCommenterTest extends AgentTestRunner {
Expand All @@ -30,25 +28,22 @@ class SQLCommenterTest extends AgentTestRunner {
" " | ""
}


def "test encode Sql Comment"() {
setup:
injectSysConfig("dd.service", ddService)
injectSysConfig("dd.env", ddEnv)
injectSysConfig("dd.version", ddVersion)

when:
String sqlWithComment = ""
String sqlWithComment
if (injectTrace) {
sqlWithComment = SQLCommenter.inject(query, dbService, dbType, host, dbName, traceParent, true, appendComment)
} else if (appendComment) {
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
}
else {
} else {
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
}


then:
sqlWithComment == expected

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.datadoghq.sketch.ddsketch.encoding.ByteArrayInput;
import com.datadoghq.sketch.ddsketch.encoding.GrowingByteArrayOutput;
import com.datadoghq.sketch.ddsketch.encoding.VarEncodingHelper;
import datadog.common.container.ContainerInfo;
import datadog.context.propagation.CarrierVisitor;
import datadog.trace.api.Config;
import datadog.trace.api.ProcessTags;
import datadog.trace.api.ServiceHash;
import datadog.trace.api.WellKnownTags;
import datadog.trace.api.datastreams.DataStreamsContext;
import datadog.trace.api.datastreams.DataStreamsTags;
Expand Down Expand Up @@ -269,19 +269,7 @@ private static DefaultPathwayContext decode(
}

public static long getBaseHash(WellKnownTags wellKnownTags) {
StringBuilder builder = new StringBuilder();
builder.append(wellKnownTags.getService());
builder.append(wellKnownTags.getEnv());

String primaryTag = Config.get().getPrimaryTag();
if (primaryTag != null) {
builder.append(primaryTag);
}
CharSequence processTags = ProcessTags.getTagsForSerialization();
if (processTags != null) {
builder.append(processTags);
}
return FNV64Hash.generateHash(builder.toString(), FNV64Hash.Version.v1);
return ServiceHash.getBaseHash(wellKnownTags, ContainerInfo.get().getContainerTagsHash());
Copy link
Contributor Author

@ygree ygree Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this into the ServiceHash class so that it is available in the DBM instrumentation.

}

private long generatePathwayHash(long nodeHash, long parentHash) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.core.datastreams

import datadog.common.container.ContainerInfo
import datadog.communication.ddagent.DDAgentFeaturesDiscovery
import datadog.trace.api.Config
import datadog.trace.api.DDTraceId
Expand Down Expand Up @@ -481,6 +482,32 @@ class DefaultPathwayContextTest extends DDCoreSpecification {
ProcessTags.reset()
}

def "ContainerTagsHash used in hash calculation when enabled propagateTagsEnabled=#propagateTagsEnabled"() {
when:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, propagateTagsEnabled.toString())
ProcessTags.reset()
ProcessTags.addTag("000", "first")
def firstBaseHash = DefaultPathwayContext.getBaseHash(wellKnownTags)

then:
ContainerInfo.get().setContainerTagsHash("<test-container-tags-hash>")
def secondBaseHash = DefaultPathwayContext.getBaseHash(wellKnownTags)

expect:
if (propagateTagsEnabled) {
assert secondBaseHash != firstBaseHash
} else {
assert secondBaseHash == firstBaseHash
}

cleanup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "false")
ProcessTags.reset()

where:
propagateTagsEnabled << [true, false]
}

def "Check context extractor decorator behavior"() {
given:
def sink = Mock(Sink)
Expand Down
47 changes: 47 additions & 0 deletions internal-api/src/main/java/datadog/trace/api/ServiceHash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package datadog.trace.api;

import datadog.trace.util.FNV64Hash;

public final class ServiceHash {

public static long getBaseHash(WellKnownTags wellKnownTags, String containerTagsHash) {
return getBaseHash(
wellKnownTags.getService(),
wellKnownTags.getEnv(),
Config.get().getPrimaryTag(),
ProcessTags.getTagsForSerialization(),
containerTagsHash);
}

public static long getBaseHash(
CharSequence serviceName, CharSequence env, String containerTagsHash) {
return getBaseHash(
serviceName,
env,
Config.get().getPrimaryTag(),
ProcessTags.getTagsForSerialization(),
containerTagsHash);
}

private static long getBaseHash(
CharSequence serviceName,
CharSequence env,
String primaryTag,
CharSequence processTags,
String containerTagsHash) {
StringBuilder builder = new StringBuilder();
builder.append(serviceName);
builder.append(env);

if (primaryTag != null) {
builder.append(primaryTag);
}
if (processTags != null) {
builder.append(processTags);
if (containerTagsHash != null && !containerTagsHash.isEmpty()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Populate the base hash with the container tag hash if the process tag propagation feature flag is.

builder.append(containerTagsHash);
}
}
return FNV64Hash.generateHash(builder.toString(), FNV64Hash.Version.v1);
}
}
Loading