Skip to content

Commit 6315e8e

Browse files
authored
feat(auth): emit metrics with token index (#871)
1 parent 06880a1 commit 6315e8e

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

devenv/docker/go-build/docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ services:
4949
environment:
5050
TRACING_ENDPOINT: http://tempo:4318/v1/traces
5151
LOG_LEVEL: debug
52+
AUTH_TOKEN: one,two,three,four,five
5253
command:
5354
- server
5455
# 1 GiB
@@ -91,6 +92,7 @@ services:
9192
environment:
9293
GF_RENDERING_SERVER_URL: http://renderer:8081/render
9394
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
95+
GF_RENDERING_RENDERER_TOKEN: three
9496
GF_TRACING_OPENTELEMETRY_OTLP_ADDRESS: tempo:4317
9597
GF_INSTALL_PLUGINS: https://storage.googleapis.com/integration-artifacts/grafana-exploretraces-app/grafana-exploretraces-app-latest.zip;grafana-traces-app
9698
GF_LOG_FILTERS: rendering:debug

pkg/api/middleware/auth.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package middleware
22

33
import (
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.
1623
func 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

pkg/metrics/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func NewRegistry() *prometheus.Registry {
1616
collectors.NewBuildInfoCollector(),
1717

1818
middleware.MetricAuthenticatedRequestAttempt,
19+
middleware.MetricAuthenticationTokenUsage,
1920
middleware.MetricRateLimiterSlots,
2021
middleware.MetricRateLimiterRequests,
2122
middleware.MetricRequestsInFlight,

0 commit comments

Comments
 (0)