From 8afc1f3951a7b878757b39009443899ea2df9ff3 Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 10 Aug 2023 16:55:23 +0800 Subject: [PATCH] add parameter to control whether raise error or not when global channel is not enabled --- service/alerting/common/helper.go | 7 +++++-- service/alerting/elasticsearch/engine.go | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/service/alerting/common/helper.go b/service/alerting/common/helper.go index 790fce12..828f12f8 100644 --- a/service/alerting/common/helper.go +++ b/service/alerting/common/helper.go @@ -72,7 +72,7 @@ func ResolveMessage(messageTemplate string, ctx map[string]interface{}) ([]byte, return msgBuffer.Bytes(), err } -func RetrieveChannel(ch *alerting.Channel) (*alerting.Channel, error) { +func RetrieveChannel(ch *alerting.Channel, raiseChannelEnabledErr bool) (*alerting.Channel, error) { if ch == nil { return nil, fmt.Errorf("empty channel") } @@ -85,7 +85,10 @@ func RetrieveChannel(ch *alerting.Channel) (*alerting.Channel, error) { } if !refCh.Enabled { ch.Enabled = false - return ch, fmt.Errorf("global channel is not enabled") + if raiseChannelEnabledErr { + return ch, fmt.Errorf("global channel is not enabled") + } + return ch, nil } ch.Type = refCh.Type ch.Name = refCh.Name diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 6e7e3f2d..a981c7e9 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -698,7 +698,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error { if err != nil { return err } - actionResults, _ := performChannels(recoverCfg.Normal, paramsCtx) + actionResults, _ := performChannels(recoverCfg.Normal, paramsCtx, false) alertItem.ActionExecutionResults = actionResults } } @@ -819,7 +819,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error { } if alertMessage == nil || period > periodDuration { - actionResults, _ := performChannels(notifyCfg.Normal, paramsCtx) + actionResults, _ := performChannels(notifyCfg.Normal, paramsCtx, false) alertItem.ActionExecutionResults = actionResults //change and save last notification time in local kv store when action error count equals zero rule.LastNotificationTime = time.Now() @@ -849,7 +849,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error { } } if time.Now().Sub(rule.LastEscalationTime.Local()) > periodDuration { - actionResults, errCount := performChannels(notifyCfg.Escalation, paramsCtx) + actionResults, errCount := performChannels(notifyCfg.Escalation, paramsCtx, false) alertItem.ActionExecutionResults = actionResults //todo init last escalation time when create task (by last alert item is escalated) if errCount == 0 { @@ -1025,14 +1025,14 @@ func (engine *Engine) Test(rule *alerting.Rule, msgType string) ([]alerting.Acti channels = notifyCfg.Normal } if len(channels) > 0 { - actionResults, _ = performChannels(channels, paramsCtx) + actionResults, _ = performChannels(channels, paramsCtx, true) }else{ return nil, fmt.Errorf("no useable channel") } return actionResults, nil } -func performChannels(channels []alerting.Channel, ctx map[string]interface{}) ([]alerting.ActionExecutionResult, int) { +func performChannels(channels []alerting.Channel, ctx map[string]interface{}, raiseChannelEnabledErr bool) ([]alerting.ActionExecutionResult, int) { var errCount int var actionResults []alerting.ActionExecutionResult for _, channel := range channels { @@ -1041,7 +1041,7 @@ func performChannels(channels []alerting.Channel, ctx map[string]interface{}) ([ resBytes []byte messageBytes []byte ) - _, err := common.RetrieveChannel(&channel) + _, err := common.RetrieveChannel(&channel, raiseChannelEnabledErr) if err != nil { log.Error(err) errCount++