Skip to content

Commit

Permalink
重构报警规则,增加count,op字段 dromara#442
Browse files Browse the repository at this point in the history
  • Loading branch information
EachannChan committed Aug 3, 2024
1 parent 477d74b commit 40e8ae4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public class NotifyItem {
*/
private int threshold;

/**
* Arithmetic operator.
*/
private char op='>';

/**
* In a period window, if the actual value exceeds this value, an alarm is triggered.
*/
private int countToTrigger = 1;

/**
* Alarm interval, time unit(s)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public class DtpExecutor extends ThreadPoolExecutor
*/
private List<NotifyItem> notifyItems;

/**
* The actual value to count that decide whether to trigger an alarm.
*/
private int countForNow = 0;

/**
* Notify platform ids.
*/
Expand Down Expand Up @@ -408,4 +413,12 @@ public void setAwaitTerminationSeconds(int awaitTerminationSeconds) {
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
allowCoreThreadTimeOut(allowCoreThreadTimeOut);
}

public int getCountForNow() {
return countForNow;
}

public void setCountForNow(int countForNow) {
this.countForNow = countForNow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ private static boolean checkLiveness(ExecutorWrapper executorWrapper, NotifyItem
val executor = executorWrapper.getExecutor();
int maximumPoolSize = executor.getMaximumPoolSize();
double div = NumberUtil.div(executor.getActiveCount(), maximumPoolSize, 2) * 100;
return div >= notifyItem.getThreshold();
int countForNow=executorWrapper.getCountForNow();
if(div>=notifyItem.getThreshold()){
countForNow++;
executorWrapper.setCountForNow(countForNow);
}
return countForNow>=notifyItem.getCountToTrigger();
}

private static boolean checkCapacity(ExecutorWrapper executorWrapper, NotifyItem notifyItem) {
Expand All @@ -134,12 +139,22 @@ private static boolean checkCapacity(ExecutorWrapper executorWrapper, NotifyItem
return false;
}
double div = NumberUtil.div(executor.getQueueSize(), executor.getQueueCapacity(), 2) * 100;
return div >= notifyItem.getThreshold();
int countForNow=executorWrapper.getCountForNow();
if(div>=notifyItem.getThreshold()){
countForNow++;
executorWrapper.setCountForNow(countForNow);
}
return countForNow>=notifyItem.getCountToTrigger();
}

private static boolean checkWithAlarmInfo(ExecutorWrapper executorWrapper, NotifyItem notifyItem) {
AlarmInfo alarmInfo = AlarmCounter.getAlarmInfo(executorWrapper.getThreadPoolName(), notifyItem.getType());
return alarmInfo.getCount() >= notifyItem.getThreshold();
int countForNow=executorWrapper.getCountForNow();
if(alarmInfo.getCount() >= notifyItem.getThreshold()){
countForNow++;
executorWrapper.setCountForNow(countForNow);
}
return countForNow>=notifyItem.getCountToTrigger();
}

private static void preAlarm(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class ExecutorWrapper {
*/
private List<NotifyItem> notifyItems;

/**
* The actual value to count that decide whether to trigger an alarm.
*/
private int countForNow = 0;

/**
* Notify platform ids.
*/
Expand Down Expand Up @@ -96,6 +101,7 @@ public ExecutorWrapper(DtpExecutor executor) {
this.threadPoolAliasName = executor.getThreadPoolAliasName();
this.executor = executor;
this.notifyItems = executor.getNotifyItems();
this.countForNow=executor.getCountForNow();
this.notifyEnabled = executor.isNotifyEnabled();
this.platformIds = executor.getPlatformIds();
this.awareNames = executor.getAwareNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ protected String buildAlarmContent(NotifyPlatform platform, NotifyItemEnum notif
context.setVariable("alarmTime", DateUtil.now());
context.setVariable("trace", getTraceInfo());
context.setVariable("alarmInterval", notifyItem.getInterval());
context.setVariable("op",">");
context.setVariable("countToTrigger", notifyItem.getCountToTrigger());
context.setVariable("highlightVariables", getAlarmKeys(notifyItemEnum));
context.setVariable("ext", getExtInfo());
return ((EmailNotifier) notifier).processTemplateContent("alarm", context);
Expand All @@ -134,6 +136,8 @@ protected String buildNoticeContent(NotifyPlatform platform, TpMainFields oldFie
context.setVariable("queueType", executor.getQueueType());
context.setVariable("oldQueueCapacity", oldFields.getQueueCapacity());
context.setVariable("newQueueCapacity", executor.getQueueCapacity());
context.setVariable("op",">");
context.setVariable("countForNow",executorWrapper.getCountForNow());
context.setVariable("oldRejectType", oldFields.getRejectType());
context.setVariable("newRejectType", executor.getRejectHandlerType());
context.setVariable("notifyTime", DateUtil.now());
Expand Down
4 changes: 3 additions & 1 deletion test/test-core/src/test/resources/demo-dtp-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
enabledBanner: true # 是否开启banner打印,默认true
enabledCollect: true # 是否开启监控指标采集,默认false
collectorTypes: micrometer,logging # 监控数据采集器类型(logging | micrometer | internal_logging),默认micrometer
logPath: /home/logs # 监控日志数据路径,默认 ${user.home}/logs
logPath: /home/logs # 监控日志数据路径,默认 ${user.home}/logs-
monitorInterval: 5 # 监控时间间隔(报警判断、指标采集),默认5s
platforms: # 通知报警平台配置
- platform: wechat
Expand Down Expand Up @@ -70,6 +70,8 @@ spring:
threshold: 80 # 报警阈值
platforms: [ding,wechat] # 可选配置,不配置默认拿上层platforms配置的所以平台
interval: 120 # 报警间隔(单位:s)
countToTrigger: 3
op: >
- type: change
enabled: true
- type: liveness
Expand Down

0 comments on commit 40e8ae4

Please sign in to comment.