Skip to content

Commit

Permalink
feat: auto-close Opsgenie alert when task state is DONE (#417)
Browse files Browse the repository at this point in the history
Signed-off-by: Wesley GALIPO <wesley.galipo@ovhcloud.com>
  • Loading branch information
Galiley authored May 2, 2023
1 parent 454acf7 commit 4355bad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
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)
}
}

0 comments on commit 4355bad

Please sign in to comment.