Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load config from System Properties if present #96

Merged
merged 9 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 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,44 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System prop name should be jinsight.reporter (even though the property name is just reporter)

if(reporter != null && !reporter.equals("")){
systemProperties.put(REPORTER_PROPERTY_NAME, reporter);
}

String accessToken = getProperty(ACCESS_TOKEN_PROPERTY_NAME);
if(accessToken != null && !accessToken.equals("")){
systemProperties.put(ACCESS_TOKEN_PROPERTY_NAME, accessToken);
}

String apiEndpoint = getProperty(API_ENDPOINT_PROPERTY_NAME);
if(apiEndpoint != null && !apiEndpoint.equals("")){
systemProperties.put(API_ENDPOINT_PROPERTY_NAME, apiEndpoint);
}

String globalTags = getProperty(GLOBAL_TAGS_PROPERTY_NAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System prop name should be jinsight.global_tags (even though the property name is just global_tags)

if(globalTags != null && !globalTags.equals("")){
systemProperties.put(GLOBAL_TAGS_PROPERTY_NAME, globalTags);
}

return systemProperties;
}

private static String getProperty(String propertyName) {
String propertyValue = System.getenv(propertyName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not propertyName.contains(".") then propertyName="jinsight."+propertyName

Then all new constants GLOBAL_TAGS_SYSTEM_PROPERTY_NAME REPORTER_SYSTEM_PROPERTY_NAME etc can be deleted

if(propertyValue == null || propertyValue.equals("")){
propertyValue = System.getProperty(propertyName);
}
return propertyValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If propertyValue is "" return null, so that we dont have to do !="" check everywhere

}

private Sanitizer readSanitizer(Properties config) {
String configSanitizer = config.getProperty("apptuit.sanitizer");
if (configSanitizer != null && !configSanitizer.equals("")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ public void testAgentVersion() throws Exception {
assertEquals(System.getProperty("project.version"), cs.getAgentVersion());
}

@Test
public void testGetReporterFromSystemProperty() throws Exception {
System.setProperty("reporter", "APPTUIT");
ReporterType reporterType = ConfigService.getInstance().getReporterType();
assertEquals(ReporterType.APPTUIT, reporterType);
}

private Properties getDefaultConfigProperties() {
return new Properties();
}
Expand Down