diff --git a/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java b/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java index b55f2a8c..171c9061 100644 --- a/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java +++ b/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java @@ -17,7 +17,6 @@ package com.megaease.easeagent.config; -import com.google.common.base.CaseFormat; import com.megaease.easeagent.log4j2.Logger; import com.megaease.easeagent.log4j2.LoggerFactory; import com.megaease.easeagent.plugin.utils.ImmutableMap; @@ -36,7 +35,6 @@ public class ConfigFactory { private static final String CONFIG_YAML_FILE = "agent.yaml"; public static final String AGENT_CONFIG_PATH_PROP_KEY = "easeagent.config.path"; - public static final String AGENT_CONFIG_PATH_ENV_KEY = "EASEAGENT_CONFIG_PATH"; public static final String AGENT_SERVICE = "name"; public static final String AGENT_SYSTEM = "system"; @@ -60,9 +58,9 @@ public class ConfigFactory { static { for (Map.Entry entry : AGENT_CONFIG_KEYS_TO_PROPS.entrySet()) { - // lower.hyphen -> UPPER_UNDERSCORE + // dot.case -> UPPER_UNDERSCORE AGENT_ENV_KEY_TO_PROPS.put( - CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, entry.getKey().replace('.', '-')), + ConfigPropertiesUtils.toEnvVarName(entry.getKey()), entry.getValue() ); } @@ -112,22 +110,12 @@ private ConfigFactory() { /** * Get config file path from system properties or environment variables - *

- * user special config: - * -Deaseagent.config.path=/easeagent/agent.properties || export EASEAGENT_CONFIG_PATH=/easeagent/agent.properties - * or OTEL config format - * -Dotel.javaagent.configuration-file=/easeagent/agent.properties || export OTEL_JAVAAGENT_CONFIGURATION_FILE=/easeagent/agent.properties */ public static String getConfigPath() { - // get config path from -Deaseagent.config.path=xxx - String path = System.getProperty(AGENT_CONFIG_PATH_PROP_KEY); - if (StringUtils.isEmpty(path)) { - // get config path from export EASEAGENT_CONFIG_PATH=xxx - path = SystemEnv.get(AGENT_CONFIG_PATH_ENV_KEY); - } + // get config path from -Deaseagent.config.path=/easeagent/agent.properties || export EASEAGENT_CONFIG_PATH=/easeagent/agent.properties + String path = ConfigPropertiesUtils.getString(AGENT_CONFIG_PATH_PROP_KEY); if (StringUtils.isEmpty(path)) { - // get config path from OTEL configuration // eg: -Dotel.javaagent.configuration-file=/easeagent/agent.properties || export OTEL_JAVAAGENT_CONFIGURATION_FILE=/easeagent/agent.properties path = OtelSdkConfigs.getConfigPath(); } diff --git a/config/src/main/java/com/megaease/easeagent/config/ConfigPropertiesUtils.java b/config/src/main/java/com/megaease/easeagent/config/ConfigPropertiesUtils.java new file mode 100644 index 00000000..b9b79a11 --- /dev/null +++ b/config/src/main/java/com/megaease/easeagent/config/ConfigPropertiesUtils.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022, MegaEase + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.megaease.easeagent.config; + +import com.megaease.easeagent.plugin.utils.SystemEnv; + +import javax.annotation.Nullable; +import java.util.Locale; + +/** + * Get config from system properties or environment variables. + */ +final class ConfigPropertiesUtils { + + public static boolean getBoolean(String propertyName, boolean defaultValue) { + String strValue = getString(propertyName); + return strValue == null ? defaultValue : Boolean.parseBoolean(strValue); + } + + public static int getInt(String propertyName, int defaultValue) { + String strValue = getString(propertyName); + if (strValue == null) { + return defaultValue; + } + try { + return Integer.parseInt(strValue); + } catch (NumberFormatException ignored) { + return defaultValue; + } + } + + @Nullable + public static String getString(String propertyName) { + String value = System.getProperty(propertyName); + if (value != null) { + return value; + } + return SystemEnv.get(toEnvVarName(propertyName)); + } + + /** + * dot.case -> UPPER_UNDERSCORE + */ + public static String toEnvVarName(String propertyName) { + return propertyName.toUpperCase(Locale.ROOT).replace('-', '_').replace('.', '_'); + } + + private ConfigPropertiesUtils() { + } +} diff --git a/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java b/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java index 5d5e36ec..d5b48416 100644 --- a/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java +++ b/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java @@ -1,12 +1,12 @@ /* - * Copyright (c) 2021, MegaEase + * Copyright (c) 2022, MegaEase * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,7 +17,6 @@ package com.megaease.easeagent.config; -import com.google.common.base.CaseFormat; import com.google.common.base.Splitter; import com.megaease.easeagent.plugin.utils.ImmutableMap; import com.megaease.easeagent.plugin.utils.SystemEnv; @@ -34,10 +33,9 @@ * {@see https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md#disabling-opentelemetrysdk} */ public class OtelSdkConfigs { - private static final String OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; + private static final String OTEL_RESOURCE_ATTRIBUTES_KEY = "otel.resource.attributes"; private static final String CONFIG_PATH_PROP_KEY = "otel.javaagent.configuration-file"; - private static final String CONFIG_PATH_ENV_KEY = "OTEL_JAVAAGENT_CONFIGURATION_FILE"; private static final Splitter.MapSplitter OTEL_RESOURCE_ATTRIBUTES_SPLITTER = Splitter.on(",") @@ -67,9 +65,9 @@ public class OtelSdkConfigs { } for (Map.Entry entry : OTEL_SDK_PROPS_TO_EASE_AGENT_PROPS.entrySet()) { - // lower.hyphen -> UPPER_UNDERSCORE + // dot.case -> UPPER_UNDERSCORE OTEL_SDK_ENV_VAR_TO_EASE_AGENT_PROPS.put( - CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, entry.getKey().replace('.', '-')), + ConfigPropertiesUtils.toEnvVarName(entry.getKey()), entry.getValue() ); } @@ -79,12 +77,7 @@ public class OtelSdkConfigs { * Get config path from java properties or environment variables */ static String getConfigPath() { - String path = System.getProperty(CONFIG_PATH_PROP_KEY); - if (StringUtils.isEmpty(path)) { - path = SystemEnv.get(CONFIG_PATH_ENV_KEY); - } - - return path; + return ConfigPropertiesUtils.getString(CONFIG_PATH_PROP_KEY); } @@ -96,7 +89,7 @@ static String getConfigPath() { static Map updateEnvCfg() { Map envCfg = new TreeMap<>(); - String configEnv = SystemEnv.get(OTEL_RESOURCE_ATTRIBUTES); + String configEnv = ConfigPropertiesUtils.getString(OTEL_RESOURCE_ATTRIBUTES_KEY); if (StringUtils.isNotEmpty(configEnv)) { Map map = OTEL_RESOURCE_ATTRIBUTES_SPLITTER.split(configEnv); if (!map.isEmpty()) { diff --git a/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java b/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java index 4b0eb78d..3a6f8201 100644 --- a/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java +++ b/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java @@ -1,12 +1,12 @@ /* - * Copyright (c) 2021, MegaEase + * Copyright (c) 2022, MegaEase * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS,