diff --git a/pkg/monitor/alerting/eval_context.go b/pkg/monitor/alerting/eval_context.go index 6b0e5bcaf3..5399c79291 100644 --- a/pkg/monitor/alerting/eval_context.go +++ b/pkg/monitor/alerting/eval_context.go @@ -214,13 +214,12 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch Matches: matches, MatchTags: make([]map[string]string, len(matches)), MatchTagsStr: make([]string, len(matches)), - //Matches: c.GetEvalMatches(), - StartTime: c.StartTime.In(tz).Format("2006-01-02 15:04:05"), - EndTime: c.EndTime.In(tz).Format("2006-01-02 15:04:05"), - Description: desc, - Level: c.Rule.Level, - NoDataFound: c.NoDataFound, - WebUrl: c.GetCallbackURLPrefix(), + StartTime: c.StartTime.In(tz).Format("2006-01-02 15:04:05"), + EndTime: c.EndTime.In(tz).Format("2006-01-02 15:04:05"), + Description: desc, + Level: c.Rule.Level, + NoDataFound: c.NoDataFound, + WebUrl: c.GetCallbackURLPrefix(), } // calculate match tags diffKeySets := make(map[string]sets.String) @@ -249,23 +248,14 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch return cfg } -func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch { - ret := make([]monitor.EvalMatch, 0) +func (c *EvalContext) GetEvalMatches() []*monitor.EvalMatch { + ret := make([]*monitor.EvalMatch, 0) matches := c.EvalMatches - if !c.Firing { - matches = c.AlertOkEvalMatches - } - for _, c := range matches { + for i, c := range matches { if _, ok := c.Tags[monitor.ALERT_RESOURCE_RECORD_SHIELD_KEY]; ok { continue } - ret = append(ret, monitor.EvalMatch{ - Condition: c.Condition, - Value: c.Value, - ValueStr: c.ValueStr, - Metric: c.Metric, - Tags: c.Tags, - }) + ret = append(ret, matches[i]) } return ret } diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go index d821138146..21666e4907 100644 --- a/pkg/monitor/alerting/notifier.go +++ b/pkg/monitor/alerting/notifier.go @@ -149,6 +149,8 @@ func (n *notificationService) getNeededNotifiers(nIds []string, evalCtx *EvalCon } func (n *notificationService) syncResources(evalCtx *EvalContext, shouldNotify bool) { + n.processNeedShieldEvalMatches(evalCtx, evalCtx.EvalMatches) + n.processNeedShieldEvalMatches(evalCtx, evalCtx.AlertOkEvalMatches) if shouldNotify || evalCtx.Rule.State == monitor.AlertStateAlerting { go func() { if err := n.createAlertRecordWhenNotify(evalCtx, shouldNotify); err != nil { @@ -178,7 +180,6 @@ func (n *notificationService) createAlertRecordWhenNotify(evalCtx *EvalContext, } else { matches = evalCtx.AlertOkEvalMatches } - n.dealNeedShieldEvalMatches(evalCtx, matches) recordCreateInput := monitor.AlertRecordCreateInput{ StandaloneResourceCreateInput: apis.StandaloneResourceCreateInput{ GenerateName: evalCtx.Rule.Name, @@ -217,35 +218,32 @@ func (n *notificationService) createAlertRecordWhenNotify(evalCtx *EvalContext, return nil } -func (n *notificationService) dealNeedShieldEvalMatches(evalCtx *EvalContext, match []*monitor.EvalMatch) { +func (n *notificationService) processNeedShieldEvalMatches(evalCtx *EvalContext, match []*monitor.EvalMatch) { input := monitor.AlertRecordShieldListInput{ - ResType: evalCtx.Rule.RuleDescription[0].ResType, AlertId: evalCtx.Rule.Id, } -filterMatch: for i := range match { input.ResId = monitor.GetMeasurementResourceId(match[i].Tags, input.ResType) alertRecordShields, err := models.AlertRecordShieldManager.GetRecordShields(input) if err != nil { - log.Errorf("GetRecordShields byAlertId:%s,err:%v", input.AlertId, err) - return + log.Errorf("GetRecordShields by input: %s, err: %v", jsonutils.Marshal(input), err) + continue } if len(alertRecordShields) != 0 { for _, shield := range alertRecordShields { if shield.EndTime.After(time.Now().UTC()) && shield.StartTime.Before(time.Now().UTC()) { match[i].Tags[monitor.ALERT_RESOURCE_RECORD_SHIELD_KEY] = monitor.ALERT_RESOURCE_RECORD_SHIELD_VALUE - continue filterMatch + break } } } } - } func (n *notificationService) detachAlertResourceWhenNodata(evalCtx *EvalContext) { errs := models.CommonAlertManager.DetachAlertResourceByAlertId(evalCtx.Ctx, evalCtx.UserCred, evalCtx.Rule.Id) if len(errs) != 0 { - log.Errorf("detachAlertResourceWhenNodata err:%#v", errors.NewAggregate(errs)) + log.Errorf("detachAlertResourceWhenNodata err: %v", errors.NewAggregate(errs).Error()) } } diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go index 8176a7e308..1f2bc70f6b 100644 --- a/pkg/monitor/alerting/notifiers/onecloud.go +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -320,8 +320,8 @@ func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *al log.Warningf("skip notify rule because state is pending: %s", jsonutils.Marshal(evalCtx.Rule)) return nil } - if len(evalCtx.EvalMatches) > 0 { - if err := oc.notifyMatchesByContextLang(ctx, evalCtx, evalCtx.EvalMatches, uids, false); err != nil { + if len(evalCtx.GetEvalMatches()) > 0 { + if err := oc.notifyMatchesByContextLang(ctx, evalCtx, evalCtx.GetEvalMatches(), uids, false); err != nil { errs = append(errs, errors.Wrapf(err, "notify alerting matches")) } }