Skip to content

Commit 7a35b7d

Browse files
author
Cairry
committed
🚀 Optimized alarm route logic
1 parent 0ac4b4e commit 7a35b7d

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

alert/consumer/consumer.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,7 @@ func (ag *AlertGroups) getNoticeId(alert *models.AlertCurEvent, faultCenter mode
104104
labels := alert.Labels
105105

106106
for _, route := range faultCenter.NoticeRoutes {
107-
val, ok := labels[route.Key].(string)
108-
if !ok {
109-
continue
110-
}
111-
112-
if regexp.MustCompile(route.Value).MatchString(val) {
107+
if evalCondition(labels, route.NoticeLabels) {
113108
return route.NoticeIds
114109
}
115110
}
@@ -383,3 +378,37 @@ func (c *Consume) StopAllConsumers() {
383378

384379
logc.Infof(c.ctx.Ctx, "所有故障中心消费者已停止")
385380
}
381+
382+
func evalCondition(metrics map[string]interface{}, muteLabels []models.NoticeLabels) bool {
383+
for _, muteLabel := range muteLabels {
384+
value, exists := metrics[muteLabel.Key]
385+
if !exists {
386+
return false
387+
}
388+
389+
val, ok := value.(string)
390+
if !ok {
391+
continue
392+
}
393+
394+
var matched bool
395+
switch muteLabel.Operator {
396+
case "==", "=":
397+
matched = (val == muteLabel.Value)
398+
case "!=":
399+
matched = (val != muteLabel.Value)
400+
case "=~":
401+
matched = regexp.MustCompile(muteLabel.Value).MatchString(val)
402+
case "!~":
403+
matched = !regexp.MustCompile(muteLabel.Value).MatchString(val)
404+
default:
405+
matched = false
406+
}
407+
408+
if !matched {
409+
return false
410+
}
411+
}
412+
413+
return true
414+
}

internal/models/fault_center.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ type UpgradeStrategy struct {
4949
}
5050

5151
type NoticeRoute struct {
52-
Key string `json:"key"`
53-
Value string `json:"value"`
54-
NoticeIds []string `json:"noticeIds" gorm:"column:noticeIds;serializer:json"`
52+
NoticeLabels []NoticeLabels `json:"labels" gorm:"column:labels;serializer:json"`
53+
NoticeIds []string `json:"noticeIds" gorm:"column:noticeIds;serializer:json"`
54+
}
55+
56+
type NoticeLabels struct {
57+
Key string `json:"key"`
58+
Value string `json:"value"`
59+
Operator string `json:"operator"`
5560
}
5661

5762
func (u *UpgradeStrategy) GetEnabled() bool {

0 commit comments

Comments
 (0)