Skip to content

Commit

Permalink
Add metrics for config file changes
Browse files Browse the repository at this point in the history
This uses code pieces from prometheus/alertmanager in https://github.com/prometheus/alertmanager/blob/main/config/coordinator.go#LL56C26-L56C26
licensed under Apache-2.0.

kube_state_metrics_config_hash 4.0061079457904e+13
kube_state_metrics_config_last_reload_success_timestamp_seconds 1.6697483049487052e+09
kube_state_metrics_config_last_reload_successful 1

Signed-off-by: Manuel Rüger <manuel@rueg.eu>
  • Loading branch information
mrueg committed Nov 29, 2022
1 parent 0cbabf9 commit 7a56756
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package app

import (
"context"
"crypto/md5" //nolint:gosec
"encoding/binary"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -99,6 +101,22 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
ConstLabels: prometheus.Labels{"handler": "metrics"},
}, []string{"method"},
)
configHash := promauto.With(ksmMetricsRegistry).NewGauge(
prometheus.GaugeOpts{
Name: "kube_state_metrics_config_hash",
Help: "Hash of the currently loaded configuration.",
})
configSuccess := promauto.With(ksmMetricsRegistry).NewGauge(
prometheus.GaugeOpts{
Name: "kube_state_metrics_config_last_reload_successful",
Help: "Whether the last configuration reload attempt was successful.",
})
configSuccessTime := promauto.With(ksmMetricsRegistry).NewGauge(
prometheus.GaugeOpts{
Name: "kube_state_metrics_config_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful configuration reload.",
})

storeBuilder.WithMetrics(ksmMetricsRegistry)

got := options.GetOptsConfigFile(*opts)
Expand All @@ -117,6 +135,12 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
klog.Infof("misconfigured config detected, KSM will automatically reload on next write to the config")
klog.Infof("waiting for config to be fixed")
<-ctx.Done()
configSuccess.Set(0)
} else {
configSuccess.Set(1)
configSuccessTime.SetToCurrentTime()
hash := md5HashAsMetricValue(optsConfigFile)
configHash.Set(hash)
}
}
var resources []string
Expand Down Expand Up @@ -371,3 +395,12 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
})
return mux
}

func md5HashAsMetricValue(data []byte) float64 {
sum := md5.Sum(data) //nolint:gosec
// We only want 48 bits as a float64 only has a 53 bit mantissa.
smallSum := sum[0:6]
bytes := make([]byte, 8)
copy(bytes, smallSum)
return float64(binary.LittleEndian.Uint64(bytes))
}
2 changes: 1 addition & 1 deletion pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Options struct {
cmd *cobra.Command
}

// GetOptsConfigFile is the getter for --options-config-file value.
// GetOptsConfigFile is the getter for --config value.
func GetOptsConfigFile(opt Options) string {
return opt.Config
}
Expand Down

0 comments on commit 7a56756

Please sign in to comment.