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 8 commits 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
14 changes: 14 additions & 0 deletions hack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,21 @@ function util::get_version() {
git describe --tags --dirty
}

function util::get_branch() {
git rev-parse --abbrev-ref HEAD
}

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"
Expand All @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"flag"
"fmt"
"os"
"regexp"

"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
restclient "k8s.io/client-go/rest"
Expand All @@ -32,6 +34,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"
Expand Down Expand Up @@ -110,6 +113,34 @@ func Run(ctx context.Context, o *options.Options) error {
return err
}

// Unregister default NewGoCollector
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"),
)

controllerCtx := ctrlctx.Context{
Controllers: o.Controllers,
Manager: manager,
Expand Down
2 changes: 2 additions & 0 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
32 changes: 32 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Copy link
Member

Choose a reason for hiding this comment

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

Is the GitRevision redundant with GitCommit?

Copy link
Contributor Author

@dongjiang1989 dongjiang1989 Jan 20, 2025

Choose a reason for hiding this comment

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

GitCommit for git commit ID from git rev-parse HEAD and GitRevision for commit short ID from git rev-parse --short HEAD. Short ID is more friendly to CTL version help info and prometheus metric tag value.

GitTreeState string `json:"gitTreeState"`
GitBranch string `json:"gitBranch"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Expand All @@ -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 },
)
}
4 changes: 3 additions & 1 deletion pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down