Skip to content

Commit aec12dd

Browse files
committed
fix comments
Signed-off-by: Yijie Qin <[email protected]>
1 parent 6c8b8b9 commit aec12dd

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

notify/notify.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const MinTimeout = 10 * time.Second
5454
// defaultStatusCodeCategory is the default status code category for numTotalFailedNotifications metric
5555
const defaultStatusCodeCategory = "5xx"
5656

57+
const (
58+
failure4xxCategoryCode = "4xx"
59+
failure5xxCategoryCode = "5xx"
60+
)
61+
62+
// possibleFailureStatusCategory is a list of possible failure status code category
63+
var possibleFailureStatusCategory = []string{failure4xxCategoryCode, failure5xxCategoryCode}
64+
5765
// Notifier notifies about alerts under constraints of the given context. It
5866
// returns an error if unsuccessful and a flag whether the error is
5967
// recoverable. This information is useful for a retry logic.
@@ -300,7 +308,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
300308
m.numNotificationRequestsFailedTotal.WithLabelValues(integration)
301309
m.notificationLatencySeconds.WithLabelValues(integration)
302310

303-
for _, code := range PossibleFailureStatusCategory {
311+
for _, code := range possibleFailureStatusCategory {
304312
m.numTotalFailedNotifications.WithLabelValues(integration, code)
305313
}
306314
}
@@ -672,10 +680,7 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale
672680
statusCodeCategory := defaultStatusCodeCategory
673681
if err != nil {
674682
if e, ok := errors.Cause(err).(*ErrorWithStatusCode); ok {
675-
result, interErr := getFailureStatusCodeCategory(e.StatusCode)
676-
if interErr == nil {
677-
statusCodeCategory = result
678-
}
683+
statusCodeCategory = getFailureStatusCodeCategory(e.StatusCode)
679684
}
680685
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), statusCodeCategory).Inc()
681686
}
@@ -890,3 +895,17 @@ func inTimeIntervals(now time.Time, intervals map[string][]timeinterval.TimeInte
890895
}
891896
return false, nil
892897
}
898+
899+
// getFailureStatusCodeCategory return the status code category for failure request
900+
// the status starts with 4 will return 4xx and starts with 5 will return 5xx
901+
// other than 4xx and 5xx input status will return an 5xx.
902+
func getFailureStatusCodeCategory(statusCode int) string {
903+
if statusCode/100 == 4 {
904+
return failure4xxCategoryCode
905+
}
906+
if statusCode/100 == 5 {
907+
return failure5xxCategoryCode
908+
}
909+
910+
return defaultStatusCodeCategory
911+
}

notify/util.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,9 @@ import (
3030
"github.com/prometheus/alertmanager/types"
3131
)
3232

33-
const (
34-
failure4xxCategoryCode = "4xx"
35-
failure5xxCategoryCode = "5xx"
36-
failureUnknownCategoryCode = "unknown"
37-
)
38-
3933
// UserAgentHeader is the default User-Agent for notification requests
4034
var UserAgentHeader = fmt.Sprintf("Alertmanager/%s", version.Version)
4135

42-
// PossibleFailureStatusCategory is a list of possible failure status code category
43-
var PossibleFailureStatusCategory = []string{failure4xxCategoryCode, failure5xxCategoryCode, failureUnknownCategoryCode}
44-
4536
// RedactURL removes the URL part from an error of *url.Error type.
4637
func RedactURL(err error) error {
4738
e, ok := err.(*url.Error)
@@ -176,20 +167,6 @@ func readAll(r io.Reader) string {
176167
return string(bs)
177168
}
178169

179-
// getFailureStatusCodeCategory return the status code category for failure request
180-
// the status starts with 4 will return 4xx and starts with 5 will return 5xx
181-
// other than 4xx and 5xx input status will return an error.
182-
func getFailureStatusCodeCategory(statusCode int) (string, error) {
183-
if statusCode/100 == 4 {
184-
return failure4xxCategoryCode, nil
185-
}
186-
if statusCode/100 == 5 {
187-
return failure5xxCategoryCode, nil
188-
}
189-
190-
return failureUnknownCategoryCode, fmt.Errorf("unexpected status code %v", statusCode)
191-
}
192-
193170
// Retrier knows when to retry an HTTP request to a receiver. 2xx status codes
194171
// are successful, anything else is a failure and only 5xx status codes should
195172
// be retried.

0 commit comments

Comments
 (0)