From 1d5cda01878a56ba38db1522a45613b54f09e002 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 6 Mar 2025 20:10:32 +0800 Subject: [PATCH] fix by codereview Signed-off-by: dongjiang --- operator/cmd/operator/.gitignore | 1 - operator/cmd/operator/app/operator.go | 3 +- pkg/metrics/version.go | 51 +++++++++++++++++++++++ pkg/version/base.go | 8 ++-- pkg/version/version.go | 59 ++++++++------------------- pkg/version/version_test.go | 18 ++++---- 6 files changed, 82 insertions(+), 58 deletions(-) delete mode 100644 operator/cmd/operator/.gitignore create mode 100644 pkg/metrics/version.go diff --git a/operator/cmd/operator/.gitignore b/operator/cmd/operator/.gitignore deleted file mode 100644 index e196e4b5d49f..000000000000 --- a/operator/cmd/operator/.gitignore +++ /dev/null @@ -1 +0,0 @@ -operator diff --git a/operator/cmd/operator/app/operator.go b/operator/cmd/operator/app/operator.go index 721f1668377f..e4b12e08a7ed 100644 --- a/operator/cmd/operator/app/operator.go +++ b/operator/cmd/operator/app/operator.go @@ -40,6 +40,7 @@ import ( ctrlctx "github.com/karmada-io/karmada/operator/pkg/controller/context" "github.com/karmada-io/karmada/operator/pkg/controller/karmada" "github.com/karmada-io/karmada/operator/pkg/scheme" + versionmetrics "github.com/karmada-io/karmada/pkg/metrics" "github.com/karmada-io/karmada/pkg/sharedcli" "github.com/karmada-io/karmada/pkg/sharedcli/klogflag" "github.com/karmada-io/karmada/pkg/version" @@ -113,7 +114,7 @@ func Run(ctx context.Context, o *options.Options) error { // `karmada_operator_build_info` metrics for operator version upgrade ctrlmetrics.Registry.MustRegister( - version.NewCollector("karmada_operator"), + versionmetrics.NewCollector("karmada_operator"), ) controllerCtx := ctrlctx.Context{ diff --git a/pkg/metrics/version.go b/pkg/metrics/version.go new file mode 100644 index 000000000000..3a4987caddf8 --- /dev/null +++ b/pkg/metrics/version.go @@ -0,0 +1,51 @@ +/* +Copyright 2025 The Karmada 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 metrics + +import ( + "fmt" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/karmada-io/karmada/pkg/version" +) + +// 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{ + "git_version": version.Get().GitVersion, + "git_commit": version.Get().GitCommit, + "git_short_commit": version.Get().GitShortCommit, + "git_tree_state": version.Get().GitTreeState, + "build_date": version.Get().BuildDate, + "go_version": version.Get().GoVersion, + "compiler": version.Get().Compiler, + "platform": version.Get().Platform, + }, + }, + func() float64 { return 1 }, + ) +} diff --git a/pkg/version/base.go b/pkg/version/base.go index 46d60c0a8cf2..cc0c704927bf 100644 --- a/pkg/version/base.go +++ b/pkg/version/base.go @@ -23,10 +23,10 @@ package version // version for ad-hoc builds (e.g. `go build`) that cannot get the version // information from git. 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" - gitAbbreviativeCommit = "unknown" // short sha1 from git, output of $(git rev-parse --short HEAD) + 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" + gitShortCommit = "unknown" // short sha1 from git, output of $(git rev-parse --short HEAD) 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 1b4544940520..c9d99d4959d1 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -19,20 +19,18 @@ 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"` - GitAbbreviativeCommit string `json:"gitAbbreviativeCommit"` - GitTreeState string `json:"gitTreeState"` - BuildDate string `json:"buildDate"` - GoVersion string `json:"goVersion"` - Compiler string `json:"compiler"` - Platform string `json:"platform"` + GitVersion string `json:"gitVersion"` + GitCommit string `json:"gitCommit"` + GitShortCommit string `json:"gitShortCommit"` + GitTreeState string `json:"gitTreeState"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` } // String returns a Go-syntax representation of the Info. @@ -44,38 +42,13 @@ func (info Info) String() string { // what code a binary was built from. func Get() Info { return Info{ - GitVersion: gitVersion, - GitAbbreviativeCommit: gitAbbreviativeCommit, - GitCommit: gitCommit, - GitTreeState: gitTreeState, - BuildDate: buildDate, - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + GitVersion: gitVersion, + GitShortCommit: gitShortCommit, + GitCommit: gitCommit, + GitTreeState: gitTreeState, + 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().GitAbbreviativeCommit, - "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 4a98b00e7920..c6e750e2ed93 100644 --- a/pkg/version/version_test.go +++ b/pkg/version/version_test.go @@ -29,16 +29,16 @@ func TestInfo_String(t *testing.T) { { name: "test1", info: Info{ - GitVersion: "1.3.0", - GitCommit: "da070e68f3318410c8c70ed8186a2bc4736dacbd", - GitTreeState: "clean", - GitAbbreviativeCommit: "851c78564", - BuildDate: "2022-08-31T13:09:22Z", - GoVersion: "go1.18.3", - Compiler: "gc", - Platform: "linux/amd64", + GitVersion: "1.3.0", + GitCommit: "da070e68f3318410c8c70ed8186a2bc4736dacbd", + GitTreeState: "clean", + GitShortCommit: "851c78564", + BuildDate: "2022-08-31T13:09:22Z", + GoVersion: "go1.18.3", + Compiler: "gc", + Platform: "linux/amd64", }, - want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitAbbreviativeCommit:"851c78564", 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", GitShortCommit:"851c78564", GitTreeState:"clean", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`, }, } for _, tt := range tests {