Skip to content

Commit 77ea50b

Browse files
committed
added chi route extraction and labels
1 parent 69aa365 commit 77ea50b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

coderd/httpmw/prometheus.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
2222
Name: "requests_processed_total",
2323
Help: "The total number of processed API requests",
2424
}, []string{"code", "method", "path"})
25-
requestsConcurrent := factory.NewGauge(prometheus.GaugeOpts{
25+
requestsConcurrent := factory.NewGaugeVec(prometheus.GaugeOpts{
2626
Namespace: "coderd",
2727
Subsystem: "api",
2828
Name: "concurrent_requests",
2929
Help: "The number of concurrent API requests.",
30-
})
31-
websocketsConcurrent := factory.NewGauge(prometheus.GaugeOpts{
30+
}, []string{"path"})
31+
websocketsConcurrent := factory.NewGaugeVec(prometheus.GaugeOpts{
3232
Namespace: "coderd",
3333
Subsystem: "api",
3434
Name: "concurrent_websockets",
3535
Help: "The total number of concurrent API websockets.",
36-
})
36+
}, []string{"path"})
3737
websocketsDist := factory.NewHistogramVec(prometheus.HistogramOpts{
3838
Namespace: "coderd",
3939
Subsystem: "api",
@@ -69,27 +69,28 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
6969
panic("dev error: http.ResponseWriter is not *tracing.StatusWriter")
7070
}
7171

72+
path := rctx.RoutePattern()
73+
7274
var (
7375
dist *prometheus.HistogramVec
7476
distOpts []string
7577
)
7678
// We want to count WebSockets separately.
7779
if httpapi.IsWebsocketUpgrade(r) {
78-
websocketsConcurrent.Inc()
79-
defer websocketsConcurrent.Dec()
80+
websocketsConcurrent.WithLabelValues(path).Inc()
81+
defer websocketsConcurrent.WithLabelValues(path).Dec()
8082

8183
dist = websocketsDist
8284
} else {
83-
requestsConcurrent.Inc()
84-
defer requestsConcurrent.Dec()
85+
requestsConcurrent.WithLabelValues(path).Inc()
86+
defer requestsConcurrent.WithLabelValues(path).Dec()
8587

8688
dist = requestsDist
8789
distOpts = []string{method}
8890
}
8991

9092
next.ServeHTTP(w, r)
9193

92-
path := rctx.RoutePattern()
9394
distOpts = append(distOpts, path)
9495
statusStr := strconv.Itoa(sw.Status)
9596

0 commit comments

Comments
 (0)