Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pkg/metricshandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"kubeops.dev/ui-server/pkg/metricsstore"

"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server/mux"
"k8s.io/klog/v2"
"k8s.io/kube-state-metrics/v2/pkg/metric"
Expand Down Expand Up @@ -92,25 +93,27 @@ func (h *MetricsHandler) Install(c *mux.PathRecorderMux) {
func StartMetricsCollector(mgr manager.Manager) func(ctx context.Context) error {
return func(ctx context.Context) error {
klog.Infoln("Starts the Metrics Collector")
for {
// Recollect every MetricsRefreshPeriod until the manager shuts down. Using
// wait.UntilWithContext ensures we honor ctx cancellation and always wait a
// full period between runs, even when a collection fails (a bare "continue"
// here previously busy-looped on persistent errors).
wait.UntilWithContext(ctx, func(ctx context.Context) {
collector := &Collector{
kc: mgr.GetClient(),
opaInstalled: graph.OPAInstalled.Load(),
scannerInstalled: graph.ScannerInstalled.Load(),
}
collector.init()
err := collector.collectMetrics()
if err != nil {
if err := collector.collectMetrics(); err != nil {
klog.Errorf("Error occurred while collecting metrics : %s \n", err.Error())
continue
return
}

mu.Lock()
store = collector.store
mu.Unlock()

time.Sleep(MetricsRefreshPeriod)
}
}, MetricsRefreshPeriod)
return nil
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/metricsstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (s *MetricsStore) Add(family ...*metric.Family) {
// help text of each metric family.
func (s *MetricsStore) WriteAll(w io.Writer) error {
for i, help := range s.headers {
// Defensive: headers and families are expected to be the same length (one
// family per header), but guard against a short families slice so a partially
// populated store cannot panic the /metrics handler.
if i >= len(s.families) {
break
}
_, err := w.Write([]byte(help))
if err != nil {
return err
Expand Down
Loading