Skip to content

Commit 073c52a

Browse files
committed
feat: Add a reference to the notification rule in the permalink
When notifications are being received but there's no record in the send history, this will help us identify the notification rule that sent this notificaiton.
1 parent 7227582 commit 073c52a

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

notification/cel.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notification
33
import (
44
"errors"
55
"fmt"
6+
"net/url"
67
"slices"
78
"time"
89

@@ -61,6 +62,28 @@ func (t *celVariables) SetSilenceURL(frontendURL string) {
6162
}
6263
}
6364

65+
func (t celVariables) WithNotificationRef(notificationID string) celVariables {
66+
t.Permalink = appendRefNotification(t.Permalink, notificationID)
67+
return t
68+
}
69+
70+
func appendRefNotification(rawURL string, notificationID string) string {
71+
if rawURL == "" || notificationID == "" {
72+
return rawURL
73+
}
74+
75+
u, err := url.Parse(rawURL)
76+
if err != nil {
77+
return rawURL
78+
}
79+
80+
query := u.Query()
81+
query.Set("refNotification", notificationID)
82+
u.RawQuery = query.Encode()
83+
84+
return u.String()
85+
}
86+
6487
type ResourceHealthRow struct {
6588
Health models.Health
6689
Status string

notification/events.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ func (t *notificationHandler) addNotificationEvent(ctx context.Context, event mo
169169
}
170170

171171
for _, id := range notificationIDs {
172-
if err := addNotificationEvent(ctx, id, celEnv, event, matchingSilences); err != nil {
172+
notificationEnv := celEnv.WithNotificationRef(id)
173+
if err := addNotificationEvent(ctx, id, &notificationEnv, event, matchingSilences); err != nil {
173174
return ctx.Oops().Wrapf(err, "failed to add notification.send event for event=%s notification=%s", event.Name, id)
174175
}
175176
}
@@ -780,6 +781,8 @@ func _sendNotification(ctx *Context, payload NotificationEventPayload) error {
780781
if err != nil {
781782
return fmt.Errorf("failed to get cel env: %w", err)
782783
}
784+
notificationEnv := celEnv.WithNotificationRef(payload.NotificationID.String())
785+
celEnv = &notificationEnv
783786

784787
if payload.GroupID != nil {
785788
celEnv.GroupedResources, err = db.GetGroupedResources(ctx.Context, *payload.GroupID, payload.ResourceID.String())

0 commit comments

Comments
 (0)