From b9b68d2b2ea8e9f34a1a9aaca3bfdcaca2ba76fd Mon Sep 17 00:00:00 2001 From: Yijie Qin Date: Tue, 28 Mar 2023 18:24:22 -0400 Subject: [PATCH 1/2] add reason code to notifiers Signed-off-by: Yijie Qin --- notify/opsgenie/opsgenie.go | 2 +- notify/pagerduty/pagerduty.go | 7 ++++++- notify/pushover/pushover.go | 6 +++++- notify/victorops/victorops.go | 6 +++++- notify/webhook/webhook.go | 6 +++++- notify/wechat/wechat.go | 2 +- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/notify/opsgenie/opsgenie.go b/notify/opsgenie/opsgenie.go index 426734f275..0a5e218f3a 100644 --- a/notify/opsgenie/opsgenie.go +++ b/notify/opsgenie/opsgenie.go @@ -110,7 +110,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) notify.Drain(resp) if err != nil { - return shouldRetry, err + return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) } } return true, nil diff --git a/notify/pagerduty/pagerduty.go b/notify/pagerduty/pagerduty.go index b6b816e039..1b1fb7c839 100644 --- a/notify/pagerduty/pagerduty.go +++ b/notify/pagerduty/pagerduty.go @@ -296,7 +296,12 @@ func (n *Notifier) notifyV2( } defer notify.Drain(resp) - return n.retrier.Check(resp.StatusCode, resp.Body) + retry, err := n.retrier.Check(resp.StatusCode, resp.Body) + + if err != nil { + return retry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) + } + return retry, err } // Notify implements the Notifier interface. diff --git a/notify/pushover/pushover.go b/notify/pushover/pushover.go index d1c43174c6..4d9a86c633 100644 --- a/notify/pushover/pushover.go +++ b/notify/pushover/pushover.go @@ -163,5 +163,9 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) } defer notify.Drain(resp) - return n.retrier.Check(resp.StatusCode, resp.Body) + shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) + if err != nil { + return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) + } + return shouldRetry, err } diff --git a/notify/victorops/victorops.go b/notify/victorops/victorops.go index 02c9362190..780f7a8d1d 100644 --- a/notify/victorops/victorops.go +++ b/notify/victorops/victorops.go @@ -104,7 +104,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) } defer notify.Drain(resp) - return n.retrier.Check(resp.StatusCode, resp.Body) + shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) + if err != nil { + return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) + } + return shouldRetry, err } // Create the JSON payload to be sent to the VictorOps API. diff --git a/notify/webhook/webhook.go b/notify/webhook/webhook.go index d361d09c3f..a1f8c37dbf 100644 --- a/notify/webhook/webhook.go +++ b/notify/webhook/webhook.go @@ -119,7 +119,11 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er } defer notify.Drain(resp) - return n.retrier.Check(resp.StatusCode, resp.Body) + shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body) + if err != nil { + return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) + } + return shouldRetry, err } func errDetails(body io.Reader, url string) string { diff --git a/notify/wechat/wechat.go b/notify/wechat/wechat.go index a926b2e8fb..4ac101a404 100644 --- a/notify/wechat/wechat.go +++ b/notify/wechat/wechat.go @@ -168,7 +168,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) defer notify.Drain(resp) if resp.StatusCode != 200 { - return true, fmt.Errorf("unexpected status code %v", resp.StatusCode) + return true, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), fmt.Errorf("unexpected status code %v", resp.StatusCode)) } body, err := io.ReadAll(resp.Body) From 73d42b7f0609bb60fc22aa762b343c7d3ec27a24 Mon Sep 17 00:00:00 2001 From: Yijie Qin Date: Tue, 28 Mar 2023 19:17:29 -0400 Subject: [PATCH 2/2] fix the lint Signed-off-by: Yijie Qin --- notify/pagerduty/pagerduty.go | 1 - 1 file changed, 1 deletion(-) diff --git a/notify/pagerduty/pagerduty.go b/notify/pagerduty/pagerduty.go index 1b1fb7c839..39ec84d919 100644 --- a/notify/pagerduty/pagerduty.go +++ b/notify/pagerduty/pagerduty.go @@ -297,7 +297,6 @@ func (n *Notifier) notifyV2( defer notify.Drain(resp) retry, err := n.retrier.Check(resp.StatusCode, resp.Body) - if err != nil { return retry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err) }