Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add operator build_info metrics and go runtime metrics #6044

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
sigs.k8s.io/cluster-api v1.7.1
sigs.k8s.io/controller-runtime v0.19.1
sigs.k8s.io/controller-runtime v0.19.6
sigs.k8s.io/custom-metrics-apiserver v1.30.1-0.20241105195130-84dc8cfe2555
sigs.k8s.io/kind v0.25.0
sigs.k8s.io/mcs-api v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1480,8 +1480,8 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3/go.mod h1:Ve9uj1
sigs.k8s.io/cluster-api v1.7.1 h1:JkMAbAMzBM+WBHxXLTJXTiCisv1PAaHRzld/3qrmLYY=
sigs.k8s.io/cluster-api v1.7.1/go.mod h1:V9ZhKLvQtsDODwjXOKgbitjyCmC71yMBwDcMyNNIov0=
sigs.k8s.io/controller-runtime v0.6.1/go.mod h1:XRYBPdbf5XJu9kpS84VJiZ7h/u1hF3gEORz0efEja7A=
sigs.k8s.io/controller-runtime v0.19.1 h1:Son+Q40+Be3QWb+niBXAg2vFiYWolDjjRfO8hn/cxOk=
sigs.k8s.io/controller-runtime v0.19.1/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/controller-runtime v0.19.6 h1:fuq53qTLQ7aJTA7aNsklNnu7eQtSFqJUomOyM+phPLk=
sigs.k8s.io/controller-runtime v0.19.6/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/controller-tools v0.3.0/go.mod h1:enhtKGfxZD1GFEoMgP8Fdbu+uKQ/cq1/WGJhdVChfvI=
sigs.k8s.io/custom-metrics-apiserver v1.30.1-0.20241105195130-84dc8cfe2555 h1:GYU1Vmegcr1cs7+D06pa6+saS2DDu31JIHvDIbvWtcE=
sigs.k8s.io/custom-metrics-apiserver v1.30.1-0.20241105195130-84dc8cfe2555/go.mod h1:JL2q3g2QCWnIDvo73jpkksZOVd3ee3FWzZs4EHvx5NE=
Expand Down
2 changes: 2 additions & 0 deletions hack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ function util::get_version() {
function util::version_ldflags() {
# Git information
GIT_VERSION=$(util::get_version)
GIT_SHORT_COMMIT=$(git rev-parse --short HEAD)
GIT_COMMIT_HASH=$(git rev-parse HEAD)
if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
GIT_TREESTATE="clean"
Expand All @@ -720,6 +721,7 @@ 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.gitShortCommit=${GIT_SHORT_COMMIT} \
-X github.com/karmada-io/karmada/pkg/version.buildDate=${BUILDDATE}"
echo $LDFLAGS
}
Expand Down
7 changes: 7 additions & 0 deletions operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ 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"
operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1"
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 @@ -110,6 +112,11 @@ func Run(ctx context.Context, o *options.Options) error {
return err
}

// `karmada_operator_build_info` metrics for operator version upgrade
ctrlmetrics.Registry.MustRegister(
versionmetrics.NewCollector("karmada_operator"),
)

controllerCtx := ctrlctx.Context{
Controllers: o.Controllers,
Manager: manager,
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func NewCollector(program string) prometheus.Collector {
func NewBuildInfoCollector(program string) prometheus.Collector {

At least we can use a more straightforward name which I suggested at #6044 (comment).

return prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: program,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CharlesQQ Do you think using the component name as a prefix for the metrics is necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is usually used in combination with metrics and prometheus; when prometheus aggregates data, users usually add specific labels related to components from different sources.

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 },
)
}
7 changes: 4 additions & 3 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +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"
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')
)
30 changes: 16 additions & 14 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (

// Info contains versioning information.
type Info struct {
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
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 @@ -41,12 +42,13 @@ func (info Info) String() string {
// what code a binary was built from.
func Get() Info {
return Info{
GitVersion: gitVersion,
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),
}
}
17 changes: 9 additions & 8 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ func TestInfo_String(t *testing.T) {
{
name: "test1",
info: Info{
GitVersion: "1.3.0",
GitCommit: "da070e68f3318410c8c70ed8186a2bc4736dacbd",
GitTreeState: "clean",
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", 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
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ sigs.k8s.io/cluster-api/errors
sigs.k8s.io/cluster-api/feature
sigs.k8s.io/cluster-api/util/certs
sigs.k8s.io/cluster-api/util/secret
# sigs.k8s.io/controller-runtime v0.19.1
# sigs.k8s.io/controller-runtime v0.19.6
## explicit; go 1.22.0
sigs.k8s.io/controller-runtime
sigs.k8s.io/controller-runtime/pkg/builder
Expand Down
43 changes: 41 additions & 2 deletions vendor/sigs.k8s.io/controller-runtime/pkg/cache/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading