From 174126fa10be34f7a90d6e775714df266b4613ab Mon Sep 17 00:00:00 2001 From: liugq Date: Mon, 21 Aug 2023 18:03:23 +0800 Subject: [PATCH] fix wrong channel name --- plugin/api/alerting/message.go | 14 ++++++++++---- service/alerting/elasticsearch/engine.go | 15 ++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/plugin/api/alerting/message.go b/plugin/api/alerting/message.go index c583b645..3a8d790a 100644 --- a/plugin/api/alerting/message.go +++ b/plugin/api/alerting/message.go @@ -470,7 +470,7 @@ func getMessageNotificationStats(msg *alerting.AlertMessage )(util.MapStr, error }, }, "_source": util.MapStr{ - "includes": []string{"created", "action_execution_results.channel_name"}, + "includes": []string{"created", "action_execution_results.channel_name", "action_execution_results.channel_type"}, }, "size": 1, }, @@ -493,7 +493,7 @@ func getMessageNotificationStats(msg *alerting.AlertMessage )(util.MapStr, error }, }, "_source": util.MapStr{ - "includes": []string{"created", "escalation_action_results.channel_name"}, + "includes": []string{"created", "escalation_action_results.channel_name", "escalation_action_results.channel_type"}, }, "size": 1, }, @@ -518,7 +518,7 @@ func getMessageNotificationStats(msg *alerting.AlertMessage )(util.MapStr, error }, }, "_source": util.MapStr{ - "includes": []string{"created", "recover_action_results.channel_name"}, + "includes": []string{"created", "recover_action_results.channel_name", "recover_action_results.channel_type"}, }, "size": 1, }, @@ -575,7 +575,13 @@ func extractStatsFromRaw(searchRawRes []byte, grpKey string, actionKey string) [ statsItem := util.MapStr{} statsItem["channel_type"], _ = jsonparser.GetString(value, "key") statsItem["count"], _ = jsonparser.GetInt(value, "doc_count") - statsItem["channel_name"], _ = jsonparser.GetString(value, "top", "hits","hits", "[0]", "_source",actionKey, "[0]", "channel_name") + jsonparser.ArrayEach(value, func(v []byte, dataType jsonparser.ValueType, offset int, err error) { + ck, _ := jsonparser.GetString(v, "channel_type") + cn, _ := jsonparser.GetString(v, "channel_name") + if ck == statsItem["channel_type"] { + statsItem["channel_name"] = cn + } + }, "top", "hits","hits", "[0]", "_source",actionKey) statsItem["last_time"], _ = jsonparser.GetString(value, "top", "hits","hits", "[0]", "_source","created") stats = append(stats, statsItem) }, "aggregations", grpKey, "buckets") diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 038baba0..7a0c1239 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -822,6 +822,9 @@ func (engine *Engine) Do(rule *alerting.Rule) error { if alertMessage == nil || period > periodDuration { actionResults, _ := performChannels(notifyCfg.Normal, paramsCtx, false) + if rule.ID == "builtin-calaqnh7h710dpnp2bm8" { + log.Info(actionResults) + } alertItem.ActionExecutionResults = actionResults //change and save last notification time in local kv store when action error count equals zero rule.LastNotificationTime = time.Now() @@ -851,15 +854,13 @@ func (engine *Engine) Do(rule *alerting.Rule) error { } } if time.Now().Sub(rule.LastEscalationTime.Local()) > periodDuration { - actionResults, errCount := performChannels(notifyCfg.Escalation, paramsCtx, false) + actionResults, _ := performChannels(notifyCfg.Escalation, paramsCtx, false) alertItem.EscalationActionResults = actionResults //todo init last escalation time when create task (by last alert item is escalated) - if errCount == 0 { - rule.LastEscalationTime = time.Now() - alertItem.IsEscalated = true - strTime := rule.LastEscalationTime.UTC().Format(time.RFC3339) - kv.AddValue(alerting2.KVLastEscalationTime, []byte(rule.ID), []byte(strTime)) - } + rule.LastEscalationTime = time.Now() + alertItem.IsEscalated = true + strTime := rule.LastEscalationTime.UTC().Format(time.RFC3339) + kv.AddValue(alerting2.KVLastEscalationTime, []byte(rule.ID), []byte(strTime)) } }