Skip to content

Commit a7ac3b9

Browse files
committed
Add authentication metrics: overall failure and error count
1 parent d7ecc85 commit a7ac3b9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/filters/authentication.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ import (
4040
* involves explicitly acknowledging support for the metric across multiple releases, in accordance with
4141
* the metric stability policy.
4242
*/
43+
const (
44+
successLabel = "success"
45+
failureLabel = "failure"
46+
errorLabel = "error"
47+
)
48+
4349
var (
4450
authenticatedUserCounter = metrics.NewCounterVec(
4551
&metrics.CounterOpts{
@@ -49,10 +55,19 @@ var (
4955
},
5056
[]string{"username"},
5157
)
58+
59+
authenticatedAttemptsCounter = metrics.NewCounterVec(
60+
&metrics.CounterOpts{
61+
Name: "authentication_attempts",
62+
Help: "Counter of authenticated attempts.",
63+
},
64+
[]string{"result"},
65+
)
5266
)
5367

5468
func init() {
5569
legacyregistry.MustRegister(authenticatedUserCounter)
70+
legacyregistry.MustRegister(authenticatedAttemptsCounter)
5671
}
5772

5873
// 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
7287
if err != nil || !ok {
7388
if err != nil {
7489
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()
7593
}
94+
7695
failed.ServeHTTP(w, req)
7796
return
7897
}
@@ -86,6 +105,7 @@ func WithAuthentication(handler http.Handler, auth authenticator.Request, failed
86105
req = req.WithContext(genericapirequest.WithUser(req.Context(), resp.User))
87106

88107
authenticatedUserCounter.WithLabelValues(compressUsername(resp.User.GetName())).Inc()
108+
authenticatedAttemptsCounter.WithLabelValues(successLabel).Inc()
89109

90110
handler.ServeHTTP(w, req)
91111
})

0 commit comments

Comments
 (0)