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

Support setting flush interval of the metric log via SentinelConfig property #1919

Merged
merged 3 commits into from
Dec 31, 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 @@ -55,12 +55,14 @@ public final class SentinelConfig {
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";
public static final String METRIC_FLUSH_INTERVAL = "csp.sentinel.metric.flush.interval";

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;
public static final long DEFAULT_METRIC_FLUSH_INTERVAL = 1L;

static {
try {
Expand Down Expand Up @@ -98,6 +100,7 @@ private static void initialize() {
setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
setConfig(METRIC_FLUSH_INTERVAL, String.valueOf(DEFAULT_METRIC_FLUSH_INTERVAL));
}

private static void loadProps() {
Expand Down Expand Up @@ -155,6 +158,24 @@ public static int getAppType() {
public static String charset() {
return props.get(CHARSET);
}

/**
* Get the metric log flush interval in second
* @return the metric log flush interval in second
*/
public static long metricLogFlushIntervalSec() {
String flushIntervalStr = SentinelConfig.getConfig(METRIC_FLUSH_INTERVAL);
if (flushIntervalStr == null) {
return DEFAULT_METRIC_FLUSH_INTERVAL;
}
try {
return Long.parseLong(flushIntervalStr);
} catch (Throwable throwable) {
RecordLog.warn("[SentinelConfig] Parse the metricLogFlushInterval fail, use default value: "
+ DEFAULT_METRIC_FLUSH_INTERVAL, throwable);
return DEFAULT_METRIC_FLUSH_INTERVAL;
}
}

public static long singleMetricFileSize() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.atomic.AtomicReference;

import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
Expand Down Expand Up @@ -61,9 +62,29 @@ public class FlowRuleManager {
static {
flowRules.set(Collections.<String, List<FlowRule>>emptyMap());
currentProperty.addListener(LISTENER);
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, 1, TimeUnit.SECONDS);
startMetricTimerListener();
}


/**
* <p> Start the MetricTimerListener
* <ol>
* <li>If the flushInterval more than 0,
* the timer will run with the flushInterval as the rate </li>.
* <li>If the flushInterval less than 0(include) or value is not valid,
* then means the timer will not be started </li>
* <ol></p>
*/
private static void startMetricTimerListener() {
long flushInterval = SentinelConfig.metricLogFlushIntervalSec();
if (flushInterval <= 0) {
RecordLog.info("[FlowRuleManager] The MetricTimerListener is'n started. If you want to start it, "
+ "please change the value(current: {}) of config({}) more than 0 to start it.", flushInterval,
SentinelConfig.METRIC_FLUSH_INTERVAL);
return;
}
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, flushInterval, TimeUnit.SECONDS);
}

/**
* Listen to the {@link SentinelProperty} for {@link FlowRule}s. The property is the source of {@link FlowRule}s.
* Flow rules can also be set by {@link #loadRules(List)} directly.
Expand Down