Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
  • Loading branch information
qinxx108 committed Jan 12, 2023
1 parent d928299 commit ebbb2f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
29 changes: 24 additions & 5 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ const MinTimeout = 10 * time.Second
// defaultStatusCodeCategory is the default status code category for numTotalFailedNotifications metric
const defaultStatusCodeCategory = "5xx"

const (
failure4xxCategoryCode = "4xx"
failure5xxCategoryCode = "5xx"
)

// possibleFailureStatusCategory is a list of possible failure status code category
var possibleFailureStatusCategory = []string{failure4xxCategoryCode, failure5xxCategoryCode}

// Notifier notifies about alerts under constraints of the given context. It
// returns an error if unsuccessful and a flag whether the error is
// recoverable. This information is useful for a retry logic.
Expand Down Expand Up @@ -300,7 +308,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
m.numNotificationRequestsFailedTotal.WithLabelValues(integration)
m.notificationLatencySeconds.WithLabelValues(integration)

for _, code := range PossibleFailureStatusCategory {
for _, code := range possibleFailureStatusCategory {
m.numTotalFailedNotifications.WithLabelValues(integration, code)
}
}
Expand Down Expand Up @@ -672,10 +680,7 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale
statusCodeCategory := defaultStatusCodeCategory
if err != nil {
if e, ok := errors.Cause(err).(*ErrorWithStatusCode); ok {
result, interErr := getFailureStatusCodeCategory(e.StatusCode)
if interErr == nil {
statusCodeCategory = result
}
statusCodeCategory = getFailureStatusCodeCategory(e.StatusCode)
}
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), statusCodeCategory).Inc()
}
Expand Down Expand Up @@ -890,3 +895,17 @@ func inTimeIntervals(now time.Time, intervals map[string][]timeinterval.TimeInte
}
return false, nil
}

// 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 5xx.
func getFailureStatusCodeCategory(statusCode int) string {
if statusCode/100 == 4 {
return failure4xxCategoryCode
}
if statusCode/100 == 5 {
return failure5xxCategoryCode
}

return defaultStatusCodeCategory
}
23 changes: 0 additions & 23 deletions notify/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,9 @@ import (
"github.com/prometheus/alertmanager/types"
)

const (
failure4xxCategoryCode = "4xx"
failure5xxCategoryCode = "5xx"
failureUnknownCategoryCode = "unknown"
)

// UserAgentHeader is the default User-Agent for notification requests
var UserAgentHeader = fmt.Sprintf("Alertmanager/%s", version.Version)

// PossibleFailureStatusCategory is a list of possible failure status code category
var PossibleFailureStatusCategory = []string{failure4xxCategoryCode, failure5xxCategoryCode, failureUnknownCategoryCode}

// RedactURL removes the URL part from an error of *url.Error type.
func RedactURL(err error) error {
e, ok := err.(*url.Error)
Expand Down Expand Up @@ -176,20 +167,6 @@ func readAll(r io.Reader) string {
return string(bs)
}

// 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 failure4xxCategoryCode, nil
}
if statusCode/100 == 5 {
return failure5xxCategoryCode, nil
}

return failureUnknownCategoryCode, fmt.Errorf("unexpected status code %v", statusCode)
}

// Retrier knows when to retry an HTTP request to a receiver. 2xx status codes
// are successful, anything else is a failure and only 5xx status codes should
// be retried.
Expand Down

0 comments on commit ebbb2f7

Please sign in to comment.