-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmetrics.go
52 lines (46 loc) · 1.19 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
package metrics
import (
"net/http"
"strconv"
"sync"
"time"
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/retry"
"github.com/aws/aws-network-policy-agent/pkg/logger"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// metricsPort is the port for prometheus metrics
metricsPort = 61680
)
func ServeMetrics() {
metricsLogger := getMetricsLogger()
metricsLogger.Info("Serving metrics on ", "port", metricsPort)
server := setupMetricsServer()
for {
once := sync.Once{}
_ = retry.WithBackoff(retry.NewSimpleBackoff(time.Second, time.Minute, 0.2, 2), func() error {
err := server.ListenAndServe()
once.Do(func() {
metricsLogger.Error(err, "Error running http API: %v")
})
return err
})
}
}
func setupMetricsServer() *http.Server {
serveMux := http.NewServeMux()
serveMux.Handle("/metrics", promhttp.Handler())
server := &http.Server{
Addr: ":" + strconv.Itoa(metricsPort),
Handler: serveMux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
return server
}
func getMetricsLogger() logr.Logger {
ctrlLogger := logger.New("info", "")
return zapr.NewLogger(ctrlLogger)
}