diff --git a/pkg/kube_state_metrics/server.go b/pkg/kube_state_metrics/server.go index 2094e8fb0..62d022df1 100644 --- a/pkg/kube_state_metrics/server.go +++ b/pkg/kube_state_metrics/server.go @@ -13,6 +13,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/exporter-toolkit/web" + "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" "github.com/clusterpedia-io/clusterpedia/pkg/metrics" @@ -26,7 +27,7 @@ type ServerConfig struct { } func RunServer(config ServerConfig, getter ClusterMetricsWriterListGetter) { - durationVec := promauto.With(metrics.DefaultRegistry()).NewHistogramVec( + durationVec := promauto.With(legacyregistry.Registerer()).NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "A histogram of requests for clusterpedia's kube-state-metrics metrics handler.", diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 3b6d9ea9a..f00950391 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -1,16 +1,12 @@ package metrics import ( - "github.com/prometheus/client_golang/prometheus" versionCollector "github.com/prometheus/client_golang/prometheus/collectors/version" + "k8s.io/component-base/metrics/legacyregistry" ) -var registry = prometheus.NewRegistry() - -func DefaultRegistry() prometheus.Registerer { - return registry -} - func init() { - registry.MustRegister(versionCollector.NewCollector("clusterpedia_kube_state_metrics")) + legacyregistry.RawMustRegister( + versionCollector.NewCollector("clusterpedia_kube_state_metrics"), + ) } diff --git a/pkg/metrics/server.go b/pkg/metrics/server.go index c9f03081f..bcc676fd1 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server.go @@ -4,8 +4,9 @@ import ( "net/http" "time" - "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/exporter-toolkit/web" + "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" "github.com/clusterpedia-io/clusterpedia/pkg/pprof" @@ -37,10 +38,13 @@ func RunServer(config Config) { func buildMetricsServer(config Config) *http.ServeMux { mux := http.NewServeMux() - mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ - ErrorLog: Logger, - DisableCompression: config.DisableGZIPEncoding, - })) + mux.Handle("/metrics", metrics.HandlerWithReset( + legacyregistry.DefaultGatherer.(metrics.KubeRegistry), + metrics.HandlerOpts{ + ErrorLog: Logger, + DisableCompression: config.DisableGZIPEncoding, + }), + ) // add profiler pprof.RegisterProfileHandler(mux) // Add index