@@ -54,6 +54,14 @@ const MinTimeout = 10 * time.Second
54
54
// defaultStatusCodeCategory is the default status code category for numTotalFailedNotifications metric
55
55
const defaultStatusCodeCategory = "5xx"
56
56
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
+
57
65
// Notifier notifies about alerts under constraints of the given context. It
58
66
// returns an error if unsuccessful and a flag whether the error is
59
67
// recoverable. This information is useful for a retry logic.
@@ -300,7 +308,7 @@ func NewMetrics(r prometheus.Registerer) *Metrics {
300
308
m .numNotificationRequestsFailedTotal .WithLabelValues (integration )
301
309
m .notificationLatencySeconds .WithLabelValues (integration )
302
310
303
- for _ , code := range PossibleFailureStatusCategory {
311
+ for _ , code := range possibleFailureStatusCategory {
304
312
m .numTotalFailedNotifications .WithLabelValues (integration , code )
305
313
}
306
314
}
@@ -672,10 +680,7 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale
672
680
statusCodeCategory := defaultStatusCodeCategory
673
681
if err != nil {
674
682
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 )
679
684
}
680
685
r .metrics .numTotalFailedNotifications .WithLabelValues (r .integration .Name (), statusCodeCategory ).Inc ()
681
686
}
@@ -890,3 +895,17 @@ func inTimeIntervals(now time.Time, intervals map[string][]timeinterval.TimeInte
890
895
}
891
896
return false , nil
892
897
}
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
+ }
0 commit comments