-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
112 lines (92 loc) · 2.88 KB
/
metrics.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package sqlite
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
)
var (
requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Duration of HTTP requests.",
Buckets: prometheus.DefBuckets,
},
[]string{"method", "path", "status"},
)
)
func init() {
prometheus.MustRegister(requestDuration)
}
type instrumentedResponseWriter struct {
http.ResponseWriter
statusCode int
path string
}
func (rec *instrumentedResponseWriter) WriteHeader(code int) {
rec.statusCode = code
rec.ResponseWriter.WriteHeader(code)
}
func (rec *instrumentedResponseWriter) WritePath(path string) {
rec.path = path
}
func instrumentedHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := &instrumentedResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
path := r.URL.Path
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
if rec.path != "" {
path = rec.path
}
requestDuration.WithLabelValues(r.Method, path, http.StatusText(rec.statusCode)).Observe(v)
}))
defer timer.ObserveDuration()
handler.ServeHTTP(rec, r)
})
}
type InstrumentedRouter struct {
*httprouter.Router
}
func NewInstrumentedRouter() *InstrumentedRouter {
return &InstrumentedRouter{
Router: httprouter.New(),
}
}
func (r *InstrumentedRouter) GET(path string, handle httprouter.Handle) {
r.Handle(http.MethodGet, path, handle)
}
func (r *InstrumentedRouter) HEAD(path string, handle httprouter.Handle) {
r.Handle(http.MethodHead, path, handle)
}
func (r *InstrumentedRouter) OPTIONS(path string, handle httprouter.Handle) {
r.Handle(http.MethodOptions, path, handle)
}
func (r *InstrumentedRouter) POST(path string, handle httprouter.Handle) {
r.Handle(http.MethodPost, path, handle)
}
func (r *InstrumentedRouter) PUT(path string, handle httprouter.Handle) {
r.Handle(http.MethodPut, path, handle)
}
func (r *InstrumentedRouter) PATCH(path string, handle httprouter.Handle) {
r.Handle(http.MethodPatch, path, handle)
}
func (r *InstrumentedRouter) DELETE(path string, handle httprouter.Handle) {
r.Handle(http.MethodDelete, path, handle)
}
func (r *InstrumentedRouter) Handle(method, path string, handle httprouter.Handle) {
r.Router.HandlerFunc(method, path, func(w http.ResponseWriter, r *http.Request) {
if rec, ok := w.(*instrumentedResponseWriter); ok {
rec.WritePath(path)
}
handle(w, r, httprouter.ParamsFromContext(r.Context()))
})
}
func (r *InstrumentedRouter) Handler(method, path string, handler http.Handler) {
r.Handle(method, path,
func(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
handler.ServeHTTP(w, req)
},
)
}
func (r *InstrumentedRouter) HandlerFunc(method, path string, handler http.HandlerFunc) {
r.Handler(method, path, handler)
}