Skip to content

Commit

Permalink
Add a method label to the in-flight HTTP requests
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Camata <[email protected]>
  • Loading branch information
douglascamata authored Jun 30, 2022
1 parent 67edcf2 commit 32503f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions pkg/extprom/http/instrument_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion pkg/extprom/http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
),
}
}

0 comments on commit 32503f5

Please sign in to comment.