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

Log Response Details for Notifier errors for Webhooks, Pushover and VictorOps #3103

Merged
merged 4 commits into from
Oct 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion notify/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}
defer notify.Drain(resp)

return n.retrier.Check(resp.StatusCode, nil)
return n.retrier.Check(resp.StatusCode, resp.Body)
}
2 changes: 1 addition & 1 deletion notify/victorops/victorops.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}
defer notify.Drain(resp)

return n.retrier.Check(resp.StatusCode, nil)
return n.retrier.Check(resp.StatusCode, resp.Body)
}

// Create the JSON payload to be sent to the VictorOps API.
Expand Down
20 changes: 16 additions & 4 deletions notify/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -53,8 +54,8 @@ func New(conf *config.WebhookConfig, t *template.Template, l log.Logger, httpOpt
// Webhooks are assumed to respond with 2xx response codes on a successful
// request and 5xx response codes are assumed to be recoverable.
retrier: &notify.Retrier{
CustomDetailsFunc: func(int, io.Reader) string {
return conf.URL.String()
CustomDetailsFunc: func(_ int, body io.Reader) string {
return errDetails(body, conf.URL.String())
simonpasquier marked this conversation as resolved.
Show resolved Hide resolved
},
},
}, nil
Expand Down Expand Up @@ -104,7 +105,18 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
if err != nil {
return true, err
}
notify.Drain(resp)
defer notify.Drain(resp)

return n.retrier.Check(resp.StatusCode, nil)
return n.retrier.Check(resp.StatusCode, resp.Body)
OktarianTB marked this conversation as resolved.
Show resolved Hide resolved
}

func errDetails(body io.Reader, url string) string {
if body == nil {
return url
}
bs, err := io.ReadAll(body)
if err != nil {
return url
}
return fmt.Sprintf("%s: %s", url, string(bs))
}
42 changes: 38 additions & 4 deletions notify/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
package webhook

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"testing"

Expand Down Expand Up @@ -43,10 +46,41 @@ func TestWebhookRetry(t *testing.T) {
if err != nil {
require.NoError(t, err)
}
for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
actual, _ := notifier.retrier.Check(statusCode, nil)
require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
}

t.Run("test retry status code", func(t *testing.T) {
for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
actual, _ := notifier.retrier.Check(statusCode, nil)
require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
}
})

t.Run("test retry error details", func(t *testing.T) {
for _, tc := range []struct {
status int
body io.Reader

exp string
}{
{
status: http.StatusBadRequest,
body: bytes.NewBuffer([]byte(
`{"status":"invalid event"}`,
)),

exp: fmt.Sprintf(`unexpected status code %d: %s: {"status":"invalid event"}`, http.StatusBadRequest, u.String()),
},
{
status: http.StatusBadRequest,

exp: fmt.Sprintf(`unexpected status code %d: %s`, http.StatusBadRequest, u.String()),
},
} {
t.Run("", func(t *testing.T) {
_, err = notifier.retrier.Check(tc.status, tc.body)
require.Equal(t, tc.exp, err.Error())
})
}
})
}

func TestWebhookTruncateAlerts(t *testing.T) {
Expand Down