diff --git a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java index 58b67a3..7277356 100644 --- a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java +++ b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java @@ -25,17 +25,14 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import java.time.Duration; import java.time.format.DateTimeParseException; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.logging.Level; @@ -63,6 +60,8 @@ public class ConfigService { private static final String API_ENDPOINT_PROPERTY_NAME = "apptuit.api_url"; private static final String HOST_TAG_NAME = "host"; + private static final String UUID_TEMPLATE_VARIABLE = "${UUID}"; + private static final String PID_TEMPLATE_VARIABLE = "${PID}"; private static final File JINSIGHT_HOME = new File(System.getProperty("user.home"), ".jinsight"); private static final File UNIX_JINSIGHT_CONF_DIR = new File("/etc/jinsight/"); @@ -293,6 +292,11 @@ private void loadGlobalTags(Properties config) throws ConfigurationException { String tag = tagAndValue[0].trim(); String value = tagAndValue[1].trim(); if (tag.length() > 0 && value.length() > 0) { + if (value.equalsIgnoreCase(UUID_TEMPLATE_VARIABLE)) { + value = UUID.randomUUID().toString(); + } else if (value.equalsIgnoreCase(PID_TEMPLATE_VARIABLE)) { + value = getThisJVMProcessID() + ""; + } loadedGlobalTags.put(tag, value); continue; } @@ -305,6 +309,24 @@ private void loadGlobalTags(Properties config) throws ConfigurationException { } } + static int getThisJVMProcessID() throws ConfigurationException { + try { + java.lang.management.RuntimeMXBean runtime = + java.lang.management.ManagementFactory.getRuntimeMXBean(); + java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm"); + jvm.setAccessible(true); + sun.management.VMManagement mgmt = + (sun.management.VMManagement) jvm.get(runtime); + java.lang.reflect.Method pid_method = + mgmt.getClass().getDeclaredMethod("getProcessId"); + pid_method.setAccessible(true); + + return (Integer) pid_method.invoke(mgmt); + } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new ConfigurationException("Error fetching " + PID_TEMPLATE_VARIABLE + " of JVM", e); + } + } + String getApiToken() { return apiToken; } diff --git a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java index a5fc87d..5c92e6e 100644 --- a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java +++ b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java @@ -16,9 +16,7 @@ package ai.apptuit.metrics.jinsight; -import static ai.apptuit.metrics.jinsight.ConfigService.REPORTER_PROPERTY_NAME; -import static ai.apptuit.metrics.jinsight.ConfigService.REPORTING_FREQ_PROPERTY_NAME; -import static ai.apptuit.metrics.jinsight.ConfigService.REPORTING_MODE_PROPERTY_NAME; +import static ai.apptuit.metrics.jinsight.ConfigService.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -222,6 +220,26 @@ public void testGetTagsMissing() throws Exception { assertEquals(globalTags, Collections.emptyMap()); } + @Test + public void testGlobalUUIDTag() throws Exception { + Properties p = new Properties(); + p.setProperty("global_tags", "jvmid:${UUID}"); + ConfigService configService = new ConfigService(p); + Map globalTags = configService.getGlobalTags(); + assertEquals(globalTags.size(), 1); + assertEquals(globalTags.get("jvmid").length(), 36); + } + + @Test + public void testGlobalProcessIDTag() throws Exception { + Properties p = new Properties(); + p.setProperty("global_tags", "pid:${PID}"); + ConfigService configService = new ConfigService(p); + Map globalTags = configService.getGlobalTags(); + assertEquals(globalTags.size(), 1); + assertEquals(globalTags.get("pid"), getThisJVMProcessID() + ""); + } + @Test public void testPutReporterHasHostTagAlways() throws Exception { Properties p = getApptuitReporterConfigProperties(ReportingMode.API_PUT);