diff --git a/dd-java-agent/agent-profiling/profiling-uploader/src/test/java/com/datadog/profiling/uploader/ProfileUploaderTest.java b/dd-java-agent/agent-profiling/profiling-uploader/src/test/java/com/datadog/profiling/uploader/ProfileUploaderTest.java index 05bf54bb0e8..a611bd20612 100644 --- a/dd-java-agent/agent-profiling/profiling-uploader/src/test/java/com/datadog/profiling/uploader/ProfileUploaderTest.java +++ b/dd-java-agent/agent-profiling/profiling-uploader/src/test/java/com/datadog/profiling/uploader/ProfileUploaderTest.java @@ -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( diff --git a/internal-api/src/main/java/datadog/trace/api/ProcessTags.java b/internal-api/src/main/java/datadog/trace/api/ProcessTags.java index 5b50e0f44aa..e3f3d91b03f 100644 --- a/internal-api/src/main/java/datadog/trace/api/ProcessTags.java +++ b/internal-api/src/main/java/datadog/trace/api/ProcessTags.java @@ -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 TAGS = loadTags(); + static final SortedMap TAGS = loadTags(Config.get()); static volatile UTF8BytesString serializedForm; static volatile List utf8ListForm; static volatile List stringListForm; - private static SortedMap loadTags() { + private static SortedMap loadTags(final Config config) { SortedMap 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); @@ -112,10 +113,17 @@ private static void fillBaseTags(Map 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 tags, final Config config) { + if (config.isServiceNameSetByUser()) { + tags.put("svc.user", "true"); + } else { + tags.put("svc.auto", config.getServiceName()); + } + } + private static boolean fillJbossTags(Map tags) { if (insertLastPathSegmentIfPresent( tags, SystemProperties.get("jboss.home.dir"), "jboss.home")) { @@ -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)); } } } diff --git a/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy b/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy index 5aae2906699..346631b5884 100644 --- a/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy +++ b/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy @@ -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 { @@ -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() @@ -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) } @@ -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": @@ -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 @@ -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