2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2015-06-05 00:24:54 +03:00
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"net"
|
2014-05-07 22:08:33 +04:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-15 17:34:52 +04:00
|
|
|
var instLabels = []string{"method", "code"}
|
2014-05-07 22:08:33 +04:00
|
|
|
|
|
|
|
type nower interface {
|
|
|
|
Now() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type nowFunc func() time.Time
|
|
|
|
|
|
|
|
func (n nowFunc) Now() time.Time {
|
|
|
|
return n()
|
|
|
|
}
|
|
|
|
|
|
|
|
var now nower = nowFunc(func() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
})
|
|
|
|
|
|
|
|
func nowSeries(t ...time.Time) nower {
|
|
|
|
return nowFunc(func() time.Time {
|
|
|
|
defer func() {
|
|
|
|
t = t[1:]
|
|
|
|
}()
|
|
|
|
|
|
|
|
return t[0]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentHandler wraps the given HTTP handler for instrumentation. It
|
2015-02-19 17:34:04 +03:00
|
|
|
// registers four metric collectors (if not already done) and reports HTTP
|
2015-01-13 19:26:38 +03:00
|
|
|
// metrics to the (newly or already) registered collectors: http_requests_total
|
|
|
|
// (CounterVec), http_request_duration_microseconds (Summary),
|
|
|
|
// http_request_size_bytes (Summary), http_response_size_bytes (Summary). Each
|
|
|
|
// has a constant label named "handler" with the provided handlerName as
|
|
|
|
// value. http_requests_total is a metric vector partitioned by HTTP method
|
|
|
|
// (label name "method") and HTTP status code (label name "code").
|
2016-05-15 18:59:51 +03:00
|
|
|
//
|
|
|
|
// Note that InstrumentHandler has several issues:
|
|
|
|
//
|
|
|
|
// - It uses Summaries rather than Histograms. Summaries are not useful if
|
2016-06-27 17:26:48 +03:00
|
|
|
// aggregation across multiple instances is required.
|
2016-05-15 18:59:51 +03:00
|
|
|
//
|
2016-06-27 12:49:34 +03:00
|
|
|
// - It uses microseconds as unit, which is deprecated and should be replaced by
|
2016-06-27 17:26:48 +03:00
|
|
|
// seconds.
|
2016-05-15 18:59:51 +03:00
|
|
|
//
|
|
|
|
// - The size of the request is calculated in a separate goroutine. Since this
|
2016-06-27 17:26:48 +03:00
|
|
|
// calculator requires access to the request header, it creates a race with
|
|
|
|
// any writes to the header performed during request handling.
|
|
|
|
// httputil.ReverseProxy is a prominent example for a handler
|
|
|
|
// performing such writes.
|
2016-05-15 18:59:51 +03:00
|
|
|
//
|
|
|
|
// Upcoming versions of this package will provide ways of instrumenting HTTP
|
|
|
|
// handlers that are more flexible and have fewer issues. Consider this function
|
|
|
|
// DEPRECATED and prefer direct instrumentation in the meantime.
|
2014-05-07 22:08:33 +04:00
|
|
|
func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
|
2014-06-20 22:40:48 +04:00
|
|
|
return InstrumentHandlerFunc(handlerName, handler.ServeHTTP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentHandlerFunc wraps the given function for instrumentation. It
|
2016-05-15 18:59:51 +03:00
|
|
|
// otherwise works in the same way as InstrumentHandler (and shares the same
|
|
|
|
// issues).
|
2014-06-20 22:40:48 +04:00
|
|
|
func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
2014-07-15 17:34:52 +04:00
|
|
|
return InstrumentHandlerFuncWithOpts(
|
|
|
|
SummaryOpts{
|
|
|
|
Subsystem: "http",
|
|
|
|
ConstLabels: Labels{"handler": handlerName},
|
|
|
|
},
|
|
|
|
handlerFunc,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-05-15 18:59:51 +03:00
|
|
|
// InstrumentHandlerWithOpts works like InstrumentHandler (and shares the same
|
|
|
|
// issues) but provides more flexibility (at the cost of a more complex call
|
|
|
|
// syntax). As InstrumentHandler, this function registers four metric
|
|
|
|
// collectors, but it uses the provided SummaryOpts to create them. However, the
|
|
|
|
// fields "Name" and "Help" in the SummaryOpts are ignored. "Name" is replaced
|
|
|
|
// by "requests_total", "request_duration_microseconds", "request_size_bytes",
|
|
|
|
// and "response_size_bytes", respectively. "Help" is replaced by an appropriate
|
2015-01-13 19:26:38 +03:00
|
|
|
// help string. The names of the variable labels of the http_requests_total
|
|
|
|
// CounterVec are "method" (get, post, etc.), and "code" (HTTP status code).
|
2014-07-15 17:34:52 +04:00
|
|
|
//
|
|
|
|
// If InstrumentHandlerWithOpts is called as follows, it mimics exactly the
|
|
|
|
// behavior of InstrumentHandler:
|
|
|
|
//
|
|
|
|
// prometheus.InstrumentHandlerWithOpts(
|
|
|
|
// prometheus.SummaryOpts{
|
|
|
|
// Subsystem: "http",
|
|
|
|
// ConstLabels: prometheus.Labels{"handler": handlerName},
|
|
|
|
// },
|
|
|
|
// handler,
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// Technical detail: "requests_total" is a CounterVec, not a SummaryVec, so it
|
|
|
|
// cannot use SummaryOpts. Instead, a CounterOpts struct is created internally,
|
|
|
|
// and all its fields are set to the equally named fields in the provided
|
|
|
|
// SummaryOpts.
|
|
|
|
func InstrumentHandlerWithOpts(opts SummaryOpts, handler http.Handler) http.HandlerFunc {
|
|
|
|
return InstrumentHandlerFuncWithOpts(opts, handler.ServeHTTP)
|
|
|
|
}
|
|
|
|
|
2016-05-15 18:59:51 +03:00
|
|
|
// InstrumentHandlerFuncWithOpts works like InstrumentHandlerFunc (and shares
|
|
|
|
// the same issues) but provides more flexibility (at the cost of a more complex
|
|
|
|
// call syntax). See InstrumentHandlerWithOpts for details how the provided
|
|
|
|
// SummaryOpts are used.
|
2014-07-15 17:34:52 +04:00
|
|
|
func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
reqCnt := NewCounterVec(
|
|
|
|
CounterOpts{
|
|
|
|
Namespace: opts.Namespace,
|
|
|
|
Subsystem: opts.Subsystem,
|
|
|
|
Name: "requests_total",
|
|
|
|
Help: "Total number of HTTP requests made.",
|
|
|
|
ConstLabels: opts.ConstLabels,
|
|
|
|
},
|
|
|
|
instLabels,
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.Name = "request_duration_microseconds"
|
|
|
|
opts.Help = "The HTTP request latencies in microseconds."
|
2015-01-13 16:57:37 +03:00
|
|
|
reqDur := NewSummary(opts)
|
2014-07-15 17:34:52 +04:00
|
|
|
|
|
|
|
opts.Name = "request_size_bytes"
|
|
|
|
opts.Help = "The HTTP request sizes in bytes."
|
2015-01-13 16:57:37 +03:00
|
|
|
reqSz := NewSummary(opts)
|
2014-07-15 17:34:52 +04:00
|
|
|
|
|
|
|
opts.Name = "response_size_bytes"
|
|
|
|
opts.Help = "The HTTP response sizes in bytes."
|
2015-01-13 16:57:37 +03:00
|
|
|
resSz := NewSummary(opts)
|
2014-07-15 17:34:52 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec)
|
2015-01-13 16:57:37 +03:00
|
|
|
regReqDur := MustRegisterOrGet(reqDur).(Summary)
|
|
|
|
regReqSz := MustRegisterOrGet(reqSz).(Summary)
|
|
|
|
regResSz := MustRegisterOrGet(resSz).(Summary)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
delegate := &responseWriterDelegator{ResponseWriter: w}
|
|
|
|
out := make(chan int)
|
2014-10-08 21:01:24 +04:00
|
|
|
urlLen := 0
|
|
|
|
if r.URL != nil {
|
|
|
|
urlLen = len(r.URL.String())
|
|
|
|
}
|
|
|
|
go computeApproximateRequestSize(r, out, urlLen)
|
2015-06-05 00:24:54 +03:00
|
|
|
|
|
|
|
_, cn := w.(http.CloseNotifier)
|
|
|
|
_, fl := w.(http.Flusher)
|
|
|
|
_, hj := w.(http.Hijacker)
|
|
|
|
_, rf := w.(io.ReaderFrom)
|
|
|
|
var rw http.ResponseWriter
|
|
|
|
if cn && fl && hj && rf {
|
|
|
|
rw = &fancyResponseWriterDelegator{delegate}
|
|
|
|
} else {
|
|
|
|
rw = delegate
|
|
|
|
}
|
|
|
|
handlerFunc(rw, r)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
2014-06-20 22:40:48 +04:00
|
|
|
elapsed := float64(time.Since(now)) / float64(time.Microsecond)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
|
|
|
method := sanitizeMethod(r.Method)
|
|
|
|
code := sanitizeCode(delegate.status)
|
2014-07-15 17:34:52 +04:00
|
|
|
regReqCnt.WithLabelValues(method, code).Inc()
|
2015-01-13 16:57:37 +03:00
|
|
|
regReqDur.Observe(elapsed)
|
|
|
|
regResSz.Observe(float64(delegate.written))
|
|
|
|
regReqSz.Observe(float64(<-out))
|
2014-05-07 22:08:33 +04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-08 21:01:24 +04:00
|
|
|
func computeApproximateRequestSize(r *http.Request, out chan int, s int) {
|
|
|
|
s += len(r.Method)
|
2014-05-07 22:08:33 +04:00
|
|
|
s += len(r.Proto)
|
|
|
|
for name, values := range r.Header {
|
|
|
|
s += len(name)
|
|
|
|
for _, value := range values {
|
|
|
|
s += len(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s += len(r.Host)
|
|
|
|
|
|
|
|
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
|
|
|
|
|
|
|
|
if r.ContentLength != -1 {
|
|
|
|
s += int(r.ContentLength)
|
|
|
|
}
|
|
|
|
out <- s
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseWriterDelegator struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
|
|
|
|
handler, method string
|
|
|
|
status int
|
2015-06-05 00:14:37 +03:00
|
|
|
written int64
|
2014-05-07 22:08:33 +04:00
|
|
|
wroteHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseWriterDelegator) WriteHeader(code int) {
|
|
|
|
r.status = code
|
|
|
|
r.wroteHeader = true
|
|
|
|
r.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseWriterDelegator) Write(b []byte) (int, error) {
|
|
|
|
if !r.wroteHeader {
|
|
|
|
r.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
n, err := r.ResponseWriter.Write(b)
|
2015-06-05 00:14:37 +03:00
|
|
|
r.written += int64(n)
|
2014-05-07 22:08:33 +04:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2015-06-05 00:24:54 +03:00
|
|
|
type fancyResponseWriterDelegator struct {
|
|
|
|
*responseWriterDelegator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fancyResponseWriterDelegator) CloseNotify() <-chan bool {
|
|
|
|
return f.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fancyResponseWriterDelegator) Flush() {
|
|
|
|
f.ResponseWriter.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fancyResponseWriterDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return f.ResponseWriter.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fancyResponseWriterDelegator) ReadFrom(r io.Reader) (int64, error) {
|
|
|
|
if !f.wroteHeader {
|
|
|
|
f.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
n, err := f.ResponseWriter.(io.ReaderFrom).ReadFrom(r)
|
|
|
|
f.written += n
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func sanitizeMethod(m string) string {
|
|
|
|
switch m {
|
|
|
|
case "GET", "get":
|
|
|
|
return "get"
|
|
|
|
case "PUT", "put":
|
|
|
|
return "put"
|
|
|
|
case "HEAD", "head":
|
|
|
|
return "head"
|
|
|
|
case "POST", "post":
|
|
|
|
return "post"
|
|
|
|
case "DELETE", "delete":
|
|
|
|
return "delete"
|
|
|
|
case "CONNECT", "connect":
|
|
|
|
return "connect"
|
|
|
|
case "OPTIONS", "options":
|
|
|
|
return "options"
|
|
|
|
case "NOTIFY", "notify":
|
|
|
|
return "notify"
|
|
|
|
default:
|
|
|
|
return strings.ToLower(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizeCode(s int) string {
|
|
|
|
switch s {
|
|
|
|
case 100:
|
|
|
|
return "100"
|
|
|
|
case 101:
|
|
|
|
return "101"
|
|
|
|
|
|
|
|
case 200:
|
|
|
|
return "200"
|
|
|
|
case 201:
|
|
|
|
return "201"
|
|
|
|
case 202:
|
|
|
|
return "202"
|
|
|
|
case 203:
|
|
|
|
return "203"
|
|
|
|
case 204:
|
|
|
|
return "204"
|
|
|
|
case 205:
|
|
|
|
return "205"
|
|
|
|
case 206:
|
|
|
|
return "206"
|
|
|
|
|
|
|
|
case 300:
|
|
|
|
return "300"
|
|
|
|
case 301:
|
|
|
|
return "301"
|
|
|
|
case 302:
|
|
|
|
return "302"
|
|
|
|
case 304:
|
|
|
|
return "304"
|
|
|
|
case 305:
|
|
|
|
return "305"
|
|
|
|
case 307:
|
|
|
|
return "307"
|
|
|
|
|
|
|
|
case 400:
|
|
|
|
return "400"
|
|
|
|
case 401:
|
|
|
|
return "401"
|
|
|
|
case 402:
|
|
|
|
return "402"
|
|
|
|
case 403:
|
|
|
|
return "403"
|
|
|
|
case 404:
|
|
|
|
return "404"
|
|
|
|
case 405:
|
|
|
|
return "405"
|
|
|
|
case 406:
|
|
|
|
return "406"
|
|
|
|
case 407:
|
|
|
|
return "407"
|
|
|
|
case 408:
|
|
|
|
return "408"
|
|
|
|
case 409:
|
|
|
|
return "409"
|
|
|
|
case 410:
|
|
|
|
return "410"
|
|
|
|
case 411:
|
|
|
|
return "411"
|
|
|
|
case 412:
|
|
|
|
return "412"
|
|
|
|
case 413:
|
|
|
|
return "413"
|
|
|
|
case 414:
|
|
|
|
return "414"
|
|
|
|
case 415:
|
|
|
|
return "415"
|
|
|
|
case 416:
|
|
|
|
return "416"
|
|
|
|
case 417:
|
|
|
|
return "417"
|
|
|
|
case 418:
|
|
|
|
return "418"
|
|
|
|
|
|
|
|
case 500:
|
|
|
|
return "500"
|
|
|
|
case 501:
|
|
|
|
return "501"
|
|
|
|
case 502:
|
|
|
|
return "502"
|
|
|
|
case 503:
|
|
|
|
return "503"
|
|
|
|
case 504:
|
|
|
|
return "504"
|
|
|
|
case 505:
|
|
|
|
return "505"
|
|
|
|
|
|
|
|
case 428:
|
|
|
|
return "428"
|
|
|
|
case 429:
|
|
|
|
return "429"
|
|
|
|
case 431:
|
|
|
|
return "431"
|
|
|
|
case 511:
|
|
|
|
return "511"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return strconv.Itoa(s)
|
|
|
|
}
|
|
|
|
}
|