Skip to content

Commit

Permalink
metrics: use legacyregister instead of new registry
Browse files Browse the repository at this point in the history
Signed-off-by: Iceber Gu <[email protected]>
  • Loading branch information
Iceber committed Aug 29, 2024
1 parent 58b67c8 commit 33a3e25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pkg/kube_state_metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.",
Expand Down
12 changes: 4 additions & 8 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -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"),
)
}
17 changes: 12 additions & 5 deletions pkg/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -35,12 +36,18 @@ func RunServer(config Config) {
_ = web.ListenAndServe(server, flags, Logger)
}

var processStart = time.Now()

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,
ProcessStartTime: processStart,
}),
)
// add profiler
pprof.RegisterProfileHandler(mux)
// Add index
Expand Down

0 comments on commit 33a3e25

Please sign in to comment.