1 // Copyright 2017 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
23 dto "github.com/prometheus/client_model/go"
25 "github.com/prometheus/client_golang/prometheus"
28 // magicString is used for the hacky label test in checkLabels. Remove once fixed.
29 const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
31 // InstrumentHandlerInFlight is a middleware that wraps the provided
32 // http.Handler. It sets the provided prometheus.Gauge to the number of
33 // requests currently handled by the wrapped http.Handler.
35 // See the example for InstrumentHandlerDuration for example usage.
36 func InstrumentHandlerInFlight(g prometheus.Gauge, next http.Handler) http.Handler {
37 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44 // InstrumentHandlerDuration is a middleware that wraps the provided
45 // http.Handler to observe the request duration with the provided ObserverVec.
46 // The ObserverVec must have zero, one, or two non-const non-curried labels. For
47 // those, the only allowed label names are "code" and "method". The function
48 // panics otherwise. The Observe method of the Observer in the ObserverVec is
49 // called with the request duration in seconds. Partitioning happens by HTTP
50 // status code and/or HTTP method if the respective instance label names are
51 // present in the ObserverVec. For unpartitioned observations, use an
52 // ObserverVec with zero labels. Note that partitioning of Histograms is
53 // expensive and should be used judiciously.
55 // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
57 // If the wrapped Handler panics, no values are reported.
59 // Note that this method is only guaranteed to never observe negative durations
60 // if used with Go1.9+.
61 func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc {
62 code, method := checkLabels(obs)
65 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67 d := newDelegator(w, nil)
70 obs.With(labels(code, method, r.Method, d.Status())).Observe(time.Since(now).Seconds())
74 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
77 obs.With(labels(code, method, r.Method, 0)).Observe(time.Since(now).Seconds())
81 // InstrumentHandlerCounter is a middleware that wraps the provided http.Handler
82 // to observe the request result with the provided CounterVec. The CounterVec
83 // must have zero, one, or two non-const non-curried labels. For those, the only
84 // allowed label names are "code" and "method". The function panics
85 // otherwise. Partitioning of the CounterVec happens by HTTP status code and/or
86 // HTTP method if the respective instance label names are present in the
87 // CounterVec. For unpartitioned counting, use a CounterVec with zero labels.
89 // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
91 // If the wrapped Handler panics, the Counter is not incremented.
93 // See the example for InstrumentHandlerDuration for example usage.
94 func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) http.HandlerFunc {
95 code, method := checkLabels(counter)
98 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
99 d := newDelegator(w, nil)
101 counter.With(labels(code, method, r.Method, d.Status())).Inc()
105 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107 counter.With(labels(code, method, r.Method, 0)).Inc()
111 // InstrumentHandlerTimeToWriteHeader is a middleware that wraps the provided
112 // http.Handler to observe with the provided ObserverVec the request duration
113 // until the response headers are written. The ObserverVec must have zero, one,
114 // or two non-const non-curried labels. For those, the only allowed label names
115 // are "code" and "method". The function panics otherwise. The Observe method of
116 // the Observer in the ObserverVec is called with the request duration in
117 // seconds. Partitioning happens by HTTP status code and/or HTTP method if the
118 // respective instance label names are present in the ObserverVec. For
119 // unpartitioned observations, use an ObserverVec with zero labels. Note that
120 // partitioning of Histograms is expensive and should be used judiciously.
122 // If the wrapped Handler panics before calling WriteHeader, no value is
125 // Note that this method is only guaranteed to never observe negative durations
126 // if used with Go1.9+.
128 // See the example for InstrumentHandlerDuration for example usage.
129 func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc {
130 code, method := checkLabels(obs)
132 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
134 d := newDelegator(w, func(status int) {
135 obs.With(labels(code, method, r.Method, status)).Observe(time.Since(now).Seconds())
141 // InstrumentHandlerRequestSize is a middleware that wraps the provided
142 // http.Handler to observe the request size with the provided ObserverVec. The
143 // ObserverVec must have zero, one, or two non-const non-curried labels. For
144 // those, the only allowed label names are "code" and "method". The function
145 // panics otherwise. The Observe method of the Observer in the ObserverVec is
146 // called with the request size in bytes. Partitioning happens by HTTP status
147 // code and/or HTTP method if the respective instance label names are present in
148 // the ObserverVec. For unpartitioned observations, use an ObserverVec with zero
149 // labels. Note that partitioning of Histograms is expensive and should be used
152 // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
154 // If the wrapped Handler panics, no values are reported.
156 // See the example for InstrumentHandlerDuration for example usage.
157 func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc {
158 code, method := checkLabels(obs)
161 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162 d := newDelegator(w, nil)
164 size := computeApproximateRequestSize(r)
165 obs.With(labels(code, method, r.Method, d.Status())).Observe(float64(size))
169 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
171 size := computeApproximateRequestSize(r)
172 obs.With(labels(code, method, r.Method, 0)).Observe(float64(size))
176 // InstrumentHandlerResponseSize is a middleware that wraps the provided
177 // http.Handler to observe the response size with the provided ObserverVec. The
178 // ObserverVec must have zero, one, or two non-const non-curried labels. For
179 // those, the only allowed label names are "code" and "method". The function
180 // panics otherwise. The Observe method of the Observer in the ObserverVec is
181 // called with the response size in bytes. Partitioning happens by HTTP status
182 // code and/or HTTP method if the respective instance label names are present in
183 // the ObserverVec. For unpartitioned observations, use an ObserverVec with zero
184 // labels. Note that partitioning of Histograms is expensive and should be used
187 // If the wrapped Handler does not set a status code, a status code of 200 is assumed.
189 // If the wrapped Handler panics, no values are reported.
191 // See the example for InstrumentHandlerDuration for example usage.
192 func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler) http.Handler {
193 code, method := checkLabels(obs)
194 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
195 d := newDelegator(w, nil)
197 obs.With(labels(code, method, r.Method, d.Status())).Observe(float64(d.Written()))
201 func checkLabels(c prometheus.Collector) (code bool, method bool) {
202 // TODO(beorn7): Remove this hacky way to check for instance labels
203 // once Descriptors can have their dimensionality queried.
205 desc *prometheus.Desc
211 // Get the Desc from the Collector.
212 descc := make(chan *prometheus.Desc, 1)
218 panic("no description provided by collector")
222 panic("more than one description provided by collector")
228 // Create a ConstMetric with the Desc. Since we don't know how many
229 // variable labels there are, try for as long as it needs.
230 for err := errors.New("dummy"); err != nil; lvs = append(lvs, magicString) {
231 m, err = prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, lvs...)
234 // Write out the metric into a proto message and look at the labels.
235 // If the value is not the magicString, it is a constLabel, which doesn't interest us.
236 // If the label is curried, it doesn't interest us.
237 // In all other cases, only "code" or "method" is allowed.
238 if err := m.Write(&pm); err != nil {
239 panic("error checking metric for labels")
241 for _, label := range pm.Label {
242 name, value := label.GetName(), label.GetValue()
243 if value != magicString || isLabelCurried(c, name) {
252 panic("metric partitioned with non-supported labels")
258 func isLabelCurried(c prometheus.Collector, label string) bool {
259 // This is even hackier than the label test above.
260 // We essentially try to curry again and see if it works.
261 // But for that, we need to type-convert to the two
262 // types we use here, ObserverVec or *CounterVec.
263 switch v := c.(type) {
264 case *prometheus.CounterVec:
265 if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
268 case prometheus.ObserverVec:
269 if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
273 panic("unsupported metric vec type")
278 // emptyLabels is a one-time allocation for non-partitioned metrics to avoid
279 // unnecessary allocations on each request.
280 var emptyLabels = prometheus.Labels{}
282 func labels(code, method bool, reqMethod string, status int) prometheus.Labels {
283 if !(code || method) {
286 labels := prometheus.Labels{}
289 labels["code"] = sanitizeCode(status)
292 labels["method"] = sanitizeMethod(reqMethod)
298 func computeApproximateRequestSize(r *http.Request) int {
301 s += len(r.URL.String())
306 for name, values := range r.Header {
308 for _, value := range values {
314 // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
316 if r.ContentLength != -1 {
317 s += int(r.ContentLength)
322 func sanitizeMethod(m string) string {
332 case "DELETE", "delete":
334 case "CONNECT", "connect":
336 case "OPTIONS", "options":
338 case "NOTIFY", "notify":
341 return strings.ToLower(m)
345 // If the wrapped http.Handler has not set a status code, i.e. the value is
346 // currently 0, santizeCode will return 200, for consistency with behavior in
348 func sanitizeCode(s int) string {
445 return strconv.Itoa(s)