Skip to content

Commit

Permalink
Update resolving logic of project name and polish SentinelConfig (ali…
Browse files Browse the repository at this point in the history
…baba#1437)

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 authored Apr 27, 2020
1 parent 744f3d6 commit dd5b14a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,33 @@ public final class SentinelConfig {
public static final int APP_TYPE_COMMON = 0;

private static final Map<String, String> props = new ConcurrentHashMap<>();

private static int appType = APP_TYPE_COMMON;
private static String appName = "";

public static final String APP_TYPE = "csp.sentinel.app.type";
public static final String PROJECT_NAME_PROP_KEY = "project.name";
public static final String APP_NAME_PROP_KEY = "csp.sentinel.app.name";
public static final String APP_TYPE_PROP_KEY = "csp.sentinel.app.type";
public static final String CHARSET = "csp.sentinel.charset";
public static final String SINGLE_METRIC_FILE_SIZE = "csp.sentinel.metric.file.single.size";
public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count";
public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor";
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader";

static final String DEFAULT_CHARSET = "UTF-8";
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
static final int DEFAULT_COLD_FACTOR = 3;

private static String appName = "";
public static final String APP_NAME = "project.name";
public static final String SUN_JAVA_COMMAND = "sun.java.command";
private static final String JAR_SUFFIX_LOWER = ".jar";
private static final String JAR_SUFFIX_UPPER = ".JAR";

public static final int DEFAULT_STATISTIC_MAX_RT = 4900;
public static final String DEFAULT_CHARSET = "UTF-8";
public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
public static final int DEFAULT_COLD_FACTOR = 3;
public static final int DEFAULT_STATISTIC_MAX_RT = 5000;

static {
try {
initialize();
loadProps();
resolveAppName();
resolveAppType();
RecordLog.info("[SentinelConfig] Application type resolved: " + appType);
resolveAppName();
RecordLog.info("[SentinelConfig] Application name resolved: " + appName);
} catch (Throwable ex) {
RecordLog.warn("[SentinelConfig] Failed to initialize", ex);
ex.printStackTrace();
Expand All @@ -81,7 +77,7 @@ public final class SentinelConfig {

private static void resolveAppType() {
try {
String type = getConfig(APP_TYPE);
String type = getConfig(APP_TYPE_PROP_KEY);
if (type == null) {
appType = APP_TYPE_COMMON;
return;
Expand Down Expand Up @@ -198,9 +194,7 @@ public static int coldFactor() {
}

/**
* <p>Get the max RT value that Sentinel could accept.</p>
* <p>Response time that exceeds {@code statisticMaxRt} will be recorded as this value.
* The default value is {@link #DEFAULT_STATISTIC_MAX_RT}.</p>
* <p>Get the max RT value that Sentinel could accept for system BBR strategy.</p>
*
* @return the max allowed RT value
* @since 1.4.1
Expand All @@ -221,12 +215,13 @@ public static int statisticMaxRt() {
}

/**
* method for getting application name. This class uses the flowing order to get app's name:
* Function for resolving project name. The order is elaborated below:
*
* <ol>
* <li>get {@code project.name} from System Properties, if not null, use the value as app name;</li>
* <li>get {@code project.name} from Properties file, if not null, use the value as app name;</li>
* <li>get {@code sun.java.command} from System properties, remove path, arguments and ".jar" or ".JAR"
* <li>Resolve the value from {@code CSP_SENTINEL_APP_NAME} system environment;</li>
* <li>Resolve the value from {@code csp.sentinel.app.name} system property;</li>
* <li>Resolve the value from {@code project.name} system property (for compatibility);</li>
* <li>Resolve the value from {@code sun.java.command} system property, then remove path, arguments and ".jar" or ".JAR"
* suffix, use the result as app name. Note that whitespace in file name or path is not allowed, or a
* wrong app name may be gotten, For example:
* <p>
Expand All @@ -242,32 +237,54 @@ public static int statisticMaxRt() {
* </li>
* </ol>
*/
public static void resolveAppName() {

if (props.get(APP_NAME) != null) {
appName = props.get(APP_NAME);
private static void resolveAppName() {
// Priority: system env -> csp.sentinel.app.name -> project.name -> main class (or jar) name
String envKey = toEnvKey(APP_NAME_PROP_KEY);
String n = System.getenv(envKey);
if (!StringUtil.isBlank(n)) {
appName = n;
RecordLog.info("App name resolved from system env {}: {}", envKey, appName);
return;
}
n = props.get(APP_NAME_PROP_KEY);
if (!StringUtil.isBlank(n)) {
appName = n;
RecordLog.info("App name resolved from property {}: {}", APP_NAME_PROP_KEY, appName);
return;
}
// parse sun.java.command property
String command = System.getProperty(SUN_JAVA_COMMAND);
n = props.get(PROJECT_NAME_PROP_KEY);
if (!StringUtil.isBlank(n)) {
appName = n;
RecordLog.info("App name resolved from property {}: {}", PROJECT_NAME_PROP_KEY, appName);
return;
}
// Parse sun.java.command property by default.
String command = System.getProperty("sun.java.command");
if (StringUtil.isBlank(command)) {
RecordLog.warn("Cannot resolve default appName from property sun.java.command");
return;
}
command = command.split("\\s")[0];
String separator = File.separator;
if (command.contains(separator)) {
String[] strs;
if ("\\".equals(separator)) {
// Handle separator in Windows.
strs = command.split("\\\\");
} else {
strs = command.split(separator);
}
command = strs[strs.length - 1];
}
if (command.endsWith(JAR_SUFFIX_LOWER) || command.endsWith(JAR_SUFFIX_UPPER)) {
if (command.toLowerCase().endsWith(".jar")) {
command = command.substring(0, command.length() - 4);
}
appName = command;
RecordLog.info("App name resolved from default: {}", appName);
}

private static String toEnvKey(/*@NotBlank*/ String propKey) {
return propKey.toUpperCase().replace('.', '_');
}

private SentinelConfig() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
public final class ConfigPropertyHelper {

public static void setAppNameProperty(String appName) {
System.setProperty(SentinelConfig.APP_NAME, appName);
System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, appName);
}

public static void clearAppNameProperty() {
System.clearProperty(SentinelConfig.APP_NAME);
System.clearProperty(SentinelConfig.APP_NAME_PROP_KEY);
}

public static void runWithConfig(Properties prop, String appName, Task task) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testLoadProperties() throws IOException {
out.write("\n");
out.write(buildPropertyStr(STATISTIC_MAX_RT, "6000"));
out.write("\n");
out.write(buildPropertyStr(APP_NAME, "sentinel_test"));
out.write(buildPropertyStr(PROJECT_NAME_PROP_KEY, "sentinel_test"));
out.flush();
out.close();

Expand Down

0 comments on commit dd5b14a

Please sign in to comment.