Skip to content

Commit

Permalink
load config from System Properties if present (#96)
Browse files Browse the repository at this point in the history
* load config from System Properties if present

* read from system environment and then system property

* added unit test

* code and test refactoring

* updated test

* updated test

* incorporated review comments and updated test

* incorporated review comments and updated test

* removed unused imports

---------

Co-authored-by: Jaya Gupta <[email protected]>
  • Loading branch information
JayaGupta-Bicycle and Jaya Gupta authored May 3, 2023
1 parent c682218 commit 02608ab
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
39 changes: 39 additions & 0 deletions src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> loadSystemProperties() {
Map<String, Object> 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("")) {
Expand Down
54 changes: 47 additions & 7 deletions src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> 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();
}
Expand Down

0 comments on commit 02608ab

Please sign in to comment.