Skip to content

Commit

Permalink
fix by codereview
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiang <[email protected]>
  • Loading branch information
dongjiang1989 committed Mar 6, 2025
1 parent 532537d commit 1d5cda0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
1 change: 0 additions & 1 deletion operator/cmd/operator/.gitignore

This file was deleted.

3 changes: 2 additions & 1 deletion operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
51 changes: 51 additions & 0 deletions pkg/metrics/version.go
Original file line number Diff line number Diff line change
@@ -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 },
)
}
8 changes: 4 additions & 4 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
59 changes: 16 additions & 43 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 },
)
}
18 changes: 9 additions & 9 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1d5cda0

Please sign in to comment.