1010import java .util .Map ;
1111import java .util .SortedMap ;
1212import java .util .TreeMap ;
13+ import java .util .function .Function ;
1314import java .util .stream .Collectors ;
1415import java .util .stream .Stream ;
1516import org .slf4j .Logger ;
1819public 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 () {
0 commit comments