From 606b3fbbf5a5a11ef88fbdaa9d9b711bcc7987a2 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 2 Jun 2020 20:08:03 +0300 Subject: [PATCH] Add a metric "http_requests_in_flight" --- imageproxy.go | 7 ++++++- metrics.go | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/imageproxy.go b/imageproxy.go index 9839201..3da57a2 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -223,7 +223,12 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { } timer := prometheus.NewTimer(metricRequestDuration) - defer timer.ObserveDuration() + metricRequestsInFlight.Inc() + defer func() { + timer.ObserveDuration() + metricRequestsInFlight.Dec() + }() + h.ServeHTTP(w, r) } diff --git a/metrics.go b/metrics.go index 0466ac0..1351208 100644 --- a/metrics.go +++ b/metrics.go @@ -29,6 +29,11 @@ var ( Name: "request_duration_seconds", Help: "Request response times", }) + metricRequestsInFlight = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: "http", + Name: "requests_in_flight", + Help: "Number of requests in flight", + }) ) func init() { @@ -36,4 +41,5 @@ func init() { prometheus.MustRegister(metricServedFromCache) prometheus.MustRegister(metricRemoteErrors) prometheus.MustRegister(metricRequestDuration) + prometheus.MustRegister(metricRequestsInFlight) }