From 1ed8f38833c014e16fb2b025f54d72660846d46d Mon Sep 17 00:00:00 2001 From: xtan <38320121+tanxiao1990@users.noreply.github.com> Date: Thu, 4 Aug 2022 19:29:44 +0800 Subject: [PATCH] feat: add first trigger time (#1086) Co-authored-by: tanxiao --- docker/initsql/a-n9e.sql | 2 ++ docker/initsql_for_postgres/a-n9e-for-Postgres.sql | 2 ++ src/models/alert_cur_event.go | 2 ++ src/models/alert_his_event.go | 3 ++- src/server/engine/worker.go | 3 +++ 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docker/initsql/a-n9e.sql b/docker/initsql/a-n9e.sql index 76d8a2c2..3ad36cb6 100644 --- a/docker/initsql/a-n9e.sql +++ b/docker/initsql/a-n9e.sql @@ -402,6 +402,7 @@ CREATE TABLE `alert_cur_event` ( `notify_cur_number` int not null default 0 comment '', `target_ident` varchar(191) not null default '' comment 'target ident, also in tags', `target_note` varchar(191) not null default '' comment 'target note', + `first_trigger_time` bigint, `trigger_time` bigint not null, `trigger_value` varchar(255) not null, `tags` varchar(1024) not null default '' comment 'merge data_tags rule_tags, split by ,,', @@ -436,6 +437,7 @@ CREATE TABLE `alert_his_event` ( `notify_cur_number` int not null default 0 comment '', `target_ident` varchar(191) not null default '' comment 'target ident, also in tags', `target_note` varchar(191) not null default '' comment 'target note', + `first_trigger_time` bigint, `trigger_time` bigint not null, `trigger_value` varchar(255) not null, `recover_time` bigint not null default 0, diff --git a/docker/initsql_for_postgres/a-n9e-for-Postgres.sql b/docker/initsql_for_postgres/a-n9e-for-Postgres.sql index 2f6869bb..21a3e3b4 100644 --- a/docker/initsql_for_postgres/a-n9e-for-Postgres.sql +++ b/docker/initsql_for_postgres/a-n9e-for-Postgres.sql @@ -436,6 +436,7 @@ CREATE TABLE alert_cur_event ( notify_cur_number int4 not null default 0, target_ident varchar(191) NOT NULL DEFAULT ''::character varying, target_note varchar(191) NOT NULL DEFAULT ''::character varying, + first_trigger_time int8, trigger_time int8 NOT NULL, trigger_value varchar(255) NOT NULL, tags varchar(1024) NOT NULL DEFAULT ''::character varying, @@ -487,6 +488,7 @@ CREATE TABLE alert_his_event ( notify_cur_number int4 not null default 0, target_ident varchar(191) NOT NULL DEFAULT ''::character varying, target_note varchar(191) NOT NULL DEFAULT ''::character varying, + first_trigger_time int8, trigger_time int8 NOT NULL, trigger_value varchar(255) NOT NULL, recover_time int8 NOT NULL DEFAULT 0, diff --git a/src/models/alert_cur_event.go b/src/models/alert_cur_event.go index 68fcceb3..68508cf3 100644 --- a/src/models/alert_cur_event.go +++ b/src/models/alert_cur_event.go @@ -46,6 +46,7 @@ type AlertCurEvent struct { LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py 上次计算的时间 LastSentTime int64 `json:"last_sent_time" gorm:"-"` // 上次发送时间 NotifyCurNumber int `json:"notify_cur_number"` // notify: current number + FirstTriggerTime int64 `json:"first_trigger_time"` // 连续告警的首次告警时间 } func (e *AlertCurEvent) TableName() string { @@ -180,6 +181,7 @@ func (e *AlertCurEvent) ToHis() *AlertHisEvent { RecoverTime: recoverTime, LastEvalTime: e.LastEvalTime, NotifyCurNumber: e.NotifyCurNumber, + FirstTriggerTime: e.FirstTriggerTime, } } diff --git a/src/models/alert_his_event.go b/src/models/alert_his_event.go index 7549ddb5..efd11d25 100644 --- a/src/models/alert_his_event.go +++ b/src/models/alert_his_event.go @@ -38,7 +38,8 @@ type AlertHisEvent struct { LastEvalTime int64 `json:"last_eval_time"` Tags string `json:"-"` TagsJSON []string `json:"tags" gorm:"-"` - NotifyCurNumber int `json:"notify_cur_number"` // notify: current number + NotifyCurNumber int `json:"notify_cur_number"` // notify: current number + FirstTriggerTime int64 `json:"first_trigger_time"` // 连续告警的首次告警时间 } func (e *AlertHisEvent) TableName() string { diff --git a/src/server/engine/worker.go b/src/server/engine/worker.go index 5f38b56a..faa3dd2a 100644 --- a/src/server/engine/worker.go +++ b/src/server/engine/worker.go @@ -418,6 +418,7 @@ func (r RuleEval) fireEvent(event *models.AlertCurEvent) { if r.rule.NotifyMaxNumber == 0 { // 最大可以发送次数如果是0,表示不想限制最大发送次数,一直发即可 event.NotifyCurNumber = fired.NotifyCurNumber + 1 + event.FirstTriggerTime = fired.FirstTriggerTime r.pushEventToQueue(event) } else { // 有最大发送次数的限制,就要看已经发了几次了,是否达到了最大发送次数 @@ -425,6 +426,7 @@ func (r RuleEval) fireEvent(event *models.AlertCurEvent) { return } else { event.NotifyCurNumber = fired.NotifyCurNumber + 1 + event.FirstTriggerTime = fired.FirstTriggerTime r.pushEventToQueue(event) } } @@ -432,6 +434,7 @@ func (r RuleEval) fireEvent(event *models.AlertCurEvent) { } } else { event.NotifyCurNumber = 1 + event.FirstTriggerTime = event.TriggerTime r.pushEventToQueue(event) } }