From 764d1a55247bfa947f461cbc3db3361c66c21213 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 + pkg/kube_state_metrics/server.go | 4 +- pkg/metrics/{ => logger}/logger.go | 2 +- pkg/metrics/metrics.go | 12 ----- pkg/metrics/{ => server}/options.go | 2 +- pkg/metrics/{ => server}/server.go | 8 ++-- pkg/metrics/version.go | 22 +++++++++ .../prometheus/collectors/version/version.go | 47 ------------------- .../metrics/prometheus/version/metrics.go | 41 ++++++++++++++++ vendor/modules.txt | 2 +- 10 files changed, 74 insertions(+), 67 deletions(-) rename pkg/metrics/{ => logger}/logger.go (93%) delete mode 100644 pkg/metrics/metrics.go rename pkg/metrics/{ => server}/options.go (98%) rename pkg/metrics/{ => server}/server.go (87%) 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/pkg/kube_state_metrics/server.go b/pkg/kube_state_metrics/server.go index 62d022df1..3a981d80b 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" + "github.com/clusterpedia-io/clusterpedia/pkg/metrics/logger" "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, logger.Logger) } func buildMetricsServer(config ServerConfig, getter ClusterMetricsWriterListGetter, durationObserver prometheus.ObserverVec) *mux.Router { diff --git a/pkg/metrics/logger.go b/pkg/metrics/logger/logger.go similarity index 93% rename from pkg/metrics/logger.go rename to pkg/metrics/logger/logger.go index e9fa250c1..98fd2674a 100644 --- a/pkg/metrics/logger.go +++ b/pkg/metrics/logger/logger.go @@ -1,4 +1,4 @@ -package metrics +package logger import "k8s.io/klog/v2" 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/options.go b/pkg/metrics/server/options.go similarity index 98% rename from pkg/metrics/options.go rename to pkg/metrics/server/options.go index faf9c9d9d..c8e911810 100644 --- a/pkg/metrics/options.go +++ b/pkg/metrics/server/options.go @@ -1,4 +1,4 @@ -package metrics +package server import ( "net" diff --git a/pkg/metrics/server.go b/pkg/metrics/server/server.go similarity index 87% rename from pkg/metrics/server.go rename to pkg/metrics/server/server.go index bcc676fd1..e1b49264e 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,8 @@ import ( "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" + _ "github.com/clusterpedia-io/clusterpedia/pkg/metrics" + "github.com/clusterpedia-io/clusterpedia/pkg/metrics/logger" "github.com/clusterpedia-io/clusterpedia/pkg/pprof" "github.com/clusterpedia-io/clusterpedia/pkg/version" ) @@ -33,7 +35,7 @@ func RunServer(config Config) { } klog.Info("Metrics Server is running...") - _ = web.ListenAndServe(server, flags, Logger) + _ = web.ListenAndServe(server, flags, logger.Logger) } func buildMetricsServer(config Config) *http.ServeMux { @@ -41,7 +43,7 @@ func buildMetricsServer(config Config) *http.ServeMux { mux.Handle("/metrics", metrics.HandlerWithReset( legacyregistry.DefaultGatherer.(metrics.KubeRegistry), metrics.HandlerOpts{ - ErrorLog: Logger, + ErrorLog: logger.Logger, DisableCompression: config.DisableGZIPEncoding, }), ) diff --git a/pkg/metrics/version.go b/pkg/metrics/version.go new file mode 100644 index 000000000..06badda30 --- /dev/null +++ b/pkg/metrics/version.go @@ -0,0 +1,22 @@ +package metrics + +import ( + "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" + + "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 Kubernetes was built, and platform on which it is running.", + }, + []string{"git_version", "git_commit", "git_tree_state", "build_date", "go_ve rsion", "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