Skip to content

Commit 38e69eb

Browse files
authored
Add process tags related to service naming (#10480)
* Add process tags related to service naming * fix profiling mocks * use true for svc.user
1 parent 93b3199 commit 38e69eb

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

dd-java-agent/agent-profiling/profiling-uploader/src/test/java/com/datadog/profiling/uploader/ProfileUploaderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,9 @@ public void testShutdown() throws Exception {
828828
@ValueSource(booleans = {true, false})
829829
public void testRequestWithProcessTags(boolean processTagsEnabled) throws Exception {
830830
when(config.isExperimentalPropagateProcessTagsEnabled()).thenReturn(processTagsEnabled);
831+
if (processTagsEnabled) {
832+
when(config.isServiceNameSetByUser()).thenReturn(true);
833+
}
831834
ProcessTags.reset(config);
832835
uploader =
833836
new ProfileUploader(

internal-api/src/main/java/datadog/trace/api/ProcessTags.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ public class ProcessTags {
3333

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

41-
private static SortedMap<String, String> loadTags() {
41+
private static SortedMap<String, String> loadTags(final Config config) {
4242
SortedMap<String, String> tags = new TreeMap<>();
4343
if (enabled) {
4444
try {
4545
fillBaseTags(tags);
46+
fillServiceNameTags(tags, config);
4647
fillJeeTags(tags);
4748
} catch (Throwable t) {
4849
LOGGER.debug("Unable to calculate default process tags", t);
@@ -112,10 +113,17 @@ private static void fillBaseTags(Map<String, String> tags) {
112113
tags.put("entrypoint.type", "jar");
113114
insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), ENTRYPOINT_BASEDIR);
114115
}
115-
116116
insertLastPathSegmentIfPresent(tags, SystemProperties.get("user.dir"), ENTRYPOINT_WORKDIR);
117117
}
118118

119+
private static void fillServiceNameTags(final Map<String, String> tags, final Config config) {
120+
if (config.isServiceNameSetByUser()) {
121+
tags.put("svc.user", "true");
122+
} else {
123+
tags.put("svc.auto", config.getServiceName());
124+
}
125+
}
126+
119127
private static boolean fillJbossTags(Map<String, String> tags) {
120128
if (insertLastPathSegmentIfPresent(
121129
tags, SystemProperties.get("jboss.home.dir"), "jboss.home")) {
@@ -233,7 +241,7 @@ public static void reset(Config config) {
233241
synchronized (Lazy.TAGS) {
234242
empty();
235243
enabled = config.isExperimentalPropagateProcessTagsEnabled();
236-
Lazy.TAGS.putAll(Lazy.loadTags());
244+
Lazy.TAGS.putAll(Lazy.loadTags(config));
237245
}
238246
}
239247
}

internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROC
44

55
import datadog.trace.api.env.CapturedEnvironment
66
import datadog.trace.test.util.DDSpecification
7+
import datadog.trace.util.TraceUtils
78
import java.nio.file.Paths
89

910
class ProcessTagsForkedTest extends DDSpecification {
@@ -21,7 +22,6 @@ class ProcessTagsForkedTest extends DDSpecification {
2122

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

38+
def 'should add process tags for service name set by user #userServiceName'() {
39+
given:
40+
if (userServiceName != null) {
41+
injectSysConfig("service", userServiceName)
42+
}
43+
ProcessTags.reset()
44+
def tags = ProcessTags.getTagsForSerialization()
45+
expect:
46+
tags =~ expected
47+
where:
48+
userServiceName | expected
49+
null | "svc.auto:${TraceUtils.normalizeServiceName(Config.get().getServiceName())}"
50+
"custom" | "svc.user:true"
51+
}
52+
3853
def 'should load default tags jboss (mode #mode)'() {
3954
setup:
40-
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
4155
if (jbossHome != null) {
4256
System.setProperty("jboss.home.dir", jbossHome)
4357
}
@@ -62,7 +76,6 @@ class ProcessTagsForkedTest extends DDSpecification {
6276

6377
def 'should load websphere tags (#expected)'() {
6478
setup:
65-
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
6679
ProcessTags.envGetter = key -> {
6780
switch (key) {
6881
case "WAS_CELL":
@@ -87,24 +100,24 @@ class ProcessTagsForkedTest extends DDSpecification {
87100
null | "server1" | "^((?!cluster.name|server.name|server.type).)*\$"
88101
}
89102
90-
def 'calculate process tags by default'() {
103+
def 'can disable process tags'() {
91104
when:
105+
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "false")
92106
ProcessTags.reset()
93107
def processTags = ProcessTags.tagsForSerialization
94108
then:
95-
assert ProcessTags.enabled
96-
assert (processTags != null)
109+
assert !ProcessTags.enabled
110+
assert (processTags == null)
97111
when:
98112
ProcessTags.addTag("test", "value")
99113
then:
100-
assert (ProcessTags.tagsForSerialization != null)
101-
assert (ProcessTags.tagsAsStringList != null)
102-
assert (ProcessTags.tagsAsUTF8ByteStringList != null)
114+
assert (ProcessTags.tagsForSerialization == null)
115+
assert (ProcessTags.tagsAsStringList == null)
116+
assert (ProcessTags.tagsAsUTF8ByteStringList == null)
103117
}
104118
105119
def 'should lazily recalculate when a tag is added'() {
106120
setup:
107-
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
108121
ProcessTags.reset()
109122
when:
110123
def processTags = ProcessTags.tagsForSerialization
@@ -131,7 +144,6 @@ class ProcessTagsForkedTest extends DDSpecification {
131144
132145
def 'process tag value normalization'() {
133146
setup:
134-
ProcessTags.reset()
135147
ProcessTags.addTag("test", testValue)
136148
expect:
137149
assert ProcessTags.tagsAsStringList != null

0 commit comments

Comments
 (0)