| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package monitoring |
|
|
| import ( |
| "io" |
| "net/http" |
| "strconv" |
|
|
| "github.com/felixge/httpsnoop" |
| "github.com/prometheus/client_golang/prometheus" |
| ) |
|
|
| |
| |
|
|
| |
| |
| |
| type StaticRouteLabel func(r *http.Request) (*http.Request, string) |
|
|
| type InstrumentHandler struct { |
| inflightRequests *prometheus.GaugeVec |
| duration *prometheus.HistogramVec |
|
|
| |
| requestSize *prometheus.HistogramVec |
| responseSize *prometheus.HistogramVec |
|
|
| |
| next http.Handler |
|
|
| routeLabel StaticRouteLabel |
| } |
|
|
| func InstrumentHTTP( |
| next http.Handler, |
| routeLabel StaticRouteLabel, |
| inflight *prometheus.GaugeVec, |
| duration *prometheus.HistogramVec, |
| requestSize *prometheus.HistogramVec, |
| responseSize *prometheus.HistogramVec, |
| ) *InstrumentHandler { |
| return &InstrumentHandler{ |
| next: next, |
| routeLabel: routeLabel, |
| inflightRequests: inflight, |
| duration: duration, |
| requestSize: requestSize, |
| responseSize: responseSize, |
| } |
| } |
|
|
| func (i *InstrumentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| r, route := i.routeLabel(r) |
| method := r.Method |
|
|
| inflight := i.inflightRequests.WithLabelValues(method, route) |
| inflight.Inc() |
| defer inflight.Dec() |
|
|
| origBody := r.Body |
| defer func() { |
| |
| r.Body = origBody |
| }() |
|
|
| cr := &countingReadCloser{ |
| r: r.Body, |
| } |
| r.Body = cr |
|
|
| |
| respWithMetrics := httpsnoop.CaptureMetricsFn(w, func(rw http.ResponseWriter) { |
| i.next.ServeHTTP(rw, r) |
| }) |
|
|
| i.requestSize.WithLabelValues(method, route).Observe(float64(cr.read)) |
| i.responseSize.WithLabelValues(method, route).Observe(float64(respWithMetrics.Written)) |
|
|
| labelValues := []string{ |
| method, |
| route, |
| strconv.Itoa(respWithMetrics.Code), |
| } |
|
|
| i.duration.WithLabelValues(labelValues...).Observe(respWithMetrics.Duration.Seconds()) |
| } |
|
|
| type countingReadCloser struct { |
| r io.ReadCloser |
| read int64 |
| } |
|
|
| func (c *countingReadCloser) Read(p []byte) (int, error) { |
| n, err := c.r.Read(p) |
| if n > 0 { |
| c.read += int64(n) |
| } |
| return n, err |
| } |
|
|
| func (c *countingReadCloser) Close() error { |
| return c.r.Close() |
| } |
|
|