Skip to content

Commit

Permalink
Add appType property field in SentinelConfig
Browse files Browse the repository at this point in the history
- The `appType` can be retrieved from `csp.sentinel.app.type` field. It's useful to identify the service type (e.g. API gateway).

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Apr 23, 2019
1 parent 845b258 commit bb4fde5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public final class Constants {
* The global switch for Sentinel.
*/
public static volatile boolean ON = true;

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@
import com.alibaba.csp.sentinel.util.AssertUtil;

/**
* The universal config of Courier. The config is retrieved from
* {@code ${user.home}/logs/csp/${appName}.properties} by default.
* The universal local config center of Sentinel. The config is retrieved from command line arguments
* and {@code ${user.home}/logs/csp/${appName}.properties} file by default.
*
* @author leyou
* @author Eric Zhao
*/
public class SentinelConfig {

private static final Map<String, String> props = new ConcurrentHashMap<String, String>();
/**
* The default application type.
*
* @since 1.6.0
*/
public static final int APP_TYPE_COMMON = 0;

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

public static final String APP_TYPE = "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";
Expand All @@ -49,8 +59,32 @@ public class SentinelConfig {
static final int DEFAULT_STATISTIC_MAX_RT = 4900;

static {
initialize();
loadProps();
try {
initialize();
loadProps();

resolveAppType();
RecordLog.info("[SentinelConfig] Application type resolved: " + appType);
} catch (Throwable ex) {
RecordLog.warn("[SentinelConfig] Failed to initialize", ex);
ex.printStackTrace();
}
}

private static void resolveAppType() {
try {
String type = getConfig(APP_TYPE);
if (type == null) {
appType = APP_TYPE_COMMON;
return;
}
appType = Integer.parseInt(type);
if (appType < 0) {
appType = APP_TYPE_COMMON;
}
} catch (Exception ex) {
appType = APP_TYPE_COMMON;
}
}

private static void initialize() {
Expand Down Expand Up @@ -135,6 +169,16 @@ public static String getAppName() {
return AppNameUtil.getAppName();
}

/**
* Get application type.
*
* @return application type, common (0) by default
* @since 1.6.0
*/
public static int getAppType() {
return appType;
}

public static String charset() {
return props.get(CHARSET);
}
Expand Down Expand Up @@ -162,7 +206,8 @@ public static int totalMetricFileCount() {
public static int coldFactor() {
try {
int coldFactor = Integer.parseInt(props.get(COLD_FACTOR));
if (coldFactor <= 1) {// check the cold factor larger than 1
// check the cold factor larger than 1
if (coldFactor <= 1) {
coldFactor = DEFAULT_COLD_FACTOR;
RecordLog.warn("cold factor=" + coldFactor + ", should be larger than 1, use default value: "
+ DEFAULT_COLD_FACTOR);
Expand Down

0 comments on commit bb4fde5

Please sign in to comment.