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: auto-close Opsgenie alert when task state is DONE #417

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 1 addition & 6 deletions pkg/notify/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ func WrappedSendErrorWithBody(err error, m *Message, backend, name, body string)

// newLogger creates a logger instance with pre-filled fields.
func newLogger(err error, m *Message, backend, name string) *log.Entry {
var taskID string
if m != nil { // avoid panic if `m` is nil
taskID = m.Fields["task_id"]
}

return log.WithFields(log.Fields{
"notify_backend": backend,
"notifier_name": name,
"task_id": taskID,
"task_id": m.TaskID(),
"notification_type": m.NotificationType,
"instance_id": utask.InstanceID,
}).WithError(err)
Expand Down
14 changes: 14 additions & 0 deletions pkg/notify/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ type Message struct {
Fields map[string]string
}

func (m *Message) TaskID() string {
if m != nil {
return m.Fields["task_id"]
}
return ""
}

func (m *Message) TaskState() string {
if m != nil {
return m.Fields["state"]
}
return ""
}

// TaskStateUpdate holds a digest of data representing a task state change
type TaskStateUpdate struct {
Title string
Expand Down
39 changes: 29 additions & 10 deletions pkg/notify/opsgenie/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package opsgenie

import (
"context"
"encoding/json"
"time"

"github.com/juju/errors"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"

"github.com/ovh/utask/models/task"
"github.com/ovh/utask/pkg/notify"
)

Expand Down Expand Up @@ -55,6 +57,7 @@ func NewOpsGenieNotificationSender(zone, apikey, timeout string) (*NotificationS
return nil, err
}
}

return &NotificationSender{
opsGenieZone: zone,
opsGenieAPIKey: apikey,
Expand All @@ -64,19 +67,35 @@ func NewOpsGenieNotificationSender(zone, apikey, timeout string) (*NotificationS
}

// Send dispatches a notify.Message to OpsGenie
func (ns *NotificationSender) Send(m *notify.Message, name string) {
req := &alert.CreateAlertRequest{
Message: m.MainMessage,
Description: m.MainMessage,
Details: m.Fields,
}

func (ns *NotificationSender) Send(msg *notify.Message, name string) {
ctx, cancel := context.WithTimeout(context.Background(), ns.opsGenieTimeout)
defer cancel()

_, err := ns.client.Create(ctx, req)
var err error

// Generate an alias to support alert deduplication
// cf. https://support.atlassian.com/opsgenie/docs/what-is-alert-de-duplication/
alias := msg.TaskID()

if msg.TaskState() == task.StateDone {
_, err = ns.client.Close(ctx, &alert.CloseAlertRequest{
IdentifierType: alert.ALIAS,
IdentifierValue: alias,
})
} else {
req := &alert.CreateAlertRequest{
Message: msg.MainMessage,
Description: msg.MainMessage,
Details: msg.Fields,
Alias: alias,
}
msgContent, _ := json.Marshal(msg.Fields)
if msgContent != nil {
req.Note = string(msgContent)
}
_, err = ns.client.Create(ctx, req)
}
if err != nil {
notify.WrappedSendError(err, m, Type, name)
return
notify.WrappedSendError(err, msg, Type, name)
}
}