From fa68074affe887993ccc2e1c5dbab694cf4127e2 Mon Sep 17 00:00:00 2001 From: Andrea Marziali Date: Mon, 26 May 2025 10:40:03 +0200 Subject: [PATCH 1/2] Surface websphere cell and server name on process tags --- .../java/datadog/trace/api/ProcessTags.java | 77 ++++++++++++++++--- .../trace/api/ProcessTagsForkedTest.groovy | 27 +++++++ 2 files changed, 92 insertions(+), 12 deletions(-) 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 1a60e24378e..75a97afc820 100644 --- a/internal-api/src/main/java/datadog/trace/api/ProcessTags.java +++ b/internal-api/src/main/java/datadog/trace/api/ProcessTags.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import org.slf4j.Logger; @@ -18,6 +19,14 @@ public class ProcessTags { private static final Logger LOGGER = LoggerFactory.getLogger(ProcessTags.class); private static boolean enabled = Config.get().isExperimentalPropagateProcessTagsEnabled(); + public static final String CLUSTER_NAME = "cluster.name"; + public static final String SERVER_NAME = "server.name"; + public static final String ENTRYPOINT_NAME = "entrypoint.name"; + public static final String ENTRYPOINT_BASEDIR = "entrypoint.basedir"; + public static final String ENTRYPOINT_WORKDIR = "entrypoint.workdir"; + + // visible for testing + static Function envGetter = System::getenv; private static class Lazy { // the tags are used to compute a hash for dsm hence that map must be sorted. @@ -31,7 +40,7 @@ private static SortedMap loadTags() { if (enabled) { try { fillBaseTags(tags); - fillJbossTags(tags); + fillJeeTags(tags); } catch (Throwable t) { LOGGER.debug("Unable to calculate default process tags", t); } @@ -39,14 +48,42 @@ private static SortedMap loadTags() { return tags; } + private static void fillJeeTags(SortedMap tags) { + if (fillJbossTags(tags)) { + return; + } + fillWebsphereTags(tags); + } + private static void insertSysPropIfPresent( Map tags, String propKey, String tagKey) { - String value = System.getProperty(propKey); + String value = maybeGetSystemProperty(propKey); if (value != null) { tags.put(tagKey, value); } } + private static String maybeGetSystemProperty(String propKey) { + try { + return System.getProperty(propKey); + } catch (Throwable ignored) { + } + return null; + } + + private static boolean insertEnvIfPresent( + Map tags, String envKey, String tagKey) { + try { + String value = envGetter.apply(envKey); + if (value != null) { + tags.put(tagKey, value); + return true; + } + } catch (Throwable ignored) { + } + return false; + } + private static boolean insertLastPathSegmentIfPresent( Map tags, String path, String tagKey) { if (path == null || path.isEmpty()) { @@ -63,31 +100,47 @@ private static boolean insertLastPathSegmentIfPresent( return false; } + private static boolean hasSystemProperty(String propKey) { + try { + return System.getProperties().containsKey(propKey); + } catch (Throwable ignored) { + } + return false; + } + private static void fillBaseTags(Map tags) { final CapturedEnvironment.ProcessInfo processInfo = CapturedEnvironment.get().getProcessInfo(); if (processInfo.mainClass != null) { - tags.put("entrypoint.name", processInfo.mainClass); + tags.put(ENTRYPOINT_NAME, processInfo.mainClass); tags.put("entrypoint.type", "class"); } if (processInfo.jarFile != null) { final String jarName = processInfo.jarFile.getName(); - tags.put("entrypoint.name", jarName.substring(0, jarName.length() - 4)); // strip .jar + tags.put(ENTRYPOINT_NAME, jarName.substring(0, jarName.length() - 4)); // strip .jar tags.put("entrypoint.type", "jar"); - insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), "entrypoint.basedir"); + insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), ENTRYPOINT_BASEDIR); } - insertLastPathSegmentIfPresent(tags, System.getProperty("user.dir"), "entrypoint.workdir"); + insertLastPathSegmentIfPresent(tags, maybeGetSystemProperty("user.dir"), ENTRYPOINT_WORKDIR); } - private static void fillJbossTags(Map tags) { + private static boolean fillJbossTags(Map tags) { if (insertLastPathSegmentIfPresent( - tags, System.getProperty("jboss.home.dir"), "jboss.home")) { - insertSysPropIfPresent(tags, "jboss.server.name", "server.name"); - tags.put( - "jboss.mode", - System.getProperties().containsKey("[Standalone]") ? "standalone" : "domain"); + tags, maybeGetSystemProperty("jboss.home.dir"), "jboss.home")) { + insertSysPropIfPresent(tags, "jboss.server.name", SERVER_NAME); + tags.put("jboss.mode", hasSystemProperty("[Standalone]") ? "standalone" : "domain"); + return true; } + return false; + } + + private static boolean fillWebsphereTags(Map tags) { + if (insertEnvIfPresent(tags, "WAS_CELL", CLUSTER_NAME)) { + insertEnvIfPresent(tags, "SERVER_NAME", SERVER_NAME); + return true; + } + return false; } static void calculate() { 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 cc77b19b827..3a6fddf0b1b 100644 --- a/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy +++ b/internal-api/src/test/groovy/datadog/trace/api/ProcessTagsForkedTest.groovy @@ -61,6 +61,33 @@ class ProcessTagsForkedTest extends DDSpecification { null | "[Standalone]" | "standalone" | "entrypoint.basedir:somewhere,entrypoint.name:jboss-modules,entrypoint.type:jar,entrypoint.workdir:[^,]+" // don't expect jboss tags since home is missing } + def 'should load websphere tags (#expected)'() { + setup: + injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true") + ProcessTags.envGetter = key -> { + switch (key) { + case "WAS_CELL": + return cellName + case "SERVER_NAME": + return serverName + default: + return null + } + } + ProcessTags.reset() + when: + def tags = ProcessTags.getTagsForSerialization() + then: + assert tags =~ expected + cleanup: + ProcessTags.envGetter = System::getenv + ProcessTags.reset() + where: + cellName | serverName | expected + "cell1" | "server1" | "cluster.name:cell1,.+,server.name:server1.*" + null | "server1" | "^((?!cluster.name|server.name).)*\$" + } + def 'should not calculate process tags by default'() { when: ProcessTags.reset() From db037c4c580e40cdda8c4186347bc45ac6fb3554 Mon Sep 17 00:00:00 2001 From: Andrea Marziali Date: Wed, 28 May 2025 09:20:14 +0200 Subject: [PATCH 2/2] review --- .../src/main/java/datadog/trace/api/ProcessTags.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 75a97afc820..9d863419a89 100644 --- a/internal-api/src/main/java/datadog/trace/api/ProcessTags.java +++ b/internal-api/src/main/java/datadog/trace/api/ProcessTags.java @@ -55,7 +55,7 @@ private static void fillJeeTags(SortedMap tags) { fillWebsphereTags(tags); } - private static void insertSysPropIfPresent( + private static void insertTagFromSysPropIfPresent( Map tags, String propKey, String tagKey) { String value = maybeGetSystemProperty(propKey); if (value != null) { @@ -71,7 +71,7 @@ private static String maybeGetSystemProperty(String propKey) { return null; } - private static boolean insertEnvIfPresent( + private static boolean insertTagFromEnvIfPresent( Map tags, String envKey, String tagKey) { try { String value = envGetter.apply(envKey); @@ -128,7 +128,7 @@ private static void fillBaseTags(Map tags) { private static boolean fillJbossTags(Map tags) { if (insertLastPathSegmentIfPresent( tags, maybeGetSystemProperty("jboss.home.dir"), "jboss.home")) { - insertSysPropIfPresent(tags, "jboss.server.name", SERVER_NAME); + insertTagFromSysPropIfPresent(tags, "jboss.server.name", SERVER_NAME); tags.put("jboss.mode", hasSystemProperty("[Standalone]") ? "standalone" : "domain"); return true; } @@ -136,8 +136,8 @@ tags, maybeGetSystemProperty("jboss.home.dir"), "jboss.home")) { } private static boolean fillWebsphereTags(Map tags) { - if (insertEnvIfPresent(tags, "WAS_CELL", CLUSTER_NAME)) { - insertEnvIfPresent(tags, "SERVER_NAME", SERVER_NAME); + if (insertTagFromEnvIfPresent(tags, "WAS_CELL", CLUSTER_NAME)) { + insertTagFromEnvIfPresent(tags, "SERVER_NAME", SERVER_NAME); return true; } return false;