From 4cdf1955e4d5e7b73db9747fe5930233a11e569d Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Thu, 29 Aug 2024 16:11:35 +0800 Subject: [PATCH] metrics: register clusterpedia and kubernetes build info Signed-off-by: Iceber Gu --- cmd/apiserver/app/apiserver.go | 1 + .../app/config/config.go | 4 +- .../app/options/options.go | 6 +-- cmd/clustersynchro-manager/app/synchro.go | 4 +- pkg/kube_state_metrics/options.go | 4 +- pkg/kube_state_metrics/server.go | 4 +- pkg/metrics/metrics.go | 12 ----- pkg/metrics/{ => server}/logger.go | 2 +- pkg/metrics/{ => server}/options.go | 4 +- pkg/metrics/{ => server}/server.go | 5 +- pkg/metrics/version.go | 23 +++++++++ .../prometheus/collectors/version/version.go | 47 ------------------- .../metrics/prometheus/version/metrics.go | 41 ++++++++++++++++ vendor/modules.txt | 2 +- 14 files changed, 83 insertions(+), 76 deletions(-) delete mode 100644 pkg/metrics/metrics.go rename pkg/metrics/{ => server}/logger.go (93%) rename pkg/metrics/{ => server}/options.go (94%) rename pkg/metrics/{ => server}/server.go (94%) create mode 100644 pkg/metrics/version.go delete mode 100644 vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go create mode 100644 vendor/k8s.io/component-base/metrics/prometheus/version/metrics.go diff --git a/cmd/apiserver/app/apiserver.go b/cmd/apiserver/app/apiserver.go index 84e0280d2..30ea387a3 100644 --- a/cmd/apiserver/app/apiserver.go +++ b/cmd/apiserver/app/apiserver.go @@ -16,6 +16,7 @@ import ( "k8s.io/component-base/term" "github.com/clusterpedia-io/clusterpedia/cmd/apiserver/app/options" + _ "github.com/clusterpedia-io/clusterpedia/pkg/metrics" "github.com/clusterpedia-io/clusterpedia/pkg/storage" "github.com/clusterpedia-io/clusterpedia/pkg/version/verflag" ) diff --git a/cmd/clustersynchro-manager/app/config/config.go b/cmd/clustersynchro-manager/app/config/config.go index 5b9b6503c..c09eb53f8 100644 --- a/cmd/clustersynchro-manager/app/config/config.go +++ b/cmd/clustersynchro-manager/app/config/config.go @@ -7,7 +7,7 @@ import ( crdclientset "github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset/versioned" kubestatemetrics "github.com/clusterpedia-io/clusterpedia/pkg/kube_state_metrics" - metrics "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + metricsserver "github.com/clusterpedia-io/clusterpedia/pkg/metrics/server" "github.com/clusterpedia-io/clusterpedia/pkg/storage" "github.com/clusterpedia-io/clusterpedia/pkg/synchromanager/clustersynchro" ) @@ -19,7 +19,7 @@ type Config struct { WorkerNumber int ShardingName string - MetricsServerConfig metrics.Config + MetricsServerConfig metricsserver.Config KubeMetricsServerConfig *kubestatemetrics.ServerConfig StorageFactory storage.StorageFactory ClusterSyncConfig clustersynchro.ClusterSyncConfig diff --git a/cmd/clustersynchro-manager/app/options/options.go b/cmd/clustersynchro-manager/app/options/options.go index 7e91334cf..784528ee0 100644 --- a/cmd/clustersynchro-manager/app/options/options.go +++ b/cmd/clustersynchro-manager/app/options/options.go @@ -23,7 +23,7 @@ import ( "github.com/clusterpedia-io/clusterpedia/cmd/clustersynchro-manager/app/config" crdclientset "github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset/versioned" kubestatemetrics "github.com/clusterpedia-io/clusterpedia/pkg/kube_state_metrics" - "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + metricsserver "github.com/clusterpedia-io/clusterpedia/pkg/metrics/server" "github.com/clusterpedia-io/clusterpedia/pkg/storage" storageoptions "github.com/clusterpedia-io/clusterpedia/pkg/storage/options" "github.com/clusterpedia-io/clusterpedia/pkg/synchromanager/clustersynchro" @@ -42,7 +42,7 @@ type Options struct { Logs *logs.Options Storage *storageoptions.StorageOptions - Metrics *metrics.Options + Metrics *metricsserver.Options KubeStateMetrics *kubestatemetrics.Options WorkerNumber int // WorkerNumber is the number of worker goroutines @@ -76,7 +76,7 @@ func NewClusterSynchroManagerOptions() (*Options, error) { options.Logs = logs.NewOptions() options.Storage = storageoptions.NewStorageOptions() - options.Metrics = metrics.NewMetricsServerOptions() + options.Metrics = metricsserver.NewOptions() options.KubeStateMetrics = kubestatemetrics.NewOptions() options.WorkerNumber = 5 diff --git a/cmd/clustersynchro-manager/app/synchro.go b/cmd/clustersynchro-manager/app/synchro.go index a66305725..ea86f3e0b 100644 --- a/cmd/clustersynchro-manager/app/synchro.go +++ b/cmd/clustersynchro-manager/app/synchro.go @@ -21,7 +21,7 @@ import ( "github.com/clusterpedia-io/clusterpedia/cmd/clustersynchro-manager/app/config" "github.com/clusterpedia-io/clusterpedia/cmd/clustersynchro-manager/app/options" kubestatemetrics "github.com/clusterpedia-io/clusterpedia/pkg/kube_state_metrics" - "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + metricsserver "github.com/clusterpedia-io/clusterpedia/pkg/metrics/server" "github.com/clusterpedia-io/clusterpedia/pkg/storage" "github.com/clusterpedia-io/clusterpedia/pkg/synchromanager" clusterpediafeature "github.com/clusterpedia-io/clusterpedia/pkg/utils/feature" @@ -88,7 +88,7 @@ func Run(ctx context.Context, c *config.Config) error { synchromanager := synchromanager.NewManager(c.CRDClient, c.StorageFactory, c.ClusterSyncConfig, c.ShardingName) go func() { - metrics.RunServer(c.MetricsServerConfig) + metricsserver.Run(c.MetricsServerConfig) }() if c.KubeMetricsServerConfig != nil { diff --git a/pkg/kube_state_metrics/options.go b/pkg/kube_state_metrics/options.go index 528a0a309..91e4ca622 100644 --- a/pkg/kube_state_metrics/options.go +++ b/pkg/kube_state_metrics/options.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/pflag" "k8s.io/kube-state-metrics/v2/pkg/options" - "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + metricsserver "github.com/clusterpedia-io/clusterpedia/pkg/metrics/server" ) var defaultResources = options.ResourceSet{ @@ -87,7 +87,7 @@ func (o *Options) MetricsStoreBuilderConfig() *MetricsStoreBuilderConfig { } } -func (o *Options) ServerConfig(config metrics.Config) *ServerConfig { +func (o *Options) ServerConfig(config metricsserver.Config) *ServerConfig { if !o.EnableKubeStateMetrics { return nil } diff --git a/pkg/kube_state_metrics/server.go b/pkg/kube_state_metrics/server.go index 62d022df1..abb9a5206 100644 --- a/pkg/kube_state_metrics/server.go +++ b/pkg/kube_state_metrics/server.go @@ -16,7 +16,7 @@ import ( "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" - "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + metricsserver "github.com/clusterpedia-io/clusterpedia/pkg/metrics/server" "github.com/clusterpedia-io/clusterpedia/pkg/version" ) @@ -49,7 +49,7 @@ func RunServer(config ServerConfig, getter ClusterMetricsWriterListGetter) { klog.Info("Kube State Metrics Server is running...") // TODO(iceber): handle error - _ = web.ListenAndServe(server, flags, metrics.Logger) + _ = web.ListenAndServe(server, flags, metricsserver.Logger) } func buildMetricsServer(config ServerConfig, getter ClusterMetricsWriterListGetter, durationObserver prometheus.ObserverVec) *mux.Router { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go deleted file mode 100644 index f00950391..000000000 --- a/pkg/metrics/metrics.go +++ /dev/null @@ -1,12 +0,0 @@ -package metrics - -import ( - versionCollector "github.com/prometheus/client_golang/prometheus/collectors/version" - "k8s.io/component-base/metrics/legacyregistry" -) - -func init() { - legacyregistry.RawMustRegister( - versionCollector.NewCollector("clusterpedia_kube_state_metrics"), - ) -} diff --git a/pkg/metrics/logger.go b/pkg/metrics/server/logger.go similarity index 93% rename from pkg/metrics/logger.go rename to pkg/metrics/server/logger.go index e9fa250c1..327a109ef 100644 --- a/pkg/metrics/logger.go +++ b/pkg/metrics/server/logger.go @@ -1,4 +1,4 @@ -package metrics +package server import "k8s.io/klog/v2" diff --git a/pkg/metrics/options.go b/pkg/metrics/server/options.go similarity index 94% rename from pkg/metrics/options.go rename to pkg/metrics/server/options.go index faf9c9d9d..5ee5d71e8 100644 --- a/pkg/metrics/options.go +++ b/pkg/metrics/server/options.go @@ -1,4 +1,4 @@ -package metrics +package server import ( "net" @@ -15,7 +15,7 @@ type Options struct { DisableGZIPEncoding bool } -func NewMetricsServerOptions() *Options { +func NewOptions() *Options { return &Options{ Host: "::", Port: 8081, diff --git a/pkg/metrics/server.go b/pkg/metrics/server/server.go similarity index 94% rename from pkg/metrics/server.go rename to pkg/metrics/server/server.go index bcc676fd1..79473e553 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server/server.go @@ -1,4 +1,4 @@ -package metrics +package server import ( "net/http" @@ -9,6 +9,7 @@ import ( "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" + _ "github.com/clusterpedia-io/clusterpedia/pkg/metrics" "github.com/clusterpedia-io/clusterpedia/pkg/pprof" "github.com/clusterpedia-io/clusterpedia/pkg/version" ) @@ -20,7 +21,7 @@ type Config struct { DisableGZIPEncoding bool } -func RunServer(config Config) { +func Run(config Config) { server := &http.Server{ Handler: buildMetricsServer(config), ReadHeaderTimeout: 6 * time.Second, diff --git a/pkg/metrics/version.go b/pkg/metrics/version.go new file mode 100644 index 000000000..1e4a36434 --- /dev/null +++ b/pkg/metrics/version.go @@ -0,0 +1,23 @@ +package metrics + +import ( + "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" + _ "k8s.io/component-base/metrics/prometheus/version" + + "github.com/clusterpedia-io/clusterpedia/pkg/version" +) + +var buildInfo = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Name: "clusterpedia_build_info", + Help: "A metric with a constant '1' value labeled by git version, git commit, git tree state, build date, Go version, and compiler from which Clusterpedia was built, and platform on which it is running.", + }, + []string{"git_version", "git_commit", "git_tree_state", "build_date", "go_version", "compiler", "platform"}, +) + +func init() { + info := version.Get() + legacyregistry.MustRegister(buildInfo) + buildInfo.WithLabelValues(info.GitVersion, info.GitCommit, info.GitTreeState, info.BuildDate, info.GoVersion, info.Compiler, info.Platform).Set(1) +} diff --git a/vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go b/vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go deleted file mode 100644 index c96e18712..000000000 --- a/vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2016 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package version - -import ( - "fmt" - - "github.com/prometheus/common/version" - - "github.com/prometheus/client_golang/prometheus" -) - -// NewCollector returns a collector that exports metrics about current version -// information. -func NewCollector(program string) prometheus.Collector { - return prometheus.NewGaugeFunc( - prometheus.GaugeOpts{ - Namespace: program, - Name: "build_info", - Help: fmt.Sprintf( - "A metric with a constant '1' value labeled by version, revision, branch, goversion from which %s was built, and the goos and goarch for the build.", - program, - ), - ConstLabels: prometheus.Labels{ - "version": version.Version, - "revision": version.GetRevision(), - "branch": version.Branch, - "goversion": version.GoVersion, - "goos": version.GoOS, - "goarch": version.GoArch, - "tags": version.GetTags(), - }, - }, - func() float64 { return 1 }, - ) -} diff --git a/vendor/k8s.io/component-base/metrics/prometheus/version/metrics.go b/vendor/k8s.io/component-base/metrics/prometheus/version/metrics.go new file mode 100644 index 000000000..c74f31f95 --- /dev/null +++ b/vendor/k8s.io/component-base/metrics/prometheus/version/metrics.go @@ -0,0 +1,41 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package version + +import ( + "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" + "k8s.io/component-base/version" +) + +var ( + buildInfo = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Name: "kubernetes_build_info", + Help: "A metric with a constant '1' value labeled by major, minor, git version, git commit, git tree state, build date, Go version, and compiler from which Kubernetes was built, and platform on which it is running.", + StabilityLevel: metrics.ALPHA, + }, + []string{"major", "minor", "git_version", "git_commit", "git_tree_state", "build_date", "go_version", "compiler", "platform"}, + ) +) + +// RegisterBuildInfo registers the build and version info in a metadata metric in prometheus +func init() { + info := version.Get() + legacyregistry.MustRegister(buildInfo) + buildInfo.WithLabelValues(info.Major, info.Minor, info.GitVersion, info.GitCommit, info.GitTreeState, info.BuildDate, info.GoVersion, info.Compiler, info.Platform).Set(1) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 206b8ca15..73abfdf24 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -297,7 +297,6 @@ github.com/pmezard/go-difflib/difflib ## explicit; go 1.20 github.com/prometheus/client_golang/prometheus github.com/prometheus/client_golang/prometheus/collectors -github.com/prometheus/client_golang/prometheus/collectors/version github.com/prometheus/client_golang/prometheus/internal github.com/prometheus/client_golang/prometheus/promauto github.com/prometheus/client_golang/prometheus/promhttp @@ -1372,6 +1371,7 @@ k8s.io/component-base/metrics/features k8s.io/component-base/metrics/legacyregistry k8s.io/component-base/metrics/prometheus/feature k8s.io/component-base/metrics/prometheus/slis +k8s.io/component-base/metrics/prometheus/version k8s.io/component-base/metrics/prometheus/workqueue k8s.io/component-base/metrics/prometheusextension k8s.io/component-base/metrics/testutil