Skip to content

Commit

Permalink
feat: add receiver for notification (#279)
Browse files Browse the repository at this point in the history
* feat: add receiver for notification

Signed-off-by: Gentleelephant <[email protected]>

* feat(notify): add receiver type label to alerts

- Add ReceiverType constant in constants.go
- Update notify.go to set ReceiverType label for alerts

Signed-off-by: Gentleelephant <[email protected]>

---------

Signed-off-by: Gentleelephant <[email protected]>
  • Loading branch information
Gentleelephant authored Dec 23, 2024
1 parent b7c6a5d commit 0c9b1fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
AlertSummaryCN = "summaryCn"

ReceiverName = "receiver"
ReceiverType = "receiver-type"

Verify = "verify"
Notification = "notification"
Expand Down
8 changes: 5 additions & 3 deletions pkg/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func (s *historyStage) Exec(ctx context.Context, l log.Logger, data interface{})

_ = level.Debug(l).Log("msg", "Start history stage", "seq", ctx.Value("seq"))

input := data.(map[string]*template.Alert)
input := data.(map[string][]*template.Alert)
d := &template.Data{}
for _, alert := range input {
if alert.NotifySuccessful {
d.Alerts = append(d.Alerts, alert)
for _, t := range alert {
if t.NotifySuccessful {
d.Alerts = append(d.Alerts, t)
}
}
}

Expand Down
22 changes: 16 additions & 6 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
_ = level.Debug(l).Log("msg", "Start notify stage", "seq", ctx.Value("seq"))

input := data.(map[internal.Receiver][]*template.Data)
alertMap := make(map[string]*template.Alert)
for _, dataList := range input {
for _, d := range dataList {
alertMap := make(map[string][]*template.Alert)
for r, dataList := range input {
receiver := r
ds := convertor.DeepClone(dataList)
s.addExtensionLabels(receiver, ds)
for _, d := range ds {
for _, alert := range d.Alerts {
alertMap[alert.ID] = alert
alertMap[alert.ID] = append(alertMap[alert.ID], alert)
}
}
}
Expand All @@ -91,7 +94,9 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})

for _, alert := range alerts {
if a := alertMap[alert.ID]; a != nil {
a.NotifySuccessful = true
for _, t := range a {
t.NotifySuccessful = true
}
}
}
}
Expand Down Expand Up @@ -125,7 +130,12 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
func (s *notifyStage) addExtensionLabels(receiver internal.Receiver, data []*template.Data) {
for _, d := range data {
for _, alert := range d.Alerts {
alert.Labels[constants.ReceiverName] = receiver.GetName()
if alert.Labels[constants.ReceiverName] == "" {
alert.Labels[constants.ReceiverName] = receiver.GetName()
}
if alert.Labels[constants.ReceiverType] == "" {
alert.Labels[constants.ReceiverType] = receiver.GetType()
}
}
}
}

0 comments on commit 0c9b1fe

Please sign in to comment.