From cb9724db47dc1c1e4b8352baf62d21e9a6919d36 Mon Sep 17 00:00:00 2001 From: Holger Freyther Date: Fri, 12 Apr 2024 17:28:59 +0800 Subject: [PATCH] notify/webhook: Fix crash on errors when url_file is used (#3798) (#3800) --- notify/webhook/webhook.go | 18 +----------------- notify/webhook/webhook_test.go | 4 ++-- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/notify/webhook/webhook.go b/notify/webhook/webhook.go index 41241569ef..ce0eef3feb 100644 --- a/notify/webhook/webhook.go +++ b/notify/webhook/webhook.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "fmt" - "io" "net/http" "os" "strings" @@ -55,11 +54,7 @@ func New(conf *config.WebhookConfig, t *template.Template, l log.Logger, httpOpt client: client, // Webhooks are assumed to respond with 2xx response codes on a successful // request and 5xx response codes are assumed to be recoverable. - retrier: ¬ify.Retrier{ - CustomDetailsFunc: func(_ int, body io.Reader) string { - return errDetails(body, conf.URL.String()) - }, - }, + retrier: ¬ify.Retrier{}, }, nil } @@ -126,14 +121,3 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er } return shouldRetry, err } - -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)) -} diff --git a/notify/webhook/webhook_test.go b/notify/webhook/webhook_test.go index 03f2592479..ede0840657 100644 --- a/notify/webhook/webhook_test.go +++ b/notify/webhook/webhook_test.go @@ -68,12 +68,12 @@ func TestWebhookRetry(t *testing.T) { `{"status":"invalid event"}`, )), - exp: fmt.Sprintf(`unexpected status code %d: %s: {"status":"invalid event"}`, http.StatusBadRequest, u.String()), + exp: fmt.Sprintf(`unexpected status code %d: {"status":"invalid event"}`, http.StatusBadRequest), }, { status: http.StatusBadRequest, - exp: fmt.Sprintf(`unexpected status code %d: %s`, http.StatusBadRequest, u.String()), + exp: fmt.Sprintf(`unexpected status code %d`, http.StatusBadRequest), }, } { t.Run("", func(t *testing.T) {