diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java b/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java index 2bc6ef961..932477a60 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/NotifyItem.java @@ -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) */ diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java index 27b632531..d012d8d35 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java @@ -76,6 +76,11 @@ public class DtpExecutor extends ThreadPoolExecutor */ private List notifyItems; + /** + * The actual value to count that decide whether to trigger an alarm. + */ + private int countForNow = 0; + /** * Notify platform ids. */ @@ -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; + } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java index 378986196..551ec0ec9 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java +++ b/core/src/main/java/org/dromara/dynamictp/core/notifier/manager/AlarmManager.java @@ -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) { @@ -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) { diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 2b8745ff7..a07745458 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -63,6 +63,11 @@ public class ExecutorWrapper { */ private List notifyItems; + /** + * The actual value to count that decide whether to trigger an alarm. + */ + private int countForNow = 0; + /** * Notify platform ids. */ @@ -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(); diff --git a/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java b/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java index ebc6304f8..812f9bf35 100644 --- a/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java +++ b/extension/extension-notify-email/src/main/java/org/dromara/dynamictp/extension/notify/email/DtpEmailNotifier.java @@ -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); @@ -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()); diff --git a/test/test-core/src/test/resources/demo-dtp-dev.yml b/test/test-core/src/test/resources/demo-dtp-dev.yml index c0570d5bb..60e338158 100644 --- a/test/test-core/src/test/resources/demo-dtp-dev.yml +++ b/test/test-core/src/test/resources/demo-dtp-dev.yml @@ -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 @@ -70,6 +70,8 @@ spring: threshold: 80 # 报警阈值 platforms: [ding,wechat] # 可选配置,不配置默认拿上层platforms配置的所以平台 interval: 120 # 报警间隔(单位:s) + countToTrigger: 3 + op: > - type: change enabled: true - type: liveness