generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
45 lines (40 loc) · 1.11 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
package main
import (
"context"
"net/http"
"os"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var rttHist = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "ping_rtt_seconds",
Help: "Round trip time of pings",
Buckets: []float64{0.001, 0.005, 0.01, 0.05, 0.1, 1},
}, []string{"source", "target", "reason"})
func serveMetrics(ctx context.Context, addr string) error {
l := logr.FromContextOrDiscard(ctx)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
})
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
})
prom := http.Server{
Addr: addr,
}
go func() {
<-ctx.Done()
if err := prom.Shutdown(context.Background()); err != nil {
l.Error(err, "Failed to stop metrics server")
os.Exit(1)
}
}()
err := prom.ListenAndServe()
if err == http.ErrServerClosed {
return nil
}
return err
}