Skip to content

Commit 205b802

Browse files
committed
use iota for reason type
Signed-off-by: Yijie Qin <[email protected]>
1 parent 12b65a7 commit 205b802

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

notify/notify.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
262262
Namespace: "alertmanager",
263263
Name: "notifications_failed_total",
264264
Help: "The total number of failed notifications.",
265-
}, []string{"integration", "code"}),
265+
}, []string{"integration", "reason"}),
266266
numNotificationRequestsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
267267
Namespace: "alertmanager",
268268
Name: "notification_requests_total",
@@ -297,8 +297,8 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
297297
m.numNotificationRequestsFailedTotal.WithLabelValues(integration)
298298
m.notificationLatencySeconds.WithLabelValues(integration)
299299

300-
for _, code := range possibleFailureStatusCategory {
301-
m.numTotalFailedNotifications.WithLabelValues(integration, code)
300+
for _, reason := range possibleFailureReasonCategory {
301+
m.numTotalFailedNotifications.WithLabelValues(integration, reason)
302302
}
303303
}
304304
r.MustRegister(
@@ -666,10 +666,10 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale
666666
r.metrics.numNotifications.WithLabelValues(r.integration.Name()).Inc()
667667
ctx, alerts, err := r.exec(ctx, l, alerts...)
668668

669-
failureReason := defaultFailureReason
669+
failureReason := DefaultReason.String()
670670
if err != nil {
671671
if e, ok := errors.Cause(err).(*ErrorWithReason); ok {
672-
failureReason = e.Reason
672+
failureReason = e.Reason.String()
673673
}
674674
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name(), failureReason).Inc()
675675
}

notify/notify_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -425,27 +425,25 @@ func TestRetryStageWithError(t *testing.T) {
425425

426426
func TestRetryStageWithErrorCode(t *testing.T) {
427427
testcases := map[string]struct {
428-
reason string
429-
reasonlabel string
430-
expectedCount int
428+
isNewErrorWithReason bool
429+
reason Reason
430+
reasonlabel string
431+
expectedCount int
431432
}{
432-
"for clientError": {reason: clientError, reasonlabel: clientError, expectedCount: 1},
433-
"for serverError": {reason: serverError, reasonlabel: serverError, expectedCount: 1},
434-
"for unexpected code": {reason: "unexpected", reasonlabel: defaultFailureReason, expectedCount: 1},
433+
"for clientError": {isNewErrorWithReason: true, reason: ClientErrorReason, reasonlabel: ClientErrorReason.String(), expectedCount: 1},
434+
"for serverError": {isNewErrorWithReason: true, reason: ServerErrorReason, reasonlabel: ServerErrorReason.String(), expectedCount: 1},
435+
"for unexpected code": {isNewErrorWithReason: false, reason: DefaultReason, reasonlabel: DefaultReason.String(), expectedCount: 1},
435436
}
436437
for _, testData := range testcases {
437-
fail, retry := true, false
438-
sent := []*types.Alert{}
438+
retry := false
439439
testData := testData
440440
i := Integration{
441441
name: "test",
442442
notifier: notifierFunc(func(ctx context.Context, alerts ...*types.Alert) (bool, error) {
443-
if fail {
444-
fail = false
445-
return retry, NewErrorWithReason(testData.reason, errors.New("fail to deliver notification"))
443+
if !testData.isNewErrorWithReason {
444+
return retry, errors.New("fail to deliver notification")
446445
}
447-
sent = append(sent, alerts...)
448-
return false, nil
446+
return retry, NewErrorWithReason(testData.reason, errors.New("fail to deliver notification"))
449447
}),
450448
rs: sendResolved(false),
451449
}

notify/util.go

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

33-
// defaultFailureReason is the default reason for numTotalFailedNotifications metric
34-
const defaultFailureReason = "other"
35-
36-
const (
37-
clientError = "clientError"
38-
serverError = "ServerError"
39-
)
40-
41-
// possibleFailureStatusCategory is a list of possible failure reason
42-
var possibleFailureStatusCategory = []string{clientError, serverError, defaultFailureReason}
43-
4433
// UserAgentHeader is the default User-Agent for notification requests
4534
var UserAgentHeader = fmt.Sprintf("Alertmanager/%s", version.Version)
4635

@@ -225,10 +214,10 @@ type ErrorWithReason struct {
225214
Err error
226215

227216
// The reason of the failure.
228-
Reason string
217+
Reason Reason
229218
}
230219

231-
func NewErrorWithReason(reason string, err error) *ErrorWithReason {
220+
func NewErrorWithReason(reason Reason, err error) *ErrorWithReason {
232221
return &ErrorWithReason{
233222
Err: err,
234223
Reason: reason,
@@ -239,16 +228,41 @@ func (e *ErrorWithReason) Error() string {
239228
return e.Err.Error()
240229
}
241230

231+
// Reason is the failure reason
232+
type Reason int
233+
234+
const (
235+
DefaultReason Reason = iota
236+
ClientErrorReason
237+
ServerErrorReason
238+
)
239+
240+
func (s Reason) String() string {
241+
switch s {
242+
case DefaultReason:
243+
return "other"
244+
case ClientErrorReason:
245+
return "clientError"
246+
case ServerErrorReason:
247+
return "serverError"
248+
default:
249+
panic(fmt.Sprintf("unknown Reason: %d", s))
250+
}
251+
}
252+
253+
// possibleFailureReasonCategory is a list of possible failure reason
254+
var possibleFailureReasonCategory = []string{DefaultReason.String(), ClientErrorReason.String(), ServerErrorReason.String()}
255+
242256
// GetFailureReasonFromStatusCode return the reason for failure request
243257
// the status starts with 4 will return 4xx and starts with 5 will return 5xx
244258
// other than 4xx and 5xx input status will return an 5xx.
245-
func GetFailureReasonFromStatusCode(statusCode int) string {
259+
func GetFailureReasonFromStatusCode(statusCode int) Reason {
246260
if statusCode/100 == 4 {
247-
return clientError
261+
return ClientErrorReason
248262
}
249263
if statusCode/100 == 5 {
250-
return serverError
264+
return ServerErrorReason
251265
}
252266

253-
return defaultFailureReason
267+
return DefaultReason
254268
}

0 commit comments

Comments
 (0)