From 106cf6438806907e885e1941f3fc39be62f5c280 Mon Sep 17 00:00:00 2001 From: Yijie Qin Date: Tue, 11 Oct 2022 22:38:48 -0400 Subject: [PATCH] getFailureStatusCodeCategory to return error rather than empty string when unknown status code --- notify/notify.go | 14 +++++++++----- notify/util.go | 11 +++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/notify/notify.go b/notify/notify.go index 6d16172345..ae7542687e 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -51,8 +51,8 @@ type Peer interface { // to a notification pipeline. const MinTimeout = 10 * time.Second -// defaultStatusCode is the default status code for numTotalFailedNotifications metric -const defaultStatusCode = "5xx" +// defaultStatusCodeCategory is the default status code category for numTotalFailedNotifications metric +const defaultStatusCodeCategory = "5xx" // Notifier notifies about alerts under constraints of the given context. It // returns an error if unsuccessful and a flag whether the error is @@ -665,13 +665,17 @@ func NewRetryStage(i Integration, groupName string, metrics *Metrics) *RetryStag func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) { r.metrics.numNotifications.WithLabelValues(r.integration.Name()).Inc() ctx, alerts, err := r.exec(ctx, l, alerts...) + + statusCodeCategory := defaultStatusCodeCategory if err != nil { if e, ok := errors.Cause(err).(*ErrorWithStatusCode); ok { - r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), getFailureStatusCodeCategory(e.StatusCode)).Inc() - } else { - r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), defaultStatusCode).Inc() + result, interErr := getFailureStatusCodeCategory(e.StatusCode) + if interErr == nil { + statusCodeCategory = result + } } } + r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), statusCodeCategory).Inc() return ctx, alerts, err } diff --git a/notify/util.go b/notify/util.go index f0c9d86176..b3a7a7cd5f 100644 --- a/notify/util.go +++ b/notify/util.go @@ -167,15 +167,18 @@ func readAll(r io.Reader) string { return string(bs) } -func getFailureStatusCodeCategory(statusCode int) string { +// getFailureStatusCodeCategory return the status code category for failure request +// the status starts with 4 will return 4xx and starts with 5 will return 5xx +// other than 4xx and 5xx input status will return an error. +func getFailureStatusCodeCategory(statusCode int) (string, error) { if statusCode/100 == 4 { - return "4xx" + return "4xx", nil } if statusCode/100 == 5 { - return "5xx" + return "5xx", nil } - return "" + return "", fmt.Errorf("unexpected status code %v", statusCode) } // Retrier knows when to retry an HTTP request to a receiver. 2xx status codes