2022-05-03 07:48:02 -05:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-03 10:46:07 +01:00
|
|
|
"context"
|
2022-05-03 07:48:02 -05:00
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-01-08 15:24:11 +04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2022-08-29 17:00:52 -05:00
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/tracing"
|
2022-05-03 07:48:02 -05:00
|
|
|
)
|
|
|
|
|
|
2026-06-03 10:46:07 +01:00
|
|
|
// WSMetrics groups all WebSocket-related Prometheus metrics so they
|
|
|
|
|
// can be created once and shared between the HTTP middleware and the
|
|
|
|
|
// WSWatcher probe recorder.
|
|
|
|
|
type WSMetrics struct {
|
|
|
|
|
Concurrent *prometheus.GaugeVec
|
|
|
|
|
Durations *prometheus.HistogramVec
|
|
|
|
|
Probes *prometheus.CounterVec
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewWSMetrics registers and returns WebSocket metrics. The returned
|
|
|
|
|
// struct is safe to pass to both Prometheus() and
|
|
|
|
|
// WSMetrics.RecordProbe.
|
|
|
|
|
func NewWSMetrics(reg prometheus.Registerer) *WSMetrics {
|
|
|
|
|
factory := promauto.With(reg)
|
|
|
|
|
return &WSMetrics{
|
|
|
|
|
Concurrent: factory.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "concurrent_websockets",
|
|
|
|
|
Help: "The total number of concurrent API websockets.",
|
|
|
|
|
}, []string{"path"}),
|
|
|
|
|
Durations: factory.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "websocket_durations_seconds",
|
|
|
|
|
Help: "Websocket duration distribution of requests in seconds.",
|
|
|
|
|
Buckets: []float64{
|
|
|
|
|
0.001, // 1ms
|
|
|
|
|
1,
|
|
|
|
|
60, // 1 minute
|
|
|
|
|
60 * 60, // 1 hour
|
|
|
|
|
60 * 60 * 15, // 15 hours
|
|
|
|
|
60 * 60 * 30, // 30 hours
|
|
|
|
|
},
|
|
|
|
|
}, []string{"path"}),
|
|
|
|
|
Probes: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "websocket_probes_total",
|
|
|
|
|
Help: "WebSocket liveness probe outcomes by route. " +
|
|
|
|
|
"Compare rate(...{result=\"ok\"}[1m]) against " +
|
|
|
|
|
"coderd_api_concurrent_websockets to detect " +
|
|
|
|
|
"unresponsive WebSocket connections.",
|
|
|
|
|
}, []string{"path", "result"}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RecordProbe records a single liveness probe outcome. It extracts
|
|
|
|
|
// the HTTP route from ctx via ExtractHTTPRoute.
|
|
|
|
|
func (m *WSMetrics) RecordProbe(ctx context.Context, r httpapi.ProbeResult) {
|
|
|
|
|
m.Probes.WithLabelValues(ExtractHTTPRoute(ctx), string(r)).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Prometheus(register prometheus.Registerer, ws *WSMetrics) func(http.Handler) http.Handler {
|
|
|
|
|
if ws == nil {
|
|
|
|
|
panic("developer error: WSMetrics is nil")
|
|
|
|
|
}
|
2022-08-08 10:09:46 -05:00
|
|
|
factory := promauto.With(register)
|
|
|
|
|
requestsProcessed := factory.NewCounterVec(prometheus.CounterOpts{
|
2022-05-03 07:48:02 -05:00
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "requests_processed_total",
|
|
|
|
|
Help: "The total number of processed API requests",
|
|
|
|
|
}, []string{"code", "method", "path"})
|
2025-04-16 11:10:39 +02:00
|
|
|
requestsConcurrent := factory.NewGaugeVec(prometheus.GaugeOpts{
|
2022-05-03 07:48:02 -05:00
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "concurrent_requests",
|
2023-01-13 11:15:25 -06:00
|
|
|
Help: "The number of concurrent API requests.",
|
2025-04-16 11:10:39 +02:00
|
|
|
}, []string{"method", "path"})
|
2022-08-08 10:09:46 -05:00
|
|
|
requestsDist := factory.NewHistogramVec(prometheus.HistogramOpts{
|
2022-05-03 07:48:02 -05:00
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
2023-01-13 11:15:25 -06:00
|
|
|
Name: "request_latencies_seconds",
|
|
|
|
|
Help: "Latency distribution of requests in seconds.",
|
|
|
|
|
Buckets: []float64{0.001, 0.005, 0.010, 0.025, 0.050, 0.100, 0.500, 1, 5, 10, 30},
|
2022-05-03 07:48:02 -05:00
|
|
|
}, []string{"method", "path"})
|
2026-08-18 12:54:45 -07:00
|
|
|
// Series here exist only for routes that have actually rejected a body,
|
|
|
|
|
// which is what makes this readable at a glance where filtering
|
|
|
|
|
// requests_processed_total by code is not.
|
|
|
|
|
requestsTooLarge := factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: "coderd",
|
|
|
|
|
Subsystem: "api",
|
|
|
|
|
Name: "requests_too_large_total",
|
|
|
|
|
Help: "The total number of API requests answered 413, by the reason " +
|
|
|
|
|
"they were rejected. A sustained rate of reason=\"request_body\" " +
|
|
|
|
|
"on one route is more often a limit set too tight for a " +
|
|
|
|
|
"legitimate payload than an attempt to exhaust memory. " +
|
|
|
|
|
"reason=\"other\" counts 413s raised for reasons unrelated to " +
|
|
|
|
|
"the size of the request body, such as agent log storage " +
|
|
|
|
|
"overflow or an archive that is too large once expanded.",
|
|
|
|
|
}, []string{"method", "path", "reason"})
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var (
|
|
|
|
|
start = time.Now()
|
|
|
|
|
method = r.Method
|
|
|
|
|
)
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-09-16 13:32:15 -05:00
|
|
|
sw, ok := w.(*tracing.StatusWriter)
|
2022-08-08 10:09:46 -05:00
|
|
|
if !ok {
|
2022-09-16 13:32:15 -05:00
|
|
|
panic("dev error: http.ResponseWriter is not *tracing.StatusWriter")
|
2022-08-08 10:09:46 -05:00
|
|
|
}
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
var (
|
|
|
|
|
dist *prometheus.HistogramVec
|
|
|
|
|
distOpts []string
|
2026-01-15 10:26:50 +00:00
|
|
|
path = ExtractHTTPRoute(r.Context())
|
2022-08-08 10:09:46 -05:00
|
|
|
)
|
2025-04-16 11:10:39 +02:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
// We want to count WebSockets separately.
|
2022-08-29 17:00:52 -05:00
|
|
|
if httpapi.IsWebsocketUpgrade(r) {
|
2026-06-03 10:46:07 +01:00
|
|
|
ws.Concurrent.WithLabelValues(path).Inc()
|
|
|
|
|
defer ws.Concurrent.WithLabelValues(path).Dec()
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2026-06-03 10:46:07 +01:00
|
|
|
dist = ws.Durations
|
2022-08-08 10:09:46 -05:00
|
|
|
} else {
|
2025-04-16 11:10:39 +02:00
|
|
|
requestsConcurrent.WithLabelValues(method, path).Inc()
|
|
|
|
|
defer requestsConcurrent.WithLabelValues(method, path).Dec()
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
dist = requestsDist
|
|
|
|
|
distOpts = []string{method}
|
|
|
|
|
}
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2026-08-18 12:54:45 -07:00
|
|
|
// Counting by status rather than at the point of rejection reaches
|
|
|
|
|
// handlers that bound their bodies themselves to keep their own
|
|
|
|
|
// error shapes, such as aibridge. It also reaches the 413s that have
|
|
|
|
|
// nothing to do with body size, such as agent log storage overflow,
|
|
|
|
|
// so the tracker separates the two.
|
|
|
|
|
bodyLimit := &httpapi.RequestBodyLimitTracker{}
|
|
|
|
|
r = r.WithContext(httpapi.WithRequestBodyLimitTracker(r.Context(), bodyLimit))
|
|
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
next.ServeHTTP(w, r)
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
distOpts = append(distOpts, path)
|
2022-08-29 17:00:52 -05:00
|
|
|
statusStr := strconv.Itoa(sw.Status)
|
2022-05-03 07:48:02 -05:00
|
|
|
|
2022-08-08 10:09:46 -05:00
|
|
|
requestsProcessed.WithLabelValues(statusStr, method, path).Inc()
|
2023-01-13 11:15:25 -06:00
|
|
|
dist.WithLabelValues(distOpts...).Observe(time.Since(start).Seconds())
|
2026-08-18 12:54:45 -07:00
|
|
|
|
|
|
|
|
if sw.Status == http.StatusRequestEntityTooLarge {
|
|
|
|
|
reason := "other"
|
|
|
|
|
if bodyLimit.Exceeded() {
|
|
|
|
|
reason = "request_body"
|
|
|
|
|
}
|
|
|
|
|
requestsTooLarge.WithLabelValues(method, path, reason).Inc()
|
|
|
|
|
}
|
2022-08-08 10:09:46 -05:00
|
|
|
})
|
|
|
|
|
}
|
2022-05-03 07:48:02 -05:00
|
|
|
}
|