-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprometheus.go
More file actions
60 lines (48 loc) · 1.91 KB
/
prometheus.go
File metadata and controls
60 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package resthelpers
import (
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
requestsCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "http_request_inflight",
Help: "How many HTTP requests are currently being processed",
}, []string{"endpoint", "method", "status"})
httpRequestDurHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "The latency of the HTTP requests.",
Buckets: prometheus.ExponentialBuckets(0.05, 2, 16), // start at 50ms, double buckets 16 times (so the last bucket is 1.6s)
}, []string{"endpoint", "method"})
httpRequestSizeHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_size_bytes",
Help: "The size of the HTTP requests.",
Buckets: prometheus.ExponentialBuckets(100, 3, 16),
}, []string{"endpoint", "method"})
)
func Prometheus(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rwInterceptor := &promResponseWriterInterceptor{ResponseWriter: w, statusCode: http.StatusOK}
start := time.Now()
next.ServeHTTP(rwInterceptor, r)
path := r.URL.Path
requestsCounter.WithLabelValues(r.Method, path, strconv.Itoa(rwInterceptor.statusCode)).Inc()
httpRequestDurHistogram.WithLabelValues(path, r.Method).Observe(time.Since(start).Seconds())
httpRequestSizeHistogram.WithLabelValues(path, r.Method).Observe(float64(rwInterceptor.bytesWritten))
})
}
type promResponseWriterInterceptor struct {
http.ResponseWriter
statusCode int
bytesWritten int
}
func (w *promResponseWriterInterceptor) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *promResponseWriterInterceptor) Write(b []byte) (int, error) {
w.bytesWritten += len(b)
return w.ResponseWriter.Write(b)
}