diff --git a/pkg/extprom/http/instrument_server.go b/pkg/extprom/http/instrument_server.go index 3a98f87c69..5af2e60dbc 100644 --- a/pkg/extprom/http/instrument_server.go +++ b/pkg/extprom/http/instrument_server.go @@ -61,8 +61,8 @@ func (ins *defaultInstrumentationMiddleware) NewHandler(handlerName string, hand func httpInstrumentationHandler(baseLabels prometheus.Labels, metrics *defaultMetrics, next http.Handler) http.HandlerFunc { return promhttp.InstrumentHandlerRequestSize( metrics.requestSize.MustCurryWith(baseLabels), - promhttp.InstrumentHandlerInFlight( - metrics.inflightHTTPRequests.With(baseLabels), + instrumentHandlerInFlight( + metrics.inflightHTTPRequests.MustCurryWith(baseLabels), promhttp.InstrumentHandlerCounter( metrics.requestsTotal.MustCurryWith(baseLabels), promhttp.InstrumentHandlerResponseSize( @@ -129,11 +129,16 @@ func (wd *responseWriterDelegator) Status() string { return fmt.Sprintf("%d", wd.StatusCode()) } -// NewInstrumentHandlerInflightTenant creates a middleware used to export the current amount of concurrent requests -// being handled. It has an optional tenant label whenever a tenant is present in the context. -func NewInstrumentHandlerInflightTenant(gaugeVec *prometheus.GaugeVec, tenantHeader string, next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - tenant := r.Header.Get(tenantHeader) - promhttp.InstrumentHandlerInFlight(gaugeVec.With(prometheus.Labels{"tenant": tenant}), next).ServeHTTP(w, r) - } +// instrumentHandlerInFlight is responsible for counting the amount of +// in-flight HTTP requests (requests being processed by the handler) at a given +// moment in time. +// This is used instead of prometheus/client_golang/promhttp.InstrumentHandlerInFlight +// to be able to have the HTTP method as a label. +func instrumentHandlerInFlight(vec *prometheus.GaugeVec, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gauge := vec.With(prometheus.Labels{"method": r.Method}) + gauge.Inc() + defer gauge.Dec() + next.ServeHTTP(w, r) + }) } diff --git a/pkg/extprom/http/metrics.go b/pkg/extprom/http/metrics.go index 65ee44859a..ef6269cefd 100644 --- a/pkg/extprom/http/metrics.go +++ b/pkg/extprom/http/metrics.go @@ -60,7 +60,7 @@ func newDefaultMetrics(reg prometheus.Registerer, buckets []float64, extraLabels Name: "http_inflight_requests", Help: "Current number of HTTP requests the handler is responding to.", }, - append([]string{"handler"}, extraLabels...), + append([]string{"handler", "method"}, extraLabels...), ), } }