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 6 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
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 @@ -50,14 +50,18 @@ public class ConfigService {

public static final String REPORTING_FREQ_PROPERTY_NAME = "reporting_frequency";
private static final String GLOBAL_TAGS_PROPERTY_NAME = "global_tags";
private static final String GLOBAL_TAGS_SYSTEM_PROPERTY_NAME = "jinsight.global_tags";
public static final String REPORTER_PROPERTY_NAME = "reporter";
public static final String REPORTER_SYSTEM_PROPERTY_NAME = "jinsight.reporter";

public static final String PROMETHEUS_EXPORTER_PORT = "prometheus.exporter_port";
public static final String PROMETHEUS_METRICS_PATH = "prometheus.exporter_endpoint";

public static final String REPORTING_MODE_PROPERTY_NAME = "apptuit.reporting_mode";
private static final String ACCESS_TOKEN_PROPERTY_NAME = "apptuit.access_token";
private static final String ACCESS_TOKEN_SYSTEM_PROPERTY_NAME = "jinsight.apptuit.access_token";
private static final String API_ENDPOINT_PROPERTY_NAME = "apptuit.api_url";
private static final String API_ENDPOINT_SYSTEM_PROPERTY_NAME = "jinsight.apptuit.api_url";

private static final String HOST_TAG_NAME = "host";
private static final String UUID_TEMPLATE_VARIABLE = "${UUID}";
Expand Down Expand Up @@ -184,9 +188,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_SYSTEM_PROPERTY_NAME);
if(reporter != null){
systemProperties.put(REPORTER_PROPERTY_NAME, reporter);
}

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

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

String globalTags = getProperty(GLOBAL_TAGS_SYSTEM_PROPERTY_NAME);
if(globalTags != null){
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 != null && !propertyValue.equals("") ? propertyValue : null;
}

private Sanitizer readSanitizer(Properties config) {
String configSanitizer = config.getProperty("apptuit.sanitizer");
if (configSanitizer != null && !configSanitizer.equals("")) {
Expand Down
51 changes: 44 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,38 @@ public void testAgentVersion() throws Exception {
assertEquals(System.getProperty("project.version"), cs.getAgentVersion());
}

@Test
public void testLoadSystemProperties() throws Exception {
// setup here since ConfigService.getInstance() has been initialized in several tests
System.setProperty("jinsight.reporter", "APPTUIT");
System.setProperty("jinsight.apptuit.access_token", "TEST_TOKEN");
System.setProperty("jinsight.apptuit.api_url", "http://api.test.bicycle.io");
System.setProperty("jinsight.global_tags", "env:dev");

Field singletonField = ConfigService.class.getDeclaredField("singleton");
singletonField.setAccessible(true);
singletonField.set(null, null);

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", "");
singletonField = ConfigService.class.getDeclaredField("singleton");
singletonField.setAccessible(true);
singletonField.set(null, null);
}

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