Skip to content

Commit

Permalink
refactor: Optimizing code for get config from system properties or en…
Browse files Browse the repository at this point in the history
…vironment variables.
  • Loading branch information
lanxenet committed Sep 13, 2023
1 parent 2e94aad commit abd4bd7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -60,9 +58,9 @@ public class ConfigFactory {

static {
for (Map.Entry<String, String> 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()
);
}
Expand Down Expand Up @@ -112,22 +110,12 @@ private ConfigFactory() {

/**
* Get config file path from system properties or environment variables
* <p>
* 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -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(",")
Expand Down Expand Up @@ -67,9 +65,9 @@ public class OtelSdkConfigs {
}

for (Map.Entry<String, String> 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()
);
}
Expand All @@ -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);
}


Expand All @@ -96,7 +89,7 @@ static String getConfigPath() {
static Map<String, String> updateEnvCfg() {
Map<String, String> envCfg = new TreeMap<>();

String configEnv = SystemEnv.get(OTEL_RESOURCE_ATTRIBUTES);
String configEnv = ConfigPropertiesUtils.getString(OTEL_RESOURCE_ATTRIBUTES_KEY);
if (StringUtils.isNotEmpty(configEnv)) {
Map<String, String> map = OTEL_RESOURCE_ATTRIBUTES_SPLITTER.split(configEnv);
if (!map.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit abd4bd7

Please sign in to comment.