fix(monitor): not split resource when notifying (#22078)

This commit is contained in:
Zexi Li
2025-02-10 19:50:06 +08:00
committed by GitHub
parent 4dcf4283a5
commit b176297a30
6 changed files with 61 additions and 8 deletions
@@ -68,6 +68,7 @@
border-bottom: 1px solid #d7d7d7;
}
</style>
{{$match_tags_str := .match_tags_str}}
<body>
<h3 class="title">报警提醒</h3>
<table border="0" cellspacing="0" cellpadding="0" class="table">
@@ -110,6 +111,9 @@
<td>平台</td>
<td>指标</td>
<td>触发值</td>
{{if gt (len $match_tags_str) 1}}
<td>标签</td>
{{end}}
</tr>
</thead>
<tbody>
@@ -127,6 +131,9 @@
</td>
<td>{{ $Matche.metric }}</td>
<td>{{ $Matche.value_str }}</td>
{{if gt (len $match_tags_str) 1}}
<td>{{ index $match_tags_str $i }}</td>
{{end}}
</tr>
{{end}}
</tbody>
@@ -68,6 +68,7 @@
border-bottom: 1px solid #d7d7d7;
}
</style>
{{$match_tags_str := .match_tags_str}}
<body>
<h3 class="title">Alert</h3>
<table border="0" cellspacing="0" cellpadding="0" class="table">
@@ -110,6 +111,9 @@
<td>Brand</td>
<td>Metric</td>
<td>Trigger value</td>
{{if gt (len $match_tags_str) 1}}
<td>Tags</td>
{{end}}
</tr>
</thead>
<tbody>
@@ -127,6 +131,9 @@
</td>
<td>{{ $Matche.metric }}</td>
<td>{{ $Matche.value_str }}</td>
{{if gt (len $match_tags_str) 1}}
<td>{{ index $match_tags_str $i }}</td>
{{end}}
</tr>
{{end}}
</tbody>
+6 -4
View File
@@ -19,10 +19,12 @@ type NotificationTemplateCreateInput struct {
}
type NotificationTemplateConfig struct {
Title string `json:"title"`
Name string `json:"name"`
ResourceName string `json:"resource_name"`
Matches []*EvalMatch `json:"matches"`
Title string `json:"title"`
Name string `json:"name"`
ResourceName string `json:"resource_name"`
Matches []*EvalMatch `json:"matches"`
MatchTags []map[string]string `json:"match_tags"`
MatchTagsStr []string `json:"match_tags_str"`
// PrevAlertState AlertStateType `json:"prev_alert_state"`
// State AlertStateType `json:"state"`
NoDataFound bool `json:"no_data"`
@@ -265,7 +265,10 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage,
topic = p.event
}
msg.Topic = topic
body, _ := getContent(langSuffix, p.event, "content", p.channel, p.data)
body, err := getContent(langSuffix, p.event, "content", p.channel, p.data)
if err != nil {
log.Errorf("get content error: %s", err)
}
if len(body) == 0 {
body, _ = p.data.GetString()
}
+29 -1
View File
@@ -22,6 +22,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/util/sets"
"yunion.io/x/onecloud/pkg/apis/monitor"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -205,11 +206,13 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch
desc += "Error: " + c.Error.Error()
}
tz, _ := time.LoadLocation(options.Options.TimeZone)
return monitor.NotificationTemplateConfig{
cfg := monitor.NotificationTemplateConfig{
Title: c.GetNotificationTitle(),
Name: c.Rule.Name,
ResourceName: c.GetResourceNameOfMatches(matches),
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"),
@@ -218,6 +221,31 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch
NoDataFound: c.NoDataFound,
WebUrl: c.GetCallbackURLPrefix(),
}
// calculate match tags
diffKeySets := make(map[string]sets.String)
for i := range cfg.Matches {
m := cfg.Matches[i]
for mk, mv := range m.Tags {
if _, ok := diffKeySets[mk]; !ok {
diffKeySets[mk] = sets.NewString()
}
if sets.NewString("name", "host", "host_id", "ip", "host_id", "vm_id", "access_ip").Has(mk) {
continue
}
diffKeySets[mk].Insert(mv)
}
}
for i := range cfg.Matches {
m := cfg.Matches[i]
cfg.MatchTags[i] = make(map[string]string)
for diffKey, s := range diffKeySets {
if s.Len() > 1 {
cfg.MatchTags[i][diffKey] = m.Tags[diffKey]
}
}
cfg.MatchTagsStr[i] = jsonutils.Marshal(cfg.MatchTags[i]).String()
}
return cfg
}
func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch {
+8 -2
View File
@@ -247,6 +247,10 @@ func getLangBystr(str string) language.Tag {
func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *alerting.EvalContext, uids []string) error {
errs := []error{}
if evalCtx.Rule.State == monitor.AlertStatePending {
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 {
errs = append(errs, errors.Wrapf(err, "notify alerting matches"))
@@ -520,7 +524,7 @@ func (s *sendRobotImpl) execNotifyFunc() error {
}
func SendNotifyInfo(base *sendnotifyBase, imp Isendnotify) error {
tmpMatches := base.config.Matches
/*tmpMatches := base.config.Matches
batch := 100
for i := 0; i < len(tmpMatches); i += batch {
split := i + batch
@@ -535,5 +539,7 @@ func SendNotifyInfo(base *sendnotifyBase, imp Isendnotify) error {
}
}
return nil
return nil*/
base.config.ResourceName = base.evalCtx.GetResourceNameOfMatches(base.config.Matches)
return imp.execNotifyFunc()
}