@@ -40,6 +40,12 @@ import (
40
40
* involves explicitly acknowledging support for the metric across multiple releases, in accordance with
41
41
* the metric stability policy.
42
42
*/
43
+ const (
44
+ successLabel = "success"
45
+ failureLabel = "failure"
46
+ errorLabel = "error"
47
+ )
48
+
43
49
var (
44
50
authenticatedUserCounter = metrics .NewCounterVec (
45
51
& metrics.CounterOpts {
@@ -49,10 +55,19 @@ var (
49
55
},
50
56
[]string {"username" },
51
57
)
58
+
59
+ authenticatedAttemptsCounter = metrics .NewCounterVec (
60
+ & metrics.CounterOpts {
61
+ Name : "authentication_attempts" ,
62
+ Help : "Counter of authenticated attempts." ,
63
+ },
64
+ []string {"result" },
65
+ )
52
66
)
53
67
54
68
func init () {
55
69
legacyregistry .MustRegister (authenticatedUserCounter )
70
+ legacyregistry .MustRegister (authenticatedAttemptsCounter )
56
71
}
57
72
58
73
// WithAuthentication creates an http handler that tries to authenticate the given request as a user, and then
@@ -72,7 +87,11 @@ func WithAuthentication(handler http.Handler, auth authenticator.Request, failed
72
87
if err != nil || ! ok {
73
88
if err != nil {
74
89
klog .Errorf ("Unable to authenticate the request due to an error: %v" , err )
90
+ authenticatedAttemptsCounter .WithLabelValues (errorLabel ).Inc ()
91
+ } else if ! ok {
92
+ authenticatedAttemptsCounter .WithLabelValues (failureLabel ).Inc ()
75
93
}
94
+
76
95
failed .ServeHTTP (w , req )
77
96
return
78
97
}
@@ -86,6 +105,7 @@ func WithAuthentication(handler http.Handler, auth authenticator.Request, failed
86
105
req = req .WithContext (genericapirequest .WithUser (req .Context (), resp .User ))
87
106
88
107
authenticatedUserCounter .WithLabelValues (compressUsername (resp .User .GetName ())).Inc ()
108
+ authenticatedAttemptsCounter .WithLabelValues (successLabel ).Inc ()
89
109
90
110
handler .ServeHTTP (w , req )
91
111
})
0 commit comments