Skip to content

Commit b99dad3

Browse files
committed
getFailureStatusCodeCategory to return error rather than empty string when unknown status code
Signed-off-by: Yijie Qin <[email protected]>
1 parent 4dbe3c2 commit b99dad3

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

notify/notify.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type Peer interface {
5151
// to a notification pipeline.
5252
const MinTimeout = 10 * time.Second
5353

54-
// defaultStatusCode is the default status code for numTotalFailedNotifications metric
55-
const defaultStatusCode = "5xx"
54+
// defaultStatusCodeCategory is the default status code category for numTotalFailedNotifications metric
55+
const defaultStatusCodeCategory = "5xx"
5656

5757
// Notifier notifies about alerts under constraints of the given context. It
5858
// 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
665665
func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
666666
r.metrics.numNotifications.WithLabelValues(r.integration.Name()).Inc()
667667
ctx, alerts, err := r.exec(ctx, l, alerts...)
668+
669+
statusCodeCategory := defaultStatusCodeCategory
668670
if err != nil {
669671
if e, ok := errors.Cause(err).(*ErrorWithStatusCode); ok {
670-
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), getFailureStatusCodeCategory(e.StatusCode)).Inc()
671-
} else {
672-
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), defaultStatusCode).Inc()
672+
result, interErr := getFailureStatusCodeCategory(e.StatusCode)
673+
if interErr == nil {
674+
statusCodeCategory = result
675+
}
673676
}
674677
}
678+
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), statusCodeCategory).Inc()
675679
return ctx, alerts, err
676680
}
677681

notify/util.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,18 @@ func readAll(r io.Reader) string {
167167
return string(bs)
168168
}
169169

170-
func getFailureStatusCodeCategory(statusCode int) string {
170+
// getFailureStatusCodeCategory return the status code category for failure request
171+
// the status starts with 4 will return 4xx and starts with 5 will return 5xx
172+
// other than 4xx and 5xx input status will return an error.
173+
func getFailureStatusCodeCategory(statusCode int) (string, error) {
171174
if statusCode/100 == 4 {
172-
return "4xx"
175+
return "4xx", nil
173176
}
174177
if statusCode/100 == 5 {
175-
return "5xx"
178+
return "5xx", nil
176179
}
177180

178-
return ""
181+
return "", fmt.Errorf("unexpected status code %v", statusCode)
179182
}
180183

181184
// Retrier knows when to retry an HTTP request to a receiver. 2xx status codes

0 commit comments

Comments
 (0)