Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add service name/id field to webhook alerts #3199

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions engine/sendmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (p *Engine) sendMessage(ctx context.Context, msg *message.Message) (*notifi
Count: count,
}
case notification.MessageTypeAlert:
name, _, err := p.a.ServiceInfo(ctx, msg.ServiceID)
if err != nil {
return nil, errors.Wrap(err, "lookup service info")
}
a, err := p.a.FindOne(ctx, msg.AlertID)
if err != nil {
return nil, errors.Wrap(err, "lookup alert")
Expand All @@ -68,11 +72,13 @@ func (p *Engine) sendMessage(ctx context.Context, msg *message.Message) (*notifi
stat = nil
}
notifMsg = notification.Alert{
Dest: msg.Dest,
AlertID: msg.AlertID,
Summary: a.Summary,
Details: a.Details,
CallbackID: msg.ID,
Dest: msg.Dest,
AlertID: msg.AlertID,
Summary: a.Summary,
Details: a.Details,
CallbackID: msg.ID,
ServiceID: a.ServiceID,
ServiceName: name,

OriginalStatus: stat,
}
Expand Down
12 changes: 7 additions & 5 deletions notification/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package notification

// Alert represents outgoing notifications for alerts.
type Alert struct {
Dest Dest
CallbackID string // CallbackID is the identifier used to communicate a response to the notification
AlertID int // The global alert number
Summary string
Details string
Dest Dest
CallbackID string // CallbackID is the identifier used to communicate a response to the notification
AlertID int // The global alert number
Summary string
Details string
ServiceID string
ServiceName string

// OriginalStatus is the status of the first Alert notification to this Dest for this AlertID.
OriginalStatus *SendResult
Expand Down
8 changes: 8 additions & 0 deletions notification/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Store struct {
sendTestLock *sql.Stmt
findManyMessageStatuses *sql.Stmt
lastMessageStatus *sql.Stmt
svcInfo *sql.Stmt

origAlertMessage *sql.Stmt

Expand Down Expand Up @@ -169,6 +170,13 @@ func NewStore(ctx context.Context, db *sql.DB) (*Store, error) {
from outgoing_messages om
where message_type = $1 and contact_method_id = $2 and created_at >= $3
`),
svcInfo: p.P(`
SELECT
name,
(SELECT count(*) FROM alerts WHERE service_id = $1 AND status = 'triggered')
FROM services
WHERE id = $1
`),
}, p.Err
}

Expand Down
24 changes: 14 additions & 10 deletions notification/webhook/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ type Sender struct{}

// POSTDataAlert represents fields in outgoing alert notification.
type POSTDataAlert struct {
AppName string
Type string
AlertID int
Summary string
Details string
AppName string
Type string
AlertID int
Summary string
Details string
ServiceID string
ServiceName string
}

// POSTDataAlertBundle represents fields in outgoing alert bundle notification.
Expand Down Expand Up @@ -102,11 +104,13 @@ func (s *Sender) Send(ctx context.Context, msg notification.Message) (*notificat
}
case notification.Alert:
payload = POSTDataAlert{
AppName: cfg.ApplicationName(),
Type: "Alert",
Details: m.Details,
AlertID: m.AlertID,
Summary: m.Summary,
AppName: cfg.ApplicationName(),
Type: "Alert",
Details: m.Details,
AlertID: m.AlertID,
Summary: m.Summary,
ServiceID: m.ServiceID,
ServiceName: m.ServiceName,
}
case notification.AlertBundle:
payload = POSTDataAlertBundle{
Expand Down
12 changes: 8 additions & 4 deletions test/smoke/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
)

type WebhookTestingAlert struct {
AlertID int
Type string
Summary string
Details string
AlertID int
Type string
Summary string
Details string
ServiceID string
ServiceName string
}

func TestWebhookAlert(t *testing.T) {
Expand Down Expand Up @@ -79,4 +81,6 @@ func TestWebhookAlert(t *testing.T) {
assert.Equal(t, alert.Type, "Alert")
assert.Equal(t, alert.Summary, "testing summary")
assert.Equal(t, alert.Details, "testing details")
assert.Equal(t, alert.ServiceID, h.UUID("sid"))
assert.Equal(t, alert.ServiceName, "service")
}