1- package process
1+ package consumer
22
33import (
44 "fmt"
55 "slices"
66 "strings"
77 "time"
8+ "watchAlert/alert/mute"
89 "watchAlert/internal/ctx"
910 "watchAlert/internal/models"
1011 "watchAlert/pkg/sender"
@@ -15,8 +16,8 @@ import (
1516 "golang.org/x/sync/errgroup"
1617)
1718
18- // HandleAlert 处理告警逻辑
19- func HandleAlert (ctx * ctx.Context , processType string , faultCenter models.FaultCenter , noticeId string , alerts []* models.AlertCurEvent ) error {
19+ // handleAlert 处理告警逻辑
20+ func handleAlert (ctx * ctx.Context , processType string , faultCenter models.FaultCenter , noticeId string , alerts []* models.AlertCurEvent ) error {
2021 curTime := time .Now ().Unix ()
2122 g := new (errgroup.Group )
2223
@@ -50,6 +51,10 @@ func HandleAlert(ctx *ctx.Context, processType string, faultCenter models.FaultC
5051 // 获取当前事件等级对应的路由配置
5152 routes := getNoticeRoutes (noticeData , severity )
5253 for _ , event := range events {
54+ if event .Fingerprint == "" {
55+ continue
56+ }
57+
5358 if processType == "alarm" && ! event .IsRecovered {
5459 event .LastSendTime = curTime
5560 ctx .Redis .Alert ().PushAlertEvent (event )
@@ -59,9 +64,20 @@ func HandleAlert(ctx *ctx.Context, processType string, faultCenter models.FaultC
5964 logc .Infof (ctx .Ctx , "没用匹配的通知策略, 告警事件名称: %s, 通知对象名称: %s" , event .RuleName , noticeData .Name )
6065 }
6166
67+ if mute .IsMuted (mute.MuteParams {
68+ EffectiveTime : event .EffectiveTime ,
69+ IsRecovered : event .IsRecovered ,
70+ TenantId : event .TenantId ,
71+ Labels : event .Labels ,
72+ FaultCenterId : event .FaultCenterId ,
73+ RecoverNotify : faultCenter .RecoverNotify ,
74+ }) {
75+ continue
76+ }
77+
6278 for _ , route := range routes {
6379 // 设置值班用户信息
64- event .DutyUser = strings .Join (GetDutyUsers (ctx , noticeData , route .NoticeType ), " " )
80+ event .DutyUser = strings .Join (getDutyUsers (ctx , noticeData , route .NoticeType ), " " )
6581
6682 // 生成告警内容
6783 content := generateAlertContent (ctx , event , noticeData , route )
@@ -136,10 +152,8 @@ func withRuleGroupByAlerts(ctx *ctx.Context, timeInt int64, alerts []*models.Ale
136152 }
137153 }
138154
139- aggregatedAlert := alerts [0 ]
140- aggregatedAlert .Annotations += fmt .Sprintf ("\n 聚合 %d 条消息,详情请前往 WatchAlert 查看\n " , len (alerts ))
141-
142- return []* models.AlertCurEvent {aggregatedAlert }
155+ event := * alerts [0 ]
156+ return []* models.AlertCurEvent {& event }
143157}
144158
145159// getNoticeData 获取 Notice 数据
@@ -198,3 +212,34 @@ func generateAlertContent(ctx *ctx.Context, alert *models.AlertCurEvent, noticeD
198212 }
199213 return template .CardContentMsg
200214}
215+
216+ func getDutyUsers (ctx * ctx.Context , noticeData models.AlertNotice , noticeType string ) []string {
217+ var us []string
218+ users , ok := ctx .DB .DutyCalendar ().GetDutyUserInfo (* noticeData .GetDutyId (), time .Now ().Format ("2006-1-2" ))
219+ if ok {
220+ switch noticeType {
221+ case "FeiShu" :
222+ for _ , user := range users {
223+ us = append (us , fmt .Sprintf ("<at id=%s></at>" , user .DutyUserId ))
224+ }
225+ return us
226+ case "DingDing" :
227+ for _ , user := range users {
228+ us = append (us , fmt .Sprintf ("@%s" , user .DutyUserId ))
229+ }
230+ return us
231+ case "Email" , "WeChat" , "WebHook" :
232+ for _ , user := range users {
233+ us = append (us , fmt .Sprintf ("@%s" , user .UserName ))
234+ }
235+ return us
236+ case "Slack" :
237+ for _ , user := range users {
238+ us = append (us , fmt .Sprintf ("<@%s>" , user .DutyUserId ))
239+ }
240+ return us
241+ }
242+ }
243+
244+ return []string {"暂无" }
245+ }
0 commit comments