11package middleware
22
33import (
4+ "fmt"
45 "net/http"
56 "slices"
67
78 "github.com/prometheus/client_golang/prometheus"
89)
910
10- var MetricAuthenticatedRequestAttempt = prometheus .NewCounterVec (prometheus.CounterOpts {
11- Name : "http_authenticated_request_attempts_total" ,
12- Help : "Counts the attempts of authenticated requests" ,
13- }, []string {"result" })
11+ var (
12+ MetricAuthenticatedRequestAttempt = prometheus .NewCounterVec (prometheus.CounterOpts {
13+ Name : "http_authenticated_request_attempts_total" ,
14+ Help : "Counts the attempts of authenticated requests" ,
15+ }, []string {"result" })
16+ MetricAuthenticationTokenUsage = prometheus .NewCounterVec (prometheus.CounterOpts {
17+ Name : "http_authentication_token_usage_total" ,
18+ Help : "Counts how many times each authentication token is used" ,
19+ }, []string {"token_index" })
20+ )
1421
1522// RequireAuthToken demands the request has a valid X-Auth-Token header attached to it.
1623func RequireAuthToken (h http.Handler , expectedTokens ... string ) http.Handler {
@@ -25,8 +32,10 @@ func RequireAuthToken(h http.Handler, expectedTokens ...string) http.Handler {
2532 MetricAuthenticatedRequestAttempt .WithLabelValues ("missing-header" ).Inc ()
2633 return
2734 }
28- if slices .Contains (expectedTokens , token ) {
35+ tokenIdx := slices .Index (expectedTokens , token )
36+ if tokenIdx != - 1 {
2937 MetricAuthenticatedRequestAttempt .WithLabelValues ("valid-token" ).Inc ()
38+ MetricAuthenticationTokenUsage .WithLabelValues (fmt .Sprintf ("%d" , tokenIdx )).Inc ()
3039 span .End () // we don't want to track the next middleware in this span
3140 h .ServeHTTP (w , r )
3241 return
0 commit comments