Support to use empty string to override existing config. (#4635)

This commit is contained in:
吴晟 Wu Sheng 2020-04-11 13:05:57 +08:00 committed by GitHub
parent 44cae97c25
commit 31385159c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 60 deletions

View File

@ -31,7 +31,11 @@ import java.util.Set;
*/ */
public enum PropertyPlaceholderHelper { public enum PropertyPlaceholderHelper {
INSTANCE(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); INSTANCE(
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR,
true
);
private final String placeholderPrefix; private final String placeholderPrefix;
@ -54,7 +58,7 @@ public enum PropertyPlaceholderHelper {
* true}) or cause an exception ({@code false}) * true}) or cause an exception ({@code false})
*/ */
PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix, String valueSeparator, PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix, String valueSeparator,
boolean ignoreUnresolvablePlaceholders) { boolean ignoreUnresolvablePlaceholders) {
if (StringUtil.isEmpty(placeholderPrefix) || StringUtil.isEmpty(placeholderSuffix)) { if (StringUtil.isEmpty(placeholderPrefix) || StringUtil.isEmpty(placeholderSuffix)) {
throw new UnsupportedOperationException("'placeholderPrefix or placeholderSuffix' must not be null"); throw new UnsupportedOperationException("'placeholderPrefix or placeholderSuffix' must not be null");
} }
@ -89,17 +93,17 @@ public enum PropertyPlaceholderHelper {
return replacePlaceholders(value, new PlaceholderResolver() { return replacePlaceholders(value, new PlaceholderResolver() {
@Override @Override
public String resolvePlaceholder(String placeholderName) { public String resolvePlaceholder(String placeholderName) {
return PropertyPlaceholderHelper.this.getConfigValue(placeholderName, properties); return getConfigValue(placeholderName, properties);
} }
}); });
} }
private String getConfigValue(String key, final Properties properties) { private String getConfigValue(String key, final Properties properties) {
String value = System.getProperty(key); String value = System.getProperty(key);
if (StringUtil.isEmpty(value)) { if (value == null) {
value = System.getenv(key); value = System.getenv(key);
} }
if (StringUtil.isEmpty(value)) { if (value == null) {
value = properties.getProperty(key); value = properties.getProperty(key);
} }
return value; return value;
@ -118,7 +122,7 @@ public enum PropertyPlaceholderHelper {
} }
protected String parseStringValue(String value, PlaceholderResolver placeholderResolver, protected String parseStringValue(String value, PlaceholderResolver placeholderResolver,
Set<String> visitedPlaceholders) { Set<String> visitedPlaceholders) {
StringBuilder result = new StringBuilder(value); StringBuilder result = new StringBuilder(value);
@ -129,7 +133,8 @@ public enum PropertyPlaceholderHelper {
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex); String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String originalPlaceholder = placeholder; String originalPlaceholder = placeholder;
if (!visitedPlaceholders.add(originalPlaceholder)) { if (!visitedPlaceholders.add(originalPlaceholder)) {
throw new IllegalArgumentException("Circular placeholder reference '" + originalPlaceholder + "' in property definitions"); throw new IllegalArgumentException(
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
} }
// Recursive invocation, parsing placeholders contained in the placeholder key. // Recursive invocation, parsing placeholders contained in the placeholder key.
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders); placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
@ -156,7 +161,8 @@ public enum PropertyPlaceholderHelper {
// Proceed with unprocessed value. // Proceed with unprocessed value.
startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length()); startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
} else { } else {
throw new IllegalArgumentException("Could not resolve placeholder '" + placeholder + "'" + " in value \"" + value + "\""); throw new IllegalArgumentException(
"Could not resolve placeholder '" + placeholder + "'" + " in value \"" + value + "\"");
} }
visitedPlaceholders.remove(originalPlaceholder); visitedPlaceholders.remove(originalPlaceholder);
} else { } else {

View File

@ -24,13 +24,12 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper; import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
import org.apache.skywalking.oap.server.library.module.ApplicationConfiguration; import org.apache.skywalking.oap.server.library.module.ApplicationConfiguration;
import org.apache.skywalking.oap.server.library.module.ProviderNotFoundException; import org.apache.skywalking.oap.server.library.module.ProviderNotFoundException;
import org.apache.skywalking.oap.server.library.util.CollectionUtils; import org.apache.skywalking.oap.server.library.util.CollectionUtils;
import org.apache.skywalking.oap.server.library.util.ResourceUtils; import org.apache.skywalking.oap.server.library.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
/** /**
@ -39,10 +38,8 @@ import org.yaml.snakeyaml.Yaml;
* <p> * <p>
* At last, override setting by system.properties and system.envs if the key matches moduleName.provideName.settingKey. * At last, override setting by system.properties and system.envs if the key matches moduleName.provideName.settingKey.
*/ */
@Slf4j
public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfiguration> { public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfiguration> {
private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigLoader.class);
private static final String DISABLE_SELECTOR = "-"; private static final String DISABLE_SELECTOR = "-";
private static final String SELECTOR = "selector"; private static final String SELECTOR = "selector";
@ -65,10 +62,14 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
selectConfig(moduleConfig); selectConfig(moduleConfig);
moduleConfig.forEach((moduleName, providerConfig) -> { moduleConfig.forEach((moduleName, providerConfig) -> {
if (providerConfig.size() > 0) { if (providerConfig.size() > 0) {
logger.info("Get a module define from application.yml, module name: {}", moduleName); log.info("Get a module define from application.yml, module name: {}", moduleName);
ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.addModule(moduleName); ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.addModule(
moduleName);
providerConfig.forEach((providerName, config) -> { providerConfig.forEach((providerName, config) -> {
logger.info("Get a provider define belong to {} module, provider name: {}", moduleName, providerName); log.info(
"Get a provider define belong to {} module, provider name: {}", moduleName,
providerName
);
final Map<String, ?> propertiesConfig = (Map<String, ?>) config; final Map<String, ?> propertiesConfig = (Map<String, ?>) config;
final Properties properties = new Properties(); final Properties properties = new Properties();
if (propertiesConfig != null) { if (propertiesConfig != null) {
@ -89,7 +90,10 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
moduleConfiguration.addProviderConfiguration(providerName, properties); moduleConfiguration.addProviderConfiguration(providerName, properties);
}); });
} else { } else {
logger.warn("Get a module define from application.yml, but no provider define, use default, module name: {}", moduleName); log.warn(
"Get a module define from application.yml, but no provider define, use default, module name: {}",
moduleName
);
} }
}); });
} }
@ -99,11 +103,26 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
} }
private void replacePropertyAndLog(final Object propertyName, final Object propertyValue, final Properties target, private void replacePropertyAndLog(final Object propertyName, final Object propertyValue, final Properties target,
final Object providerName) { final Object providerName) {
final Object replaceValue = yaml.load(PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(propertyValue + "", target)); final String valueString = PropertyPlaceholderHelper.INSTANCE
if (replaceValue != null) { .replacePlaceholders(propertyValue + "", target);
target.replace(propertyName, replaceValue); if (valueString != null) {
logger.info("The property with key: {}, value: {}, in {} provider", propertyName, replaceValue.toString(), providerName); if (valueString.trim().length() == 0) {
target.replace(propertyName, valueString);
log.info("Provider={} config={} has been set as an empty string", providerName, propertyName);
} else {
// Use YAML to do data type conversion.
final Object replaceValue = yaml.load(valueString);
if (replaceValue != null) {
target.replace(propertyName, replaceValue);
log.info(
"Provider={} config={} has been set as {}",
providerName,
propertyName,
replaceValue.toString()
);
}
}
} }
} }
@ -148,7 +167,7 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
final boolean shouldBeRemoved = modulesWithoutProvider.contains(module); final boolean shouldBeRemoved = modulesWithoutProvider.contains(module);
if (shouldBeRemoved) { if (shouldBeRemoved) {
logger.info("Remove module {} without any provider", module); log.info("Remove module {} without any provider", module);
} }
return shouldBeRemoved; return shouldBeRemoved;
@ -162,7 +181,8 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
} }
String moduleName = key.substring(0, moduleAndConfigSeparator); String moduleName = key.substring(0, moduleAndConfigSeparator);
String providerSettingSubKey = key.substring(moduleAndConfigSeparator + 1); String providerSettingSubKey = key.substring(moduleAndConfigSeparator + 1);
ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.getModuleConfiguration(moduleName); ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.getModuleConfiguration(
moduleName);
if (moduleConfiguration == null) { if (moduleConfiguration == null) {
return; return;
} }
@ -193,6 +213,9 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
return; return;
} }
logger.info("The setting has been override by key: {}, value: {}, in {} provider of {} module through {}", settingKey, value, providerName, moduleName, "System.properties"); log.info(
"The setting has been override by key: {}, value: {}, in {} provider of {} module through {}", settingKey,
value, providerName, moduleName, "System.properties"
);
} }
} }

