Skip to content

Commit

Permalink
[feat] Add rest client metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Xuzheng Chang <changxuzheng@huawei.com>
  • Loading branch information
Monokaix committed Dec 18, 2023
1 parent a2e6dab commit 5f4527d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cmd/scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"os"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"

"volcano.sh/apis/pkg/apis/helpers"

"volcano.sh/volcano/cmd/scheduler/app/options"
"volcano.sh/volcano/pkg/kube"
"volcano.sh/volcano/pkg/scheduler"
Expand All @@ -39,6 +41,7 @@ import (
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog/v2"

// Register gcp auth
Expand All @@ -47,6 +50,9 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"

// Register rest client metrics
_ "k8s.io/component-base/metrics/prometheus/restclient"
)

const (
Expand Down Expand Up @@ -81,7 +87,7 @@ func Run(opt *options.ServerOption) error {

if opt.EnableMetrics {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.Handle("/metrics", promHandler())
klog.Fatalf("Prometheus Http Server failed %s", http.ListenAndServe(opt.ListenAddress, nil))
}()
}
Expand Down Expand Up @@ -147,3 +153,10 @@ func Run(opt *options.ServerOption) error {
})
return fmt.Errorf("lost lease")
}

func promHandler() http.Handler {
// Unregister go and process related collector because it's duplicated and `legacyregistry.DefaultGatherer` also has registered them.
prometheus.DefaultRegisterer.Unregister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
prometheus.DefaultRegisterer.Unregister(collectors.NewGoCollector())
return promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.Gatherers{prometheus.DefaultGatherer, legacyregistry.DefaultGatherer}, promhttp.HandlerOpts{}))
}

0 comments on commit 5f4527d

Please sign in to comment.