Skip to content

Commit b097f57

Browse files
authored
Surface websphere cell and server name on process tags (#8880)
1 parent 51ddd0b commit b097f57

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

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

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.SortedMap;
1212
import java.util.TreeMap;
13+
import java.util.function.Function;
1314
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
1516
import org.slf4j.Logger;
@@ -18,6 +19,14 @@
1819
public class ProcessTags {
1920
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessTags.class);
2021
private static boolean enabled = Config.get().isExperimentalPropagateProcessTagsEnabled();
22+
public static final String CLUSTER_NAME = "cluster.name";
23+
public static final String SERVER_NAME = "server.name";
24+
public static final String ENTRYPOINT_NAME = "entrypoint.name";
25+
public static final String ENTRYPOINT_BASEDIR = "entrypoint.basedir";
26+
public static final String ENTRYPOINT_WORKDIR = "entrypoint.workdir";
27+
28+
// visible for testing
29+
static Function<String, String> envGetter = System::getenv;
2130

2231
private static class Lazy {
2332
// the tags are used to compute a hash for dsm hence that map must be sorted.
@@ -31,22 +40,50 @@ private static SortedMap<String, String> loadTags() {
3140
if (enabled) {
3241
try {
3342
fillBaseTags(tags);
34-
fillJbossTags(tags);
43+
fillJeeTags(tags);
3544
} catch (Throwable t) {
3645
LOGGER.debug("Unable to calculate default process tags", t);
3746
}
3847
}
3948
return tags;
4049
}
4150

42-
private static void insertSysPropIfPresent(
51+
private static void fillJeeTags(SortedMap<String, String> tags) {
52+
if (fillJbossTags(tags)) {
53+
return;
54+
}
55+
fillWebsphereTags(tags);
56+
}
57+
58+
private static void insertTagFromSysPropIfPresent(
4359
Map<String, String> tags, String propKey, String tagKey) {
44-
String value = System.getProperty(propKey);
60+
String value = maybeGetSystemProperty(propKey);
4561
if (value != null) {
4662
tags.put(tagKey, value);
4763
}
4864
}
4965

66+
private static String maybeGetSystemProperty(String propKey) {
67+
try {
68+
return System.getProperty(propKey);
69+
} catch (Throwable ignored) {
70+
}
71+
return null;
72+
}
73+
74+
private static boolean insertTagFromEnvIfPresent(
75+
Map<String, String> tags, String envKey, String tagKey) {
76+
try {
77+
String value = envGetter.apply(envKey);
78+
if (value != null) {
79+
tags.put(tagKey, value);
80+
return true;
81+
}
82+
} catch (Throwable ignored) {
83+
}
84+
return false;
85+
}
86+
5087
private static boolean insertLastPathSegmentIfPresent(
5188
Map<String, String> tags, String path, String tagKey) {
5289
if (path == null || path.isEmpty()) {
@@ -63,31 +100,47 @@ private static boolean insertLastPathSegmentIfPresent(
63100
return false;
64101
}
65102

103+
private static boolean hasSystemProperty(String propKey) {
104+
try {
105+
return System.getProperties().containsKey(propKey);
106+
} catch (Throwable ignored) {
107+
}
108+
return false;
109+
}
110+
66111
private static void fillBaseTags(Map<String, String> tags) {
67112
final CapturedEnvironment.ProcessInfo processInfo =
68113
CapturedEnvironment.get().getProcessInfo();
69114
if (processInfo.mainClass != null) {
70-
tags.put("entrypoint.name", processInfo.mainClass);
115+
tags.put(ENTRYPOINT_NAME, processInfo.mainClass);
71116
tags.put("entrypoint.type", "class");
72117
}
73118
if (processInfo.jarFile != null) {
74119
final String jarName = processInfo.jarFile.getName();
75-
tags.put("entrypoint.name", jarName.substring(0, jarName.length() - 4)); // strip .jar
120+
tags.put(ENTRYPOINT_NAME, jarName.substring(0, jarName.length() - 4)); // strip .jar
76121
tags.put("entrypoint.type", "jar");
77-
insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), "entrypoint.basedir");
122+
insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), ENTRYPOINT_BASEDIR);
78123
}
79124

80-
insertLastPathSegmentIfPresent(tags, System.getProperty("user.dir"), "entrypoint.workdir");
125+
insertLastPathSegmentIfPresent(tags, maybeGetSystemProperty("user.dir"), ENTRYPOINT_WORKDIR);
81126
}
82127

83-
private static void fillJbossTags(Map<String, String> tags) {
128+
private static boolean fillJbossTags(Map<String, String> tags) {
84129
if (insertLastPathSegmentIfPresent(
85-
tags, System.getProperty("jboss.home.dir"), "jboss.home")) {
86-
insertSysPropIfPresent(tags, "jboss.server.name", "server.name");
87-
tags.put(
88-
"jboss.mode",
89-
System.getProperties().containsKey("[Standalone]") ? "standalone" : "domain");
130+
tags, maybeGetSystemProperty("jboss.home.dir"), "jboss.home")) {
131+
insertTagFromSysPropIfPresent(tags, "jboss.server.name", SERVER_NAME);
132+
tags.put("jboss.mode", hasSystemProperty("[Standalone]") ? "standalone" : "domain");
133+
return true;
90134
}
135+
return false;
136+
}
137+
138+
private static boolean fillWebsphereTags(Map<String, String> tags) {
139+
if (insertTagFromEnvIfPresent(tags, "WAS_CELL", CLUSTER_NAME)) {
140+
insertTagFromEnvIfPresent(tags, "SERVER_NAME", SERVER_NAME);
141+
return true;
142+
}
143+
return false;
91144
}
92145

93146
static void calculate() {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ class ProcessTagsForkedTest extends DDSpecification {
6161
null | "[Standalone]" | "standalone" | "entrypoint.basedir:somewhere,entrypoint.name:jboss-modules,entrypoint.type:jar,entrypoint.workdir:[^,]+" // don't expect jboss tags since home is missing
6262
}
6363

64+
def 'should load websphere tags (#expected)'() {
65+
setup:
66+
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
67+
ProcessTags.envGetter = key -> {
68+
switch (key) {
69+
case "WAS_CELL":
70+
return cellName
71+
case "SERVER_NAME":
72+
return serverName
73+
default:
74+
return null
75+
}
76+
}
77+
ProcessTags.reset()
78+
when:
79+
def tags = ProcessTags.getTagsForSerialization()
80+
then:
81+
assert tags =~ expected
82+
cleanup:
83+
ProcessTags.envGetter = System::getenv
84+
ProcessTags.reset()
85+
where:
86+
cellName | serverName | expected
87+
"cell1" | "server1" | "cluster.name:cell1,.+,server.name:server1.*"
88+
null | "server1" | "^((?!cluster.name|server.name).)*\$"
89+
}
90+
6491
def 'should not calculate process tags by default'() {
6592
when:
6693
ProcessTags.reset()

0 commit comments

Comments
 (0)