diff --git a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java index 64a9d89..7732a1a 100644 --- a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java +++ b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java @@ -184,9 +184,48 @@ private static Properties loadProperties(File configFilePath) throws IOException try (InputStream inputStream = new BufferedInputStream(new FileInputStream(configFilePath))) { config.load(inputStream); } + // override with system properties, if present + config.putAll(loadSystemProperties()); return config; } + private static Map loadSystemProperties() { + Map systemProperties = new HashMap<>(); + String reporter = getProperty(REPORTER_PROPERTY_NAME); + if(reporter != null){ + systemProperties.put(REPORTER_PROPERTY_NAME, reporter); + } + + String accessToken = getProperty(ACCESS_TOKEN_PROPERTY_NAME); + if(accessToken != null){ + systemProperties.put(ACCESS_TOKEN_PROPERTY_NAME, accessToken); + } + + String apiEndpoint = getProperty(API_ENDPOINT_PROPERTY_NAME); + if(apiEndpoint != null){ + systemProperties.put(API_ENDPOINT_PROPERTY_NAME, apiEndpoint); + } + + String globalTags = getProperty(GLOBAL_TAGS_PROPERTY_NAME); + if(globalTags != null){ + systemProperties.put(GLOBAL_TAGS_PROPERTY_NAME, globalTags); + } + + return systemProperties; + } + + private static String getProperty(String propertyName) { + String updatedPropertyName = propertyName; + if(!propertyName.contains(".")) { + updatedPropertyName = "jinsight." + propertyName; + } + String propertyValue = System.getenv(updatedPropertyName); + if(propertyValue == null || propertyValue.equals("")){ + propertyValue = System.getProperty(updatedPropertyName); + } + return propertyValue != null && !propertyValue.equals("") ? propertyValue : null; + } + private Sanitizer readSanitizer(Properties config) { String configSanitizer = config.getProperty("apptuit.sanitizer"); if (configSanitizer != null && !configSanitizer.equals("")) { diff --git a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java index 6b1f1dc..2533e36 100644 --- a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java +++ b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java @@ -16,21 +16,26 @@ package ai.apptuit.metrics.jinsight; -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; -import static org.junit.Assert.assertTrue; - import ai.apptuit.metrics.client.Sanitizer; import ai.apptuit.metrics.dropwizard.ApptuitReporter.ReportingMode; import ai.apptuit.metrics.jinsight.ConfigService.ReporterType; +import org.junit.Test; + +import java.lang.reflect.Field; import java.net.URL; import java.util.Collections; import java.util.Map; import java.util.Properties; import java.util.UUID; -import org.junit.Test; + +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.getThisJVMProcessID; +import static ai.apptuit.metrics.jinsight.ConfigService.initialize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** * @author Rajiv Shivane @@ -315,6 +320,41 @@ public void testAgentVersion() throws Exception { assertEquals(System.getProperty("project.version"), cs.getAgentVersion()); } + @SuppressWarnings("all") + @Test + public void testLoadSystemProperties() throws Exception { + // setup here since ConfigService.getInstance() has been initialized in several tests + System.setProperty("jinsight.reporter", "APPTUIT"); + System.setProperty("apptuit.access_token", "TEST_TOKEN"); + System.setProperty("apptuit.api_url", "http://api.test.bicycle.io"); + System.setProperty("jinsight.global_tags", "env:dev"); + + doPrivilegedAction(); + + initialize(); + + ConfigService cs = ConfigService.getInstance(); + assertEquals("APPTUIT", cs.getReporterType().name()); + assertEquals("TEST_TOKEN", cs.getApiToken()); + assertEquals("http://api.test.bicycle.io", cs.getApiUrl().toString()); + Map globalTags = cs.getGlobalTags(); + assertEquals("dev", globalTags.get("env")); + + // clean the setup + System.setProperty("jinsight.reporter", ""); + System.setProperty("jinsight.apptuit.access_token", ""); + System.setProperty("jinsight.apptuit.api_url", ""); + System.setProperty("jinsight.global_tags", ""); + doPrivilegedAction(); + } + + private void doPrivilegedAction() throws Exception { + Field singletonField = ConfigService.class.getDeclaredField("singleton"); + singletonField.setAccessible(true); + singletonField.set(null, null); + + } + private Properties getDefaultConfigProperties() { return new Properties(); }