@@ -40,10 +40,11 @@ var sizeBuckets = []float64{ //nolint:gochecknoglobals // this is a global varia
40
40
// Unlike the promhttp.InstrumentHandler* chaining, this middleware creates only one delegator per request.
41
41
// It partitions the metrics by HTTP status code, HTTP method, destination host name and source IP.
42
42
type Prometheus struct {
43
- requestsTotal * prometheus.CounterVec
44
- requestDuration * prometheus.HistogramVec
45
- requestSize * prometheus.HistogramVec
46
- responseSize * prometheus.HistogramVec
43
+ requestsInFlight * prometheus.GaugeVec
44
+ requestsTotal * prometheus.CounterVec
45
+ requestDuration * prometheus.HistogramVec
46
+ requestSize * prometheus.HistogramVec
47
+ responseSize * prometheus.HistogramVec
47
48
}
48
49
49
50
func NewPrometheus (r prometheus.Registerer , namespace string ) * Prometheus {
@@ -54,6 +55,11 @@ func NewPrometheus(r prometheus.Registerer, namespace string) *Prometheus {
54
55
l := []string {"code" , "method" }
55
56
56
57
return & Prometheus {
58
+ requestsInFlight : f .NewGaugeVec (prometheus.GaugeOpts {
59
+ Namespace : namespace ,
60
+ Name : "http_requests_in_flight" ,
61
+ Help : "Current number of HTTP requests being served." ,
62
+ }, []string {"method" }),
57
63
requestsTotal : f .NewCounterVec (prometheus.CounterOpts {
58
64
Namespace : namespace ,
59
65
Name : "http_requests_total" ,
@@ -82,6 +88,8 @@ func NewPrometheus(r prometheus.Registerer, namespace string) *Prometheus {
82
88
83
89
func (p * Prometheus ) Wrap (h http.Handler ) http.Handler {
84
90
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
91
+ p .requestsInFlight .WithLabelValues (r .Method ).Inc ()
92
+
85
93
d := newDelegator (w , nil )
86
94
87
95
r .Body = bodyCounter (r .Body )
@@ -100,6 +108,7 @@ func (p *Prometheus) Wrap(h http.Handler) http.Handler {
100
108
reqSize = c .Count ()
101
109
}
102
110
111
+ p .requestsInFlight .WithLabelValues (r .Method ).Dec ()
103
112
p .requestSize .WithLabelValues (lv [:]... ).Observe (float64 (reqSize ))
104
113
p .responseSize .WithLabelValues (lv [:]... ).Observe (float64 (d .Written ()))
105
114
})
@@ -115,12 +124,17 @@ func (p *Prometheus) ModifyResponse(res *http.Response) error {
115
124
return nil
116
125
}
117
126
127
+ func (p * Prometheus ) ReadRequest (req * http.Request ) {
128
+ p .requestsInFlight .WithLabelValues (req .Method ).Inc ()
129
+ }
130
+
118
131
func (p * Prometheus ) WroteResponse (res * http.Response ) {
119
132
elapsed := martian .ContextDuration (res .Request .Context ()).Seconds ()
120
133
121
134
req := res .Request
122
135
lv := [2 ]string {strconv .Itoa (res .StatusCode ), req .Method }
123
136
137
+ p .requestsInFlight .WithLabelValues (req .Method ).Dec ()
124
138
p .requestsTotal .WithLabelValues (lv [:]... ).Inc ()
125
139
p .requestDuration .WithLabelValues (lv [:]... ).Observe (elapsed )
126
140
0 commit comments