-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
47 lines (36 loc) · 946 Bytes
/
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
package http
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
handlerDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_handlers_duration_seconds",
Help: "Handlers request duration in seconds",
}, []string{"path"})
)
func init() {
prometheus.MustRegister(handlerDuration)
}
func Metrics(path string, fn Handler) Handler {
return func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
fn(w, r)
handlerDuration.WithLabelValues(path).Observe(time.Since(now).Seconds())
}
}
func NewMetricsServer(healthCheckFn Endpoint) *http.Server {
s := &http.Server{}
r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/health", Json(healthCheckFn)).Methods("GET")
s.Handler = r
s.Addr = ":10101"
go func() {
s.ListenAndServe()
}()
return s
}