Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resolving logic of project name and polish SentinelConfig #1437

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines -66 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hhh, so now the max RT is 5000, and not the classic value 4900?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic 4900 often makes users confusing, so I've changed it to 5000. The attribute might be deprecated in future versions.


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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log info doesn't has prefix [SentinelConfig] like Line#71, and Line#71 move to inside resolveAppType method looks better. It's okay I think and we can do optimize them in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log info doesn't has prefix [SentinelConfig] like Line#71

Actually prefix is not strictly required if the log content is concrete enough :)

Line#71 move to inside resolveAppType method looks better. It's okay I think and we can do optimize them in the future.

Good idea. We may improve this later.

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