8000 feat: add path & method labels to prometheus metrics for current requests by ibetitsmike · Pull Request #17362 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add path & method labels to prometheus metrics for current requests #17362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added chi route extraction and labels
  • Loading branch information
ibetitsmike committed Apr 15, 2025
commit bf5b3a8517d9ea719950d206a20dff0fd5b3f6ca
19 changes: 10 additions & 9 deletions coderd/httpmw/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
Name: "requests_processed_total",
Help: "The total number of processed API requests",
}, []string{"code", "method", "path"})
requestsConcurrent := factory.NewGauge(prometheus.GaugeOpts{
requestsConcurrent := factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "api",
Name: "concurrent_requests",
Help: "The number of concurrent API requests.",
})
websocketsConcurrent := factory.NewGauge(prometheus.GaugeOpts{
}, []string{"path"})
websocketsConcurrent := factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "api",
Name: "concurrent_websockets",
Help: "The total number of concurrent API websockets.",
})
}, []string{"path"})
websocketsDist := factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "coderd",
Subsystem: "api",
Expand Down Expand Up @@ -69,27 +69,28 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
panic("dev error: http.ResponseWriter is not *tracing.StatusWriter")
}

path := rctx.RoutePattern()

var (
dist *prometheus.HistogramVec
distOpts []string
)
// We want to count WebSockets separately.
if httpapi.IsWebsocketUpgrade(r) {
websocketsConcurrent.Inc()
defer websocketsConcurrent.Dec()
websocketsConcurrent.WithLabelValues(path).Inc()
defer websocketsConcurrent.WithLabelValues(path).Dec()

dist = websocketsDist
} else {
requestsConcurrent.Inc()
defer requestsConcurrent.Dec()
requestsConcurrent.WithLabelValues(path).Inc()
defer requestsConcurrent.WithLabelValues(path).Dec()

dist = requestsDist
distOpts = []string{method}
}

next.ServeHTTP(w, r)

path := rctx.RoutePattern()
distOpts = append(distOpts, path)
statusStr := strconv.Itoa(sw.Status)

Expand Down
0