Skip to content
Merged
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
Expand Up @@ -828,6 +828,9 @@ public void testShutdown() throws Exception {
@ValueSource(booleans = {true, false})
public void testRequestWithProcessTags(boolean processTagsEnabled) throws Exception {
when(config.isExperimentalPropagateProcessTagsEnabled()).thenReturn(processTagsEnabled);
if (processTagsEnabled) {
when(config.isServiceNameSetByUser()).thenReturn(true);
}
ProcessTags.reset(config);
uploader =
new ProfileUploader(
Expand Down
16 changes: 12 additions & 4 deletions internal-api/src/main/java/datadog/trace/api/ProcessTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ public class ProcessTags {

private static class Lazy {
// the tags are used to compute a hash for dsm hence that map must be sorted.
static final SortedMap<String, String> TAGS = loadTags();
static final SortedMap<String, String> TAGS = loadTags(Config.get());
static volatile UTF8BytesString serializedForm;
static volatile List<UTF8BytesString> utf8ListForm;
static volatile List<String> stringListForm;

private static SortedMap<String, String> loadTags() {
private static SortedMap<String, String> loadTags(final Config config) {
SortedMap<String, String> tags = new TreeMap<>();
if (enabled) {
try {
fillBaseTags(tags);
fillServiceNameTags(tags, config);
fillJeeTags(tags);
} catch (Throwable t) {
LOGGER.debug("Unable to calculate default process tags", t);
Expand Down Expand Up @@ -112,10 +113,17 @@ private static void fillBaseTags(Map<String, String> tags) {
tags.put("entrypoint.type", "jar");
insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), ENTRYPOINT_BASEDIR);
}

insertLastPathSegmentIfPresent(tags, SystemProperties.get("user.dir"), ENTRYPOINT_WORKDIR);
}

private static void fillServiceNameTags(final Map<String, String> tags, final Config config) {
if (config.isServiceNameSetByUser()) {
tags.put("svc.user", "true");
} else {
tags.put("svc.auto", config.getServiceName());
}
}

private static boolean fillJbossTags(Map<String, String> tags) {
if (insertLastPathSegmentIfPresent(
tags, SystemProperties.get("jboss.home.dir"), "jboss.home")) {
Expand Down Expand Up @@ -233,7 +241,7 @@ public static void reset(Config config) {
synchronized (Lazy.TAGS) {
empty();
enabled = config.isExperimentalPropagateProcessTagsEnabled();
Lazy.TAGS.putAll(Lazy.loadTags());
Lazy.TAGS.putAll(Lazy.loadTags(config));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROC

import datadog.trace.api.env.CapturedEnvironment
import datadog.trace.test.util.DDSpecification
import datadog.trace.util.TraceUtils
import java.nio.file.Paths

class ProcessTagsForkedTest extends DDSpecification {
Expand All @@ -21,7 +22,6 @@ class ProcessTagsForkedTest extends DDSpecification {

def 'should load default tags for jar #jar and main class #cls'() {
given:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
CapturedEnvironment.useFixedProcessInfo(new CapturedEnvironment.ProcessInfo(cls, jar))
ProcessTags.reset()
def tags = ProcessTags.getTagsForSerialization()
Expand All @@ -35,9 +35,23 @@ class ProcessTagsForkedTest extends DDSpecification {
null | null | "entrypoint.workdir:[^,]+"
}

def 'should add process tags for service name set by user #userServiceName'() {
given:
if (userServiceName != null) {
injectSysConfig("service", userServiceName)
}
ProcessTags.reset()
def tags = ProcessTags.getTagsForSerialization()
expect:
tags =~ expected
where:
userServiceName | expected
null | "svc.auto:${TraceUtils.normalizeServiceName(Config.get().getServiceName())}"
"custom" | "svc.user:true"
}

def 'should load default tags jboss (mode #mode)'() {
setup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
if (jbossHome != null) {
System.setProperty("jboss.home.dir", jbossHome)
}
Expand All @@ -62,7 +76,6 @@ class ProcessTagsForkedTest extends DDSpecification {

def 'should load websphere tags (#expected)'() {
setup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
ProcessTags.envGetter = key -> {
switch (key) {
case "WAS_CELL":
Expand All @@ -87,24 +100,24 @@ class ProcessTagsForkedTest extends DDSpecification {
null | "server1" | "^((?!cluster.name|server.name|server.type).)*\$"
}

def 'calculate process tags by default'() {
def 'can disable process tags'() {
when:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "false")
ProcessTags.reset()
def processTags = ProcessTags.tagsForSerialization
then:
assert ProcessTags.enabled
assert (processTags != null)
assert !ProcessTags.enabled
assert (processTags == null)
when:
ProcessTags.addTag("test", "value")
then:
assert (ProcessTags.tagsForSerialization != null)
assert (ProcessTags.tagsAsStringList != null)
assert (ProcessTags.tagsAsUTF8ByteStringList != null)
assert (ProcessTags.tagsForSerialization == null)
assert (ProcessTags.tagsAsStringList == null)
assert (ProcessTags.tagsAsUTF8ByteStringList == null)
}

def 'should lazily recalculate when a tag is added'() {
setup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
ProcessTags.reset()
when:
def processTags = ProcessTags.tagsForSerialization
Expand All @@ -131,7 +144,6 @@ class ProcessTagsForkedTest extends DDSpecification {

def 'process tag value normalization'() {
setup:
ProcessTags.reset()
ProcessTags.addTag("test", testValue)
expect:
assert ProcessTags.tagsAsStringList != null
Expand Down