View File

@ -92,8 +92,8 @@ storage:
nameSpace: ${SW_NAMESPACE:""} nameSpace: ${SW_NAMESPACE:""}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200} clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200}
protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"} protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:""} trustStorePath: ${SW_STORAGE_ES_SSL_JKS_PATH:""}
trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""} trustStorePass: ${SW_STORAGE_ES_SSL_JKS_PASS:""}
user: ${SW_ES_USER:""} user: ${SW_ES_USER:""}
password: ${SW_ES_PASSWORD:""} password: ${SW_ES_PASSWORD:""}
secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool. secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
@ -113,8 +113,8 @@ storage:
nameSpace: ${SW_NAMESPACE:""} nameSpace: ${SW_NAMESPACE:""}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200} clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200}
protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"} protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:"../es_keystore.jks"} trustStorePath: ${SW_STORAGE_ES_SSL_JKS_PATH:""}
trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""} trustStorePass: ${SW_STORAGE_ES_SSL_JKS_PASS:""}
dayStep: ${SW_STORAGE_DAY_STEP:1} # Represent the number of days in the one minute/hour/day index. dayStep: ${SW_STORAGE_DAY_STEP:1} # Represent the number of days in the one minute/hour/day index.
user: ${SW_ES_USER:""} user: ${SW_ES_USER:""}
password: ${SW_ES_PASSWORD:""} password: ${SW_ES_PASSWORD:""}

View File

@ -89,37 +89,6 @@ public class StorageModuleElasticsearchConfig extends ModuleConfig {
@Setter @Setter
private int profileTaskQueryMaxSize = 200; private int profileTaskQueryMaxSize = 200;
@Setter @Setter
private int recordDataTTL = 7;
@Setter
private int minuteMetricsDataTTL = 2;
@Setter
private int hourMetricsDataTTL = 2;
@Setter
private int dayMetricsDataTTL = 2;
private int otherMetricsDataTTL = 0;
@Setter
private int monthMetricsDataTTL = 18;
@Setter
private String advanced; private String advanced;
public int getMinuteMetricsDataTTL() {
if (otherMetricsDataTTL > 0) {
return otherMetricsDataTTL;
}
return minuteMetricsDataTTL;
}
public int getHourMetricsDataTTL() {
if (otherMetricsDataTTL > 0) {
return otherMetricsDataTTL;
}
return hourMetricsDataTTL;
}
public int getDayMetricsDataTTL() {
if (otherMetricsDataTTL > 0) {
return otherMetricsDataTTL;
}
return dayMetricsDataTTL;
}
} }