Skip to content

Commit

Permalink
getFailureStatusCodeCategory to return error rather than empty string…
Browse files Browse the repository at this point in the history
… when unknown status code
  • Loading branch information
qinxx108 committed Oct 12, 2022
1 parent 61b7887 commit 106cf64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
14 changes: 9 additions & 5 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 7 additions & 4 deletions notify/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 106cf64

Please sign in to comment.