feat: persist notify cur number (#1013)

Co-authored-by: tanxiao <tanxiao@asiainfo.com>
This commit is contained in:
xtan 2022-07-05 16:42:20 +08:00 committed by GitHub
parent 2a2a96d9fc
commit a9288e376d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 99 deletions

View File

@ -377,6 +377,7 @@ CREATE TABLE `alert_cur_event` (
`notify_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom', `notify_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom',
`notify_groups` varchar(255) not null default '' comment 'split by space: 233 43', `notify_groups` varchar(255) not null default '' comment 'split by space: 233 43',
`notify_repeat_next` bigint not null default 0 comment 'next timestamp to notify, get repeat settings from rule', `notify_repeat_next` bigint not null default 0 comment 'next timestamp to notify, get repeat settings from rule',
`notify_cur_number` int not null default 0 comment '',
`target_ident` varchar(191) not null default '' comment 'target ident, also in tags', `target_ident` varchar(191) not null default '' comment 'target ident, also in tags',
`target_note` varchar(191) not null default '' comment 'target note', `target_note` varchar(191) not null default '' comment 'target note',
`trigger_time` bigint not null, `trigger_time` bigint not null,
@ -410,6 +411,7 @@ CREATE TABLE `alert_his_event` (
`notify_recovered` tinyint(1) not null comment 'whether notify when recovery', `notify_recovered` tinyint(1) not null comment 'whether notify when recovery',
`notify_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom', `notify_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom',
`notify_groups` varchar(255) not null default '' comment 'split by space: 233 43', `notify_groups` varchar(255) not null default '' comment 'split by space: 233 43',
`notify_cur_number` int not null default 0 comment '',
`target_ident` varchar(191) not null default '' comment 'target ident, also in tags', `target_ident` varchar(191) not null default '' comment 'target ident, also in tags',
`target_note` varchar(191) not null default '' comment 'target note', `target_note` varchar(191) not null default '' comment 'target note',
`trigger_time` bigint not null, `trigger_time` bigint not null,

View File

@ -228,38 +228,38 @@ ALTER TABLE chart_share ADD CONSTRAINT chart_share_pk PRIMARY KEY (id);
CREATE INDEX chart_share_create_at_idx ON chart_share (create_at); CREATE INDEX chart_share_create_at_idx ON chart_share (create_at);
CREATE TABLE alert_rule ( CREATE TABLE alert_rule (
id bigserial NOT NULL, id bigserial NOT NULL,
group_id int8 NOT NULL DEFAULT 0, group_id int8 NOT NULL DEFAULT 0,
"cluster" varchar(128) NOT NULL, "cluster" varchar(128) NOT NULL,
"name" varchar(255) NOT NULL, "name" varchar(255) NOT NULL,
note varchar(1024) NOT NULL, note varchar(1024) NOT NULL,
severity int2 NOT NULL, severity int2 NOT NULL,
disabled int2 NOT NULL, disabled int2 NOT NULL,
prom_for_duration int4 NOT NULL, prom_for_duration int4 NOT NULL,
prom_ql text NOT NULL, prom_ql text NOT NULL,
prom_eval_interval int4 NOT NULL, prom_eval_interval int4 NOT NULL,
enable_stime bpchar(5) NOT NULL DEFAULT '00:00'::bpchar, enable_stime bpchar(5) NOT NULL DEFAULT '00:00'::bpchar,
enable_etime bpchar(5) NOT NULL DEFAULT '23:59'::bpchar, enable_etime bpchar(5) NOT NULL DEFAULT '23:59'::bpchar,
enable_days_of_week varchar(32) NOT NULL DEFAULT ''::character varying, enable_days_of_week varchar(32) NOT NULL DEFAULT ''::character varying,
enable_in_bg int2 NOT NULL DEFAULT 0, enable_in_bg int2 NOT NULL DEFAULT 0,
notify_recovered int2 NOT NULL, notify_recovered int2 NOT NULL,
notify_channels varchar(255) NOT NULL DEFAULT ''::character varying, notify_channels varchar(255) NOT NULL DEFAULT ''::character varying,
notify_groups varchar(255) NOT NULL DEFAULT ''::character varying, notify_groups varchar(255) NOT NULL DEFAULT ''::character varying,
notify_repeat_step int4 NOT NULL DEFAULT 0, notify_repeat_step int4 NOT NULL DEFAULT 0,
notify_max_number int4 not null default 0, notify_max_number int4 not null default 0,
recover_duration int4 NOT NULL DEFAULT 0, recover_duration int4 NOT NULL DEFAULT 0,
callbacks varchar(255) NOT NULL DEFAULT ''::character varying, callbacks varchar(255) NOT NULL DEFAULT ''::character varying,
runbook_url varchar(255) NULL, runbook_url varchar(255) NULL,
append_tags varchar(255) NOT NULL DEFAULT ''::character varying, append_tags varchar(255) NOT NULL DEFAULT ''::character varying,
create_at int8 NOT NULL DEFAULT 0, create_at int8 NOT NULL DEFAULT 0,
create_by varchar(64) NOT NULL DEFAULT ''::character varying, create_by varchar(64) NOT NULL DEFAULT ''::character varying,
update_at int8 NOT NULL DEFAULT 0, update_at int8 NOT NULL DEFAULT 0,
update_by varchar(64) NOT NULL DEFAULT ''::character varying, update_by varchar(64) NOT NULL DEFAULT ''::character varying,
prod varchar(255) NOT NULL DEFAULT ''::character varying, prod varchar(255) NOT NULL DEFAULT ''::character varying,
algorithm varchar(255) NOT NULL DEFAULT ''::character varying, algorithm varchar(255) NOT NULL DEFAULT ''::character varying,
algo_params varchar(255) NULL, algo_params varchar(255) NULL,
delay int4 NOT NULL DEFAULT 0, delay int4 NOT NULL DEFAULT 0,
CONSTRAINT alert_rule_pk PRIMARY KEY (id) CONSTRAINT alert_rule_pk PRIMARY KEY (id)
); );
CREATE INDEX alert_rule_group_id_idx ON alert_rule USING btree (group_id); CREATE INDEX alert_rule_group_id_idx ON alert_rule USING btree (group_id);
CREATE INDEX alert_rule_update_at_idx ON alert_rule USING btree (update_at); CREATE INDEX alert_rule_update_at_idx ON alert_rule USING btree (update_at);
@ -386,32 +386,33 @@ insert into alert_aggr_view(name, rule, cate) values('By BusiGroup, Severity', '
insert into alert_aggr_view(name, rule, cate) values('By RuleName', 'field:rule_name', 0); insert into alert_aggr_view(name, rule, cate) values('By RuleName', 'field:rule_name', 0);
CREATE TABLE alert_cur_event ( CREATE TABLE alert_cur_event (
id bigserial NOT NULL, id bigserial NOT NULL,
"cluster" varchar(128) NOT NULL, "cluster" varchar(128) NOT NULL,
group_id int8 NOT NULL, group_id int8 NOT NULL,
group_name varchar(255) NOT NULL DEFAULT ''::character varying, group_name varchar(255) NOT NULL DEFAULT ''::character varying,
hash varchar(64) NOT NULL, hash varchar(64) NOT NULL,
rule_id int8 NOT NULL, rule_id int8 NOT NULL,
rule_name varchar(255) NOT NULL, rule_name varchar(255) NOT NULL,
rule_note varchar(2048) NOT NULL DEFAULT 'alert rule note'::character varying, rule_note varchar(2048) NOT NULL DEFAULT 'alert rule note'::character varying,
severity int2 NOT NULL, severity int2 NOT NULL,
prom_for_duration int4 NOT NULL, prom_for_duration int4 NOT NULL,
prom_ql varchar(8192) NOT NULL, prom_ql varchar(8192) NOT NULL,
prom_eval_interval int4 NOT NULL, prom_eval_interval int4 NOT NULL,
callbacks varchar(255) NOT NULL DEFAULT ''::character varying, callbacks varchar(255) NOT NULL DEFAULT ''::character varying,
runbook_url varchar(255) NULL, runbook_url varchar(255) NULL,
notify_recovered int2 NOT NULL, notify_recovered int2 NOT NULL,
notify_channels varchar(255) NOT NULL DEFAULT ''::character varying, notify_channels varchar(255) NOT NULL DEFAULT ''::character varying,
notify_groups varchar(255) NOT NULL DEFAULT ''::character varying, notify_groups varchar(255) NOT NULL DEFAULT ''::character varying,
notify_repeat_next int8 NOT NULL DEFAULT 0, notify_repeat_next int8 NOT NULL DEFAULT 0,
target_ident varchar(191) NOT NULL DEFAULT ''::character varying, notify_cur_number int4 not null default 0,
target_note varchar(191) NOT NULL DEFAULT ''::character varying, target_ident varchar(191) NOT NULL DEFAULT ''::character varying,
trigger_time int8 NOT NULL, target_note varchar(191) NOT NULL DEFAULT ''::character varying,
trigger_value varchar(255) NOT NULL, trigger_time int8 NOT NULL,
tags varchar(1024) NOT NULL DEFAULT ''::character varying, trigger_value varchar(255) NOT NULL,
rule_prod varchar(255) NOT NULL DEFAULT ''::character varying, tags varchar(1024) NOT NULL DEFAULT ''::character varying,
rule_algo varchar(255) NOT NULL DEFAULT ''::character varying, rule_prod varchar(255) NOT NULL DEFAULT ''::character varying,
CONSTRAINT alert_cur_event_pk PRIMARY KEY (id) rule_algo varchar(255) NOT NULL DEFAULT ''::character varying,
CONSTRAINT alert_cur_event_pk PRIMARY KEY (id)
); );
CREATE INDEX alert_cur_event_hash_idx ON alert_cur_event USING btree (hash); CREATE INDEX alert_cur_event_hash_idx ON alert_cur_event USING btree (hash);
CREATE INDEX alert_cur_event_notify_repeat_next_idx ON alert_cur_event USING btree (notify_repeat_next); CREATE INDEX alert_cur_event_notify_repeat_next_idx ON alert_cur_event USING btree (notify_repeat_next);
@ -436,34 +437,35 @@ COMMENT ON COLUMN alert_cur_event.target_note IS 'target note';
COMMENT ON COLUMN alert_cur_event.tags IS 'merge data_tags rule_tags, split by ,,'; COMMENT ON COLUMN alert_cur_event.tags IS 'merge data_tags rule_tags, split by ,,';
CREATE TABLE alert_his_event ( CREATE TABLE alert_his_event (
id bigserial NOT NULL, id bigserial NOT NULL,
is_recovered int2 NOT NULL, is_recovered int2 NOT NULL,
"cluster" varchar(128) NOT NULL, "cluster" varchar(128) NOT NULL,
group_id int8 NOT NULL, group_id int8 NOT NULL,
group_name varchar(255) NOT NULL DEFAULT ''::character varying, group_name varchar(255) NOT NULL DEFAULT ''::character varying,
hash varchar(64) NOT NULL, hash varchar(64) NOT NULL,
rule_id int8 NOT NULL, rule_id int8 NOT NULL,
rule_name varchar(255) NOT NULL, rule_name varchar(255) NOT NULL,
rule_note varchar(2048) NOT NULL DEFAULT 'alert rule note'::character varying, rule_note varchar(2048) NOT NULL DEFAULT 'alert rule note'::character varying,
severity int2 NOT NULL, severity int2 NOT NULL,
prom_for_duration int4 NOT NULL, prom_for_duration int4 NOT NULL,
prom_ql varchar(8192) NOT NULL, prom_ql varchar(8192) NOT NULL,
prom_eval_interval int4 NOT NULL, prom_eval_interval int4 NOT NULL,
callbacks varchar(255) NOT NULL DEFAULT ''::character varying, callbacks varchar(255) NOT NULL DEFAULT ''::character varying,
runbook_url varchar(255) NULL, runbook_url varchar(255) NULL,
notify_recovered int2 NOT NULL, notify_recovered int2 NOT NULL,
notify_channels varchar(255) NOT NULL DEFAULT ''::character varying, notify_channels varchar(255) NOT NULL DEFAULT ''::character varying,
notify_groups varchar(255) NOT NULL DEFAULT ''::character varying, notify_groups varchar(255) NOT NULL DEFAULT ''::character varying,
target_ident varchar(191) NOT NULL DEFAULT ''::character varying, notify_cur_number int4 not null default 0,
target_note varchar(191) NOT NULL DEFAULT ''::character varying, target_ident varchar(191) NOT NULL DEFAULT ''::character varying,
trigger_time int8 NOT NULL, target_note varchar(191) NOT NULL DEFAULT ''::character varying,
trigger_value varchar(255) NOT NULL, trigger_time int8 NOT NULL,
recover_time int8 NOT NULL DEFAULT 0, trigger_value varchar(255) NOT NULL,
last_eval_time int8 NOT NULL DEFAULT 0, recover_time int8 NOT NULL DEFAULT 0,
tags varchar(1024) NOT NULL DEFAULT ''::character varying, last_eval_time int8 NOT NULL DEFAULT 0,
rule_prod varchar(255) NOT NULL DEFAULT ''::character varying, tags varchar(1024) NOT NULL DEFAULT ''::character varying,
rule_algo varchar(255) NOT NULL DEFAULT ''::character varying, rule_prod varchar(255) NOT NULL DEFAULT ''::character varying,
CONSTRAINT alert_his_event_pk PRIMARY KEY (id) rule_algo varchar(255) NOT NULL DEFAULT ''::character varying,
CONSTRAINT alert_his_event_pk PRIMARY KEY (id)
); );
CREATE INDEX alert_his_event_hash_idx ON alert_his_event USING btree (hash); CREATE INDEX alert_his_event_hash_idx ON alert_his_event USING btree (hash);
CREATE INDEX alert_his_event_rule_id_idx ON alert_his_event USING btree (rule_id); CREATE INDEX alert_his_event_rule_id_idx ON alert_his_event USING btree (rule_id);

View File

@ -38,14 +38,14 @@ type AlertCurEvent struct {
TargetNote string `json:"target_note"` TargetNote string `json:"target_note"`
TriggerTime int64 `json:"trigger_time"` TriggerTime int64 `json:"trigger_time"`
TriggerValue string `json:"trigger_value"` TriggerValue string `json:"trigger_value"`
Tags string `json:"-"` // for db Tags string `json:"-"` // for db
TagsJSON []string `json:"tags" gorm:"-"` // for fe TagsJSON []string `json:"tags" gorm:"-"` // for fe
TagsMap map[string]string `json:"-" gorm:"-"` // for internal usage TagsMap map[string]string `json:"-" gorm:"-"` // for internal usage
IsRecovered bool `json:"is_recovered" gorm:"-"` // for notify.py IsRecovered bool `json:"is_recovered" gorm:"-"` // for notify.py
NotifyUsersObj []*User `json:"notify_users_obj" gorm:"-"` // for notify.py NotifyUsersObj []*User `json:"notify_users_obj" gorm:"-"` // for notify.py
LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py 上次计算的时间 LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py 上次计算的时间
LastSentTime int64 `json:"last_sent_time" gorm:"-"` // 上次发送时间 LastSentTime int64 `json:"last_sent_time" gorm:"-"` // 上次发送时间
NotifyCurNumber int `json:"notify_cur_number" gorm:"-"` // notify: current number NotifyCurNumber int `json:"notify_cur_number"` // notify: current number
} }
func (e *AlertCurEvent) TableName() string { func (e *AlertCurEvent) TableName() string {
@ -179,6 +179,7 @@ func (e *AlertCurEvent) ToHis() *AlertHisEvent {
Tags: e.Tags, Tags: e.Tags,
RecoverTime: recoverTime, RecoverTime: recoverTime,
LastEvalTime: e.LastEvalTime, LastEvalTime: e.LastEvalTime,
NotifyCurNumber: e.NotifyCurNumber,
} }
} }

View File

@ -38,6 +38,7 @@ type AlertHisEvent struct {
LastEvalTime int64 `json:"last_eval_time"` LastEvalTime int64 `json:"last_eval_time"`
Tags string `json:"-"` Tags string `json:"-"`
TagsJSON []string `json:"tags" gorm:"-"` TagsJSON []string `json:"tags" gorm:"-"`
NotifyCurNumber int `json:"notify_cur_number"` // notify: current number
} }
func (e *AlertHisEvent) TableName() string { func (e *AlertHisEvent) TableName() string {

View File

@ -29,9 +29,5 @@ func isNoneffective(timestamp int64, alertRule *models.AlertRule) bool {
alertRule.EnableDaysOfWeek = strings.Replace(alertRule.EnableDaysOfWeek, "7", "0", 1) alertRule.EnableDaysOfWeek = strings.Replace(alertRule.EnableDaysOfWeek, "7", "0", 1)
if !strings.Contains(alertRule.EnableDaysOfWeek, triggerWeek) { return !strings.Contains(alertRule.EnableDaysOfWeek, triggerWeek)
return true
}
return false
} }

View File

@ -365,6 +365,7 @@ func (r RuleEval) fireEvent(event *models.AlertCurEvent) {
if event.LastEvalTime > fired.LastSentTime+int64(r.rule.NotifyRepeatStep)*60 { if event.LastEvalTime > fired.LastSentTime+int64(r.rule.NotifyRepeatStep)*60 {
if r.rule.NotifyMaxNumber == 0 { if r.rule.NotifyMaxNumber == 0 {
// 最大可以发送次数如果是0表示不想限制最大发送次数一直发即可 // 最大可以发送次数如果是0表示不想限制最大发送次数一直发即可
event.NotifyCurNumber = fired.NotifyCurNumber + 1
r.pushEventToQueue(event) r.pushEventToQueue(event)
} else { } else {
// 有最大发送次数的限制,就要看已经发了几次了,是否达到了最大发送次数 // 有最大发送次数的限制,就要看已经发了几次了,是否达到了最大发送次数