From 3f919e8c479b5aeb65f5852bd031a8d9ccaee25b Mon Sep 17 00:00:00 2001 From: dongjiang Date: Tue, 14 Jan 2025 11:47:32 +0800 Subject: [PATCH 1/8] add build info and go runtime metrics Signed-off-by: dongjiang --- hack/.util.sh.swp | Bin 0 -> 16384 bytes operator/cmd/operator/app/operator.go | 23 ++++ .../prometheus/collectors/version/version.go | 47 +++++++ .../prometheus/common/version/info.go | 125 ++++++++++++++++++ vendor/modules.txt | 2 + 5 files changed, 197 insertions(+) create mode 100644 hack/.util.sh.swp create mode 100644 vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go create mode 100644 vendor/github.com/prometheus/common/version/info.go diff --git a/hack/.util.sh.swp b/hack/.util.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..50530d197f96d10b714ea40966cd1e6356c45f97 GIT binary patch literal 16384 zcmeHOTZ|i589u#0DQy5X;;j-V-fcE1u_wFXrJI^CYfm;-cI}nz&4p}a#^YoAbjNed z%$eOR+XaC@;)#L~DMI1}kSG$bm3Tq|aS4S7-g!Y0LIU-n6)$a|z<1{28PB>)S|p^R z8SNL(IsbqD%Qydw^PipVp1o3Ar_Yqm5*!~Prq0n{Wl}|jY$5(jQpW!{>kX`!Du30A5G^U zIRiNZIRiNZIRiNZIRiNZIRiNZIRiNZIRo!W223>ci-`QXYIO1Y|G58u^}~ex3it)^ zCEx<^QQ$am_d|sI5%>;p1$YejD?*29E+40sBA03XLbf#T-cJKYGNcW-~= z?(MIJKj9!Xrqg1Y9ZxO(kJA5cbrR)~L6IqG5a3ioGsVyde_|rug;uyknK>%Y*wN7* zLFH5gQ-ilNknA{I>S zC4xg!(_zFb-+|xQ_I(&4$u&)tVG`0b3Ca9S<;g`$pUkvy|C19_GnY)HIDx`c!r5R+ z@qcoV%X)KNuj-xEreU;e_0{qMnHx$u_LenGvRiUn2s?PujX9d>S)3P5R~84%VXi5K zdtqpod#rEzwmi9$oc4ty%hg8fsim_`)@AldWvU$cBIGooG=6bz#H7nDuZ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KqS6i1HQ^uquClJSPman3{j_aUm1|Hy zCI%{_w=E<>6njAtH8<;F2^LwrJ22v|t2$-uPg^ByJC`i%(A#6~Fm2~tX=za#p)xi4 zNvdJjNHfRVn#;z*{N{D!VD)1N32xd zdTd8?eB}20^Ps9ni+o{$BJ6^8vDsLsVTj9`xwQp9YU?NQthOIeaLGi+#?H)*Pu)By IoL5!)ZwMbctpET3 literal 0 HcmV?d00001 diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index 871a043927a4..588e04b77b5d 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -21,7 +21,10 @@ import ( "flag" "fmt" "os" + "regexp" + "github.com/prometheus/client_golang/prometheus/collectors" + prometheusversion "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/util/sets" restclient "k8s.io/client-go/rest" @@ -32,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/config" "sigs.k8s.io/controller-runtime/pkg/healthz" + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" "github.com/karmada-io/karmada/operator/cmd/operator/app/options" @@ -110,6 +114,25 @@ func Run(ctx context.Context, o *options.Options) error { return err } + if err := manager.AddReadyzCheck("readyz", healthz.Ping); err != nil { + klog.Errorf("Unable to set up ready check: %v", err) + return err + } + + ctrlmetrics.Registry.Register( + collectors.NewGoCollector( + collectors.WithGoCollectorRuntimeMetrics( + collectors.MetricsGC, + collectors.MetricsScheduler, + collectors.MetricsMemory, + collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile(`^/sync/.*`)}, + ), + ), + ) + ctrlmetrics.Registry.Register( + prometheusversion.NewCollector("karmada_operator"), + ) + controllerCtx := ctrlctx.Context{ Controllers: o.Controllers, Manager: manager, 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 new file mode 100644 index 000000000000..c96e187126ff --- /dev/null +++ b/vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go @@ -0,0 +1,47 @@ +// 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/github.com/prometheus/common/version/info.go b/vendor/github.com/prometheus/common/version/info.go new file mode 100644 index 000000000000..197d95e5c8b0 --- /dev/null +++ b/vendor/github.com/prometheus/common/version/info.go @@ -0,0 +1,125 @@ +// 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 ( + "bytes" + "fmt" + "runtime" + "runtime/debug" + "strings" + "text/template" +) + +// Build information. Populated at build-time. +var ( + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion = runtime.Version() + GoOS = runtime.GOOS + GoArch = runtime.GOARCH + + computedRevision string + computedTags string +) + +// versionInfoTmpl contains the template used by Info. +var versionInfoTmpl = ` +{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) + build user: {{.buildUser}} + build date: {{.buildDate}} + go version: {{.goVersion}} + platform: {{.platform}} + tags: {{.tags}} +` + +// Print returns version information. +func Print(program string) string { + m := map[string]string{ + "program": program, + "version": Version, + "revision": GetRevision(), + "branch": Branch, + "buildUser": BuildUser, + "buildDate": BuildDate, + "goVersion": GoVersion, + "platform": GoOS + "/" + GoArch, + "tags": GetTags(), + } + t := template.Must(template.New("version").Parse(versionInfoTmpl)) + + var buf bytes.Buffer + if err := t.ExecuteTemplate(&buf, "version", m); err != nil { + panic(err) + } + return strings.TrimSpace(buf.String()) +} + +// Info returns version, branch and revision information. +func Info() string { + return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, GetRevision()) +} + +// BuildContext returns goVersion, platform, buildUser and buildDate information. +func BuildContext() string { + return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, GetTags()) +} + +func GetRevision() string { + if Revision != "" { + return Revision + } + return computedRevision +} + +func GetTags() string { + return computedTags +} + +func init() { + computedRevision, computedTags = computeRevision() +} + +func computeRevision() (string, string) { + var ( + rev = "unknown" + tags = "unknown" + modified bool + ) + + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return rev, tags + } + for _, v := range buildInfo.Settings { + if v.Key == "vcs.revision" { + rev = v.Value + } + if v.Key == "vcs.modified" { + if v.Value == "true" { + modified = true + } + } + if v.Key == "-tags" { + tags = v.Value + } + } + if modified { + return rev + "-modified", tags + } + return rev, tags +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 14d437e279c3..112d6144e4b7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -385,6 +385,7 @@ 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/promhttp github.com/prometheus/client_golang/prometheus/testutil @@ -397,6 +398,7 @@ github.com/prometheus/client_model/go ## explicit; go 1.20 github.com/prometheus/common/expfmt github.com/prometheus/common/model +github.com/prometheus/common/version # github.com/prometheus/procfs v0.15.1 ## explicit; go 1.20 github.com/prometheus/procfs From fb2da370a9cab76799f491416dab4f793b119cd8 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Tue, 14 Jan 2025 11:47:52 +0800 Subject: [PATCH 2/8] add build info and go runtime metrics Signed-off-by: dongjiang --- hack/.util.sh.swp | Bin 16384 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 hack/.util.sh.swp diff --git a/hack/.util.sh.swp b/hack/.util.sh.swp deleted file mode 100644 index 50530d197f96d10b714ea40966cd1e6356c45f97..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeHOTZ|i589u#0DQy5X;;j-V-fcE1u_wFXrJI^CYfm;-cI}nz&4p}a#^YoAbjNed z%$eOR+XaC@;)#L~DMI1}kSG$bm3Tq|aS4S7-g!Y0LIU-n6)$a|z<1{28PB>)S|p^R z8SNL(IsbqD%Qydw^PipVp1o3Ar_Yqm5*!~Prq0n{Wl}|jY$5(jQpW!{>kX`!Du30A5G^U zIRiNZIRiNZIRiNZIRiNZIRiNZIRiNZIRo!W223>ci-`QXYIO1Y|G58u^}~ex3it)^ zCEx<^QQ$am_d|sI5%>;p1$YejD?*29E+40sBA03XLbf#T-cJKYGNcW-~= z?(MIJKj9!Xrqg1Y9ZxO(kJA5cbrR)~L6IqG5a3ioGs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-ctwi{SJing|I%87~ z574uhpYw#H2IazjiDN&&yYckRl4`O`p|^vA+K5$Q1|Pda)d))0S21JzGRU+rzaO0* z6v8coXP-rS^yJBy9sGqRrNN(1(@YQuYe~@<3L`U^Ts$E04nwfzu)Mr$a@h&;>I9*6 zCS;yEP4`t-yr{TRy(G3%dgz34m+@RyP(cq#%rp@3gddtd8 zF&pD@(0wYFRKuJkpjusMN7kSf@qq@}V=4yKqS6i1HQ^uquClJSPman3{j_aUm1|Hy zCI%{_w=E<>6njAtH8<;F2^LwrJ22v|t2$-uPg^ByJC`i%(A#6~Fm2~tX=za#p)xi4 zNvdJjNHfRVn#;z*{N{D!VD)1N32xd zdTd8?eB}20^Ps9ni+o{$BJ6^8vDsLsVTj9`xwQp9YU?NQthOIeaLGi+#?H)*Pu)By IoL5!)ZwMbctpET3 From 851c785640e5d66b653f31d5a5ece7f7288116c8 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Tue, 14 Jan 2025 12:21:33 +0800 Subject: [PATCH 3/8] fix golangci lint Signed-off-by: dongjiang --- operator/cmd/operator/app/operator.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index 588e04b77b5d..0ff96f4a05a3 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -119,7 +119,10 @@ func Run(ctx context.Context, o *options.Options) error { return err } - ctrlmetrics.Registry.Register( + // Unregister default NewGoCollector + ctrlmetrics.Registry.Unregister(collectors.NewGoCollector()) + + ctrlmetrics.Registry.MustRegister( collectors.NewGoCollector( collectors.WithGoCollectorRuntimeMetrics( collectors.MetricsGC, @@ -129,7 +132,7 @@ func Run(ctx context.Context, o *options.Options) error { ), ), ) - ctrlmetrics.Registry.Register( + ctrlmetrics.Registry.MustRegister( prometheusversion.NewCollector("karmada_operator"), ) From e1acbcf9602fcb27b9fbf84fd50bc13da445664c Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 16 Jan 2025 20:46:09 +0800 Subject: [PATCH 4/8] fix gflag to version Signed-off-by: dongjiang --- hack/util.sh | 14 ++++++++++++ operator/cmd/operator/app/operator.go | 3 +-- pkg/version/base.go | 2 ++ pkg/version/version.go | 32 +++++++++++++++++++++++++++ pkg/version/version_test.go | 4 +++- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/hack/util.sh b/hack/util.sh index a2d84af25940..fda54fe595fc 100755 --- a/hack/util.sh +++ b/hack/util.sh @@ -707,9 +707,21 @@ function util::get_version() { git describe --tags --dirty } +function util::get_branch() { + git branch --show-current +} + +function util::get_revision() { + git rev-parse --short HEAD +} + function util::version_ldflags() { # Git information GIT_VERSION=$(util::get_version) + # Git branch + GIT_BRANCH=$(util::get_branch) + # Git revision + GIT_REVISION=$(util::get_revision) GIT_COMMIT_HASH=$(git rev-parse HEAD) if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then GIT_TREESTATE="clean" @@ -720,6 +732,8 @@ function util::version_ldflags() { LDFLAGS="-X github.com/karmada-io/karmada/pkg/version.gitVersion=${GIT_VERSION} \ -X github.com/karmada-io/karmada/pkg/version.gitCommit=${GIT_COMMIT_HASH} \ -X github.com/karmada-io/karmada/pkg/version.gitTreeState=${GIT_TREESTATE} \ + -X github.com/karmada-io/karmada/pkg/version.gitBranch=${GIT_BRANCH} \ + -X github.com/karmada-io/karmada/pkg/version.gitRevision=${GIT_REVISION} \ -X github.com/karmada-io/karmada/pkg/version.buildDate=${BUILDDATE}" echo $LDFLAGS } diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index 0ff96f4a05a3..c4010ace379b 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -24,7 +24,6 @@ import ( "regexp" "github.com/prometheus/client_golang/prometheus/collectors" - prometheusversion "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/util/sets" restclient "k8s.io/client-go/rest" @@ -133,7 +132,7 @@ func Run(ctx context.Context, o *options.Options) error { ), ) ctrlmetrics.Registry.MustRegister( - prometheusversion.NewCollector("karmada_operator"), + version.NewCollector("karmada_operator"), ) controllerCtx := ctrlctx.Context{ diff --git a/pkg/version/base.go b/pkg/version/base.go index 3b4b83efd830..f3e006dc1713 100644 --- a/pkg/version/base.go +++ b/pkg/version/base.go @@ -26,6 +26,8 @@ var ( gitVersion = "v0.0.0-master" gitCommit = "unknown" // sha1 from git, output of $(git rev-parse HEAD) gitTreeState = "unknown" // state of git tree, either "clean" or "dirty" + gitBranch = "unknown" + gitRevision = "unknown" buildDate = "unknown" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') ) diff --git a/pkg/version/version.go b/pkg/version/version.go index a50d74f95390..1e9dd9c9c9b3 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -19,13 +19,17 @@ package version import ( "fmt" "runtime" + + "github.com/prometheus/client_golang/prometheus" ) // Info contains versioning information. type Info struct { GitVersion string `json:"gitVersion"` GitCommit string `json:"gitCommit"` + GitRevision string `json:"gitRevision"` GitTreeState string `json:"gitTreeState"` + GitBranch string `json:"gitBranch"` BuildDate string `json:"buildDate"` GoVersion string `json:"goVersion"` Compiler string `json:"compiler"` @@ -42,11 +46,39 @@ func (info Info) String() string { func Get() Info { return Info{ GitVersion: gitVersion, + GitRevision: gitRevision, GitCommit: gitCommit, GitTreeState: gitTreeState, + GitBranch: gitBranch, BuildDate: buildDate, GoVersion: runtime.Version(), Compiler: runtime.Compiler, Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), } } + +// 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": Get().GitVersion, + "revision": Get().GitRevision, + "branch": Get().GitBranch, + "goversion": runtime.Version(), + "goos": runtime.GOOS, + "goarch": runtime.GOARCH, + "compiler": runtime.Compiler, + "platform": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + }, + }, + func() float64 { return 1 }, + ) +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go index 3bef19c9dd42..82b7ba3f68d6 100644 --- a/pkg/version/version_test.go +++ b/pkg/version/version_test.go @@ -32,12 +32,14 @@ func TestInfo_String(t *testing.T) { GitVersion: "1.3.0", GitCommit: "da070e68f3318410c8c70ed8186a2bc4736dacbd", GitTreeState: "clean", + GitRevision: "851c78564", + GitBranch: "v1.3.0", BuildDate: "2022-08-31T13:09:22Z", GoVersion: "go1.18.3", Compiler: "gc", Platform: "linux/amd64", }, - want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitTreeState:"clean", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`, + want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitRevision:"851c78564", GitTreeState:"clean", GitBranch:"v1.3.0", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`, }, } for _, tt := range tests { From 5868c910ed80d02d6cf11060048b5d08d140f850 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 16 Jan 2025 20:49:54 +0800 Subject: [PATCH 5/8] go mod tidy Signed-off-by: dongjiang --- .../prometheus/collectors/version/version.go | 47 ------- .../prometheus/common/version/info.go | 125 ------------------ vendor/modules.txt | 2 - 3 files changed, 174 deletions(-) delete mode 100644 vendor/github.com/prometheus/client_golang/prometheus/collectors/version/version.go delete mode 100644 vendor/github.com/prometheus/common/version/info.go 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 c96e187126ff..000000000000 --- 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/github.com/prometheus/common/version/info.go b/vendor/github.com/prometheus/common/version/info.go deleted file mode 100644 index 197d95e5c8b0..000000000000 --- a/vendor/github.com/prometheus/common/version/info.go +++ /dev/null @@ -1,125 +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 ( - "bytes" - "fmt" - "runtime" - "runtime/debug" - "strings" - "text/template" -) - -// Build information. Populated at build-time. -var ( - Version string - Revision string - Branch string - BuildUser string - BuildDate string - GoVersion = runtime.Version() - GoOS = runtime.GOOS - GoArch = runtime.GOARCH - - computedRevision string - computedTags string -) - -// versionInfoTmpl contains the template used by Info. -var versionInfoTmpl = ` -{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) - build user: {{.buildUser}} - build date: {{.buildDate}} - go version: {{.goVersion}} - platform: {{.platform}} - tags: {{.tags}} -` - -// Print returns version information. -func Print(program string) string { - m := map[string]string{ - "program": program, - "version": Version, - "revision": GetRevision(), - "branch": Branch, - "buildUser": BuildUser, - "buildDate": BuildDate, - "goVersion": GoVersion, - "platform": GoOS + "/" + GoArch, - "tags": GetTags(), - } - t := template.Must(template.New("version").Parse(versionInfoTmpl)) - - var buf bytes.Buffer - if err := t.ExecuteTemplate(&buf, "version", m); err != nil { - panic(err) - } - return strings.TrimSpace(buf.String()) -} - -// Info returns version, branch and revision information. -func Info() string { - return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, GetRevision()) -} - -// BuildContext returns goVersion, platform, buildUser and buildDate information. -func BuildContext() string { - return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, GetTags()) -} - -func GetRevision() string { - if Revision != "" { - return Revision - } - return computedRevision -} - -func GetTags() string { - return computedTags -} - -func init() { - computedRevision, computedTags = computeRevision() -} - -func computeRevision() (string, string) { - var ( - rev = "unknown" - tags = "unknown" - modified bool - ) - - buildInfo, ok := debug.ReadBuildInfo() - if !ok { - return rev, tags - } - for _, v := range buildInfo.Settings { - if v.Key == "vcs.revision" { - rev = v.Value - } - if v.Key == "vcs.modified" { - if v.Value == "true" { - modified = true - } - } - if v.Key == "-tags" { - tags = v.Value - } - } - if modified { - return rev + "-modified", tags - } - return rev, tags -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 112d6144e4b7..14d437e279c3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -385,7 +385,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/promhttp github.com/prometheus/client_golang/prometheus/testutil @@ -398,7 +397,6 @@ github.com/prometheus/client_model/go ## explicit; go 1.20 github.com/prometheus/common/expfmt github.com/prometheus/common/model -github.com/prometheus/common/version # github.com/prometheus/procfs v0.15.1 ## explicit; go 1.20 github.com/prometheus/procfs From 4880924b3ea95452d84686674c6106836b917fd2 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 16 Jan 2025 20:57:28 +0800 Subject: [PATCH 6/8] fix in github-action Signed-off-by: dongjiang --- hack/util.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/util.sh b/hack/util.sh index fda54fe595fc..5bf3d178fdeb 100755 --- a/hack/util.sh +++ b/hack/util.sh @@ -708,7 +708,7 @@ function util::get_version() { } function util::get_branch() { - git branch --show-current + git rev-parse --abbrev-ref HEAD } function util::get_revision() { From a65fcac541d113138abc45af25ecaadc7cae1f57 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Fri, 17 Jan 2025 12:38:39 +0800 Subject: [PATCH 7/8] remove readyz handler Signed-off-by: dongjiang --- operator/cmd/operator/app/operator.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index c4010ace379b..029a48997a4f 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -113,11 +113,6 @@ func Run(ctx context.Context, o *options.Options) error { return err } - if err := manager.AddReadyzCheck("readyz", healthz.Ping); err != nil { - klog.Errorf("Unable to set up ready check: %v", err) - return err - } - // Unregister default NewGoCollector ctrlmetrics.Registry.Unregister(collectors.NewGoCollector()) From d7fd1fd2a7367b1579f3b7a30aa2d85ae2cd15c6 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Mon, 20 Jan 2025 18:08:59 +0800 Subject: [PATCH 8/8] add doc by codereivew Signed-off-by: dongjiang --- operator/cmd/operator/app/operator.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index 029a48997a4f..52e09036c827 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -117,15 +117,26 @@ func Run(ctx context.Context, o *options.Options) error { ctrlmetrics.Registry.Unregister(collectors.NewGoCollector()) ctrlmetrics.Registry.MustRegister( + // Go Runtime metrics about debug.GCStats (base metrics) and + // runtime/metrics. collectors.NewGoCollector( collectors.WithGoCollectorRuntimeMetrics( + // go runtime gc metrics. (e.g. `go_gc_duration_seconds` + // means garbage collection cycle pause duration) collectors.MetricsGC, + // go runtime scheduler metrics. (e.g. `go_sched_gomaxprocs_threads` + // means the current runtime.GOMAXPROCS setting) collectors.MetricsScheduler, + // go runtime memory metrics. (e.g. `go_memstats_alloc_bytes` + // means number of bytes allocated and still in use) collectors.MetricsMemory, + // go runtime sync lock metrics. (e.g. `go_sync_mutex_wait_total_seconds_total` + // means Approximate cumulative time goroutines have spent blocked on a sync.Mutex, sync.RWMutex, or runtime-internal lock) collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile(`^/sync/.*`)}, ), ), ) + // `karmada_operator_build_info` metrics for operator version upgrade ctrlmetrics.Registry.MustRegister( version.NewCollector("karmada_operator"), )