Skip to content

Commit

Permalink
Merge pull request #77 from ApptuitAI/76-dev-branch
Browse files Browse the repository at this point in the history
additional support for UUID (for jvm ID) and PID tags in global tags.
  • Loading branch information
phakudi authored Dec 7, 2020
2 parents 24f7b58 + f8aaab0 commit f977928
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
32 changes: 27 additions & 5 deletions src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/");
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
24 changes: 21 additions & 3 deletions src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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<String, String> globalTags = configService.getGlobalTags();
assertEquals(globalTags.size(), 1);
assertEquals(globalTags.get("pid"), getThisJVMProcessID() + "");
}

@Test
public void testPutReporterHasHostTagAlways() throws Exception {
Properties p = getApptuitReporterConfigProperties(ReportingMode.API_PUT);
Expand Down

0 comments on commit f977928

Please sign in to comment.