1+ // Package exp provides Prometheus metrics registration for the auth proxy.
12package exp
23
34import (
5+ "net/http"
6+ "strconv"
47 "time"
58
69 "github.com/prometheus/client_golang/prometheus"
@@ -41,13 +44,23 @@ func (c *CacheCollector) Collect(metrics chan<- prometheus.Metric) {
4144 }
4245}
4346
47+ // HTTP request event labels for authproxy_http_requests_total (see warmHTTPMetrics).
48+ const (
49+ HTTPEventTotal = "total"
50+ HTTPEventDelete = "delete"
51+ HTTPEventXServer = "x_server"
52+ HTTPEventInvalidKey = "invalid_key"
53+ )
54+
4455// Metrics contains the exported prometheus metrics used by the application.
4556type Metrics struct {
4657 QueryErrors * prometheus.CounterVec
4758 QueryMissing * prometheus.CounterVec
4859 QueryTime * prometheus.HistogramVec
4960 ReqTime * prometheus.HistogramVec
5061 Uptime prometheus.CounterFunc
62+ HTTPRequests * prometheus.CounterVec
63+ HTTPResponse * prometheus.CounterVec
5164}
5265
5366// GetMetrics sets up metrics on startup.
@@ -63,7 +76,7 @@ func GetMetrics(collector *CacheCollector) *Metrics {
6376 collector .gauge = prometheus .NewDesc ("authproxy_cache_gauges" , "All cache gauges" , []string {"cache" , "gauge" }, nil )
6477 prometheus .MustRegister (collector )
6578
66- return & Metrics {
79+ metrics := & Metrics {
6780 QueryErrors : promauto .NewCounterVec (prometheus.CounterOpts {
6881 Name : "authproxy_db_query_errors_total" ,
6982 Help : "The total number of DB query errors" ,
@@ -86,5 +99,47 @@ func GetMetrics(collector *CacheCollector) *Metrics {
8699 Name : "authproxy_uptime_seconds_total" ,
87100 Help : "Seconds the auth proxy has been running" ,
88101 }, func () float64 { return time .Since (start ).Seconds () }),
102+ HTTPRequests : promauto .NewCounterVec (prometheus.CounterOpts {
103+ Name : "authproxy_http_requests_total" ,
104+ Help : "HTTP request counts by event type" ,
105+ }, []string {"event" }),
106+ HTTPResponse : promauto .NewCounterVec (prometheus.CounterOpts {
107+ Name : "authproxy_http_responses_total" ,
108+ Help : "HTTP responses by status code" ,
109+ }, []string {"status_code" }),
110+ }
111+
112+ warmHTTPMetrics (metrics )
113+
114+ return metrics
115+ }
116+
117+ // warmHTTPMetrics registers label combinations up front to reduce allocations on the hot path.
118+ func warmHTTPMetrics (metrics * Metrics ) {
119+ for _ , cache := range []string {"users" , "servers" } {
120+ metrics .QueryErrors .WithLabelValues (cache )
121+ metrics .QueryMissing .WithLabelValues (cache )
122+ metrics .QueryTime .WithLabelValues (cache )
123+ metrics .ReqTime .WithLabelValues (cache )
124+ }
125+
126+ for _ , event := range []string {
127+ HTTPEventTotal ,
128+ HTTPEventDelete ,
129+ HTTPEventXServer ,
130+ HTTPEventInvalidKey ,
131+ } {
132+ metrics .HTTPRequests .WithLabelValues (event )
133+ }
134+
135+ for _ , code := range []int {
136+ http .StatusOK ,
137+ http .StatusUnauthorized ,
138+ http .StatusNotFound ,
139+ http .StatusInternalServerError ,
140+ http .StatusBadRequest ,
141+ http .StatusMethodNotAllowed ,
142+ } {
143+ metrics .HTTPResponse .WithLabelValues (strconv .Itoa (code ))
89144 }
90145}
0 commit comments