forked from openshift/insights-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
45 lines (39 loc) · 1.45 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 insights
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
)
var (
insightsMetricsRegistry *prometheus.Registry
)
func init() {
insightsMetricsRegistry = prometheus.NewRegistry()
MustRegisterMetricCollectors(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
}
// RunMetricsServer starts an HTTP server for the Insights metrics registry.
// The server will run synchronously in an infinite loop. In case of an error,
// it will be logged, and the server will be restarted after a short sleep
// (to avoid spamming the log with the same error).
func RunMetricsServer() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(insightsMetricsRegistry, promhttp.HandlerOpts{}))
for {
klog.Info("Starting the Prometheus metrics server")
klog.Errorf("Unable to serve metrics: %v", http.ListenAndServe(":8080", mux))
time.Sleep(time.Minute)
}
}
// RegisterMetricCollector registers a new metric collector or a new metric in
// the Insights metrics registry. This function should be called from init()
// functions only, because it uses the MustRegister method, and therefore panics
// in case of an error.
func MustRegisterMetricCollectors(collectors ...prometheus.Collector) {
insightsMetricsRegistry.MustRegister(collectors...)
}