Skip to content

Commit

Permalink
feat(notify): use logrus to capture errors (#365)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Delberghe <open-source@orandin.fr>
  • Loading branch information
orandin authored Oct 17, 2022
1 parent 1286327 commit 82f2ab5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
36 changes: 32 additions & 4 deletions pkg/notify/errors.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
package notify

import "fmt"
import (
log "github.com/sirupsen/logrus"

"github.com/ovh/utask"
)

const (
errSendCommon string = "Error while sending notification on"
)

// WrappedSendError print a formatted string from Send Notify in case of issue
func WrappedSendError(etype string, err string) {
fmt.Printf("%s %s: %s", errSendCommon, etype, err)
// WrappedSendError captures an error from Send Notify
func WrappedSendError(err error, m *Message, backend, name string) {
newLogger(err, m, backend, name).
Errorf("%s %s", errSendCommon, backend)
}

// WrappedSendErrorWithBody captures an error with a response body from Send Notify.
func WrappedSendErrorWithBody(err error, m *Message, backend, name, body string) {
newLogger(err, m, backend, name).
WithField("response_body", body).
Errorf("%s %s", errSendCommon, backend)
}

// 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,
"notification_type": m.NotificationType,
"instance_id": utask.InstanceID,
}).WithError(err)
}
3 changes: 2 additions & 1 deletion pkg/notify/opsgenie/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/juju/errors"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"

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

Expand Down Expand Up @@ -75,7 +76,7 @@ func (ns *NotificationSender) Send(m *notify.Message, name string) {

_, err := ns.client.Create(ctx, req)
if err != nil {
notify.WrappedSendError(Type, err.Error())
notify.WrappedSendError(err, m, Type, name)
return
}
}
8 changes: 5 additions & 3 deletions pkg/notify/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slack
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -57,14 +58,14 @@ func (sn *NotificationSender) Send(m *notify.Message, name string) {

req, err := http.NewRequest(http.MethodPost, sn.webhookURL, bytes.NewBuffer(slackBody))
if err != nil {
notify.WrappedSendError(Type, err.Error())
notify.WrappedSendError(err, m, Type, name)
return
}

req.Header.Add("Content-Type", "application/json")
resp, err := sn.httpClient.Do(req)
if err != nil {
notify.WrappedSendError(Type, err.Error())
notify.WrappedSendError(err, m, Type, name)
return
}

Expand All @@ -73,7 +74,8 @@ func (sn *NotificationSender) Send(m *notify.Message, name string) {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
notify.WrappedSendError(Type, "Non-ok response returned from Slack")
err = errors.New("non-ok response returned from Slack")
notify.WrappedSendError(err, m, Type, name)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/notify/tat/tat.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewTatNotificationSender(url, user, pass, topic string) (*NotificationSende
func (tn *NotificationSender) Send(m *notify.Message, name string) {
client, err := tn.spawnTatClient()
if err != nil {
fmt.Println(err)
notify.WrappedSendError(err, m, Type, name)
return
}

Expand All @@ -64,7 +64,7 @@ func (tn *NotificationSender) Send(m *notify.Message, name string) {
},
)
if err != nil {
fmt.Println(err)
notify.WrappedSendError(err, m, Type, name)
return
}
// TODO create message for task creation
Expand Down
15 changes: 9 additions & 6 deletions pkg/notify/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -51,13 +50,13 @@ func (w *NotificationSender) Send(m *notify.Message, name string) {

b, err := json.Marshal(msg)
if err != nil {
fmt.Println(err)
notify.WrappedSendError(err, m, Type, name)
return
}

req, err := http.NewRequest("POST", w.webhookURL, bytes.NewBuffer(b))
if err != nil {
fmt.Println(err)
notify.WrappedSendError(err, m, Type, name)
return
}

Expand All @@ -71,18 +70,22 @@ func (w *NotificationSender) Send(m *notify.Message, name string) {

res, err := w.httpClient.Do(req)
if err != nil {
log.Println(err)
notify.WrappedSendError(err, m, Type, name)
return
}

defer res.Body.Close()

if res.StatusCode >= 400 {
resErr := fmt.Errorf("failed to send notification using %q: backend returned with status code %d", name, res.StatusCode)

resBody, err := ioutil.ReadAll(res.Body)
log.Printf("failed to send notification using %q: backend returned with status code %d\n", name, res.StatusCode)
if err == nil {
log.Printf("webhook %q response body: %s\n", name, string(resBody))
notify.WrappedSendErrorWithBody(resErr, m, Type, name, string(resBody))
} else {
notify.WrappedSendError(resErr, m, Type, name)
}

return
}
}

0 comments on commit 82f2ab5

Please sign in to comment.