This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git
The following commit(s) were added to refs/heads/master by this push: new 9b1c672 Support setting override through system.properties and envs. (#997) 9b1c672 is described below commit 9b1c6724d21a93f022a79c79bdc3203a59b90e88 Author: 吴晟 Wu Sheng <wu.sh...@foxmail.com> AuthorDate: Thu Mar 29 17:20:49 2018 +0800 Support setting override through system.properties and envs. (#997) * Support setting override through system.properties and envs. * Remove test case. --- .../boot/config/ApplicationConfigLoader.java | 51 +++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/apm-collector/apm-collector-boot/src/main/java/org/apache/skywalking/apm/collector/boot/config/ApplicationConfigLoader.java b/apm-collector/apm-collector-boot/src/main/java/org/apache/skywalking/apm/collector/boot/config/ApplicationConfigLoader.java index 81050ae..902006c 100644 --- a/apm-collector/apm-collector-boot/src/main/java/org/apache/skywalking/apm/collector/boot/config/ApplicationConfigLoader.java +++ b/apm-collector/apm-collector-boot/src/main/java/org/apache/skywalking/apm/collector/boot/config/ApplicationConfigLoader.java @@ -16,11 +16,11 @@ * */ - package org.apache.skywalking.apm.collector.boot.config; import java.io.FileNotFoundException; import java.io.Reader; +import java.util.Iterator; import java.util.Map; import java.util.Properties; import org.apache.skywalking.apm.collector.core.module.ApplicationConfiguration; @@ -30,7 +30,13 @@ import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; /** - * @author peng-yongsheng + * Initialize collector settings with following sources. + * Use application.yml as primary setting, + * and fix missing setting by default settings in application-default.yml. + * + * At last, override setting by system.properties and system.envs if the key matches moduleName.provideName.settingKey. + * + * @author peng-yongsheng, wusheng */ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfiguration> { @@ -42,6 +48,7 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur ApplicationConfiguration configuration = new ApplicationConfiguration(); this.loadConfig(configuration); this.loadDefaultConfig(configuration); + this.overrideConfigBySystemEnv(configuration); return configuration; } @@ -94,4 +101,44 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur throw new ConfigFileNotFoundException(e.getMessage(), e); } } + + private void overrideConfigBySystemEnv(ApplicationConfiguration configuration) { + Iterator<Map.Entry<Object, Object>> entryIterator = System.getProperties().entrySet().iterator(); + while (entryIterator.hasNext()) { + Map.Entry<Object, Object> prop = entryIterator.next(); + overrideModuleSettings(configuration, prop.getKey().toString(), prop.getValue().toString(), true); + } + + Map<String, String> envs = System.getenv(); + for (String envKey : envs.keySet()) { + overrideModuleSettings(configuration, envKey, envs.get(envKey), false); + } + } + + private void overrideModuleSettings(ApplicationConfiguration configuration, String key, String value, + boolean isSystemProperty) { + int moduleAndConfigSeparator = key.indexOf('.'); + if (moduleAndConfigSeparator <= 0) { + return; + } + String moduleName = key.substring(0, moduleAndConfigSeparator); + String providerSettingSubKey = key.substring(moduleAndConfigSeparator + 1); + ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.getModuleConfiguration(moduleName); + if (moduleConfiguration == null) { + return; + } + int providerAndConfigSeparator = providerSettingSubKey.indexOf('.'); + if (providerAndConfigSeparator <= 0) { + return; + } + String providerName = providerSettingSubKey.substring(0, providerAndConfigSeparator); + String settingKey = providerSettingSubKey.substring(providerAndConfigSeparator + 1); + if (!moduleConfiguration.has(providerName)) { + return; + } + Properties providerSettings = moduleConfiguration.getProviderConfiguration(providerName); + providerSettings.put(settingKey, value); + logger.info("The setting has been override by key: {}, value: {}, in {} provider of {} module through {}", + settingKey, value, providerName, moduleName, isSystemProperty ? "System.properties" : "System.envs"); + } } -- To stop receiving notification emails like this one, please contact wush...@apache.org.