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 Nov 1, 2022
1 parent 839b227 commit adf583d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
Namespace: "alertmanager",
Name: "notifications_failed_total",
Help: "The total number of failed notifications.",
}, []string{"integration", "statusCode"}),
}, []string{"integration", "code"}),
numNotificationRequestsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "alertmanager",
Name: "notification_requests_total",
Expand Down Expand Up @@ -300,6 +300,10 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
m.numNotificationRequestsTotal.WithLabelValues(integration)
m.numNotificationRequestsFailedTotal.WithLabelValues(integration)
m.notificationLatencySeconds.WithLabelValues(integration)

for _, code := range PossibleFailureStatusCategory {
m.numTotalFailedNotifications.WithLabelValues(integration, code)
}
}
r.MustRegister(
m.numNotifications, m.numTotalFailedNotifications,
Expand Down
9 changes: 5 additions & 4 deletions notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,11 @@ func TestRetryStageWithErrorCode(t *testing.T) {
codelabel string
expectedCount int
}{
"for 400": {errorcode: 400, codelabel: "4xx", expectedCount: 1},
"for 402": {errorcode: 402, codelabel: "4xx", expectedCount: 1},
"for 500": {errorcode: 500, codelabel: "5xx", expectedCount: 1},
"for 502": {errorcode: 502, codelabel: "5xx", expectedCount: 1},
"for 400": {errorcode: 400, codelabel: failure4xxCategoryCode, expectedCount: 1},
"for 402": {errorcode: 402, codelabel: failure4xxCategoryCode, expectedCount: 1},
"for 500": {errorcode: 500, codelabel: failure5xxCategoryCode, expectedCount: 1},
"for 502": {errorcode: 502, codelabel: failure5xxCategoryCode, expectedCount: 1},
"for expected code 100": {errorcode: 100, codelabel: failure5xxCategoryCode, expectedCount: 1},
}
for _, testData := range testcases {
fail, retry := true, false
Expand Down
15 changes: 12 additions & 3 deletions notify/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ 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 @@ -172,13 +181,13 @@ func readAll(r io.Reader) string {
// other than 4xx and 5xx input status will return an error.
func getFailureStatusCodeCategory(statusCode int) (string, error) {
if statusCode/100 == 4 {
return "4xx", nil
return failure4xxCategoryCode, nil
}
if statusCode/100 == 5 {
return "5xx", nil
return failure5xxCategoryCode, nil
}

return "", fmt.Errorf("unexpected status code %v", statusCode)
return failureUnknownCategoryCode, 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 adf583d

Please sign in to comment.