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 10 commits into
base: master
Choose a base branch
from
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)
dongjiang1989 marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 20 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,23 @@ func Run(ctx context.Context, o *options.Options) error {
return err
}

// Unregister default NewGoCollector
ctrlmetrics.Registry.Unregister(collectors.NewGoCollector())

ctrlmetrics.Registry.MustRegister(
collectors.NewGoCollector(
collectors.WithGoCollectorRuntimeMetrics(
collectors.MetricsGC,
collectors.MetricsScheduler,
collectors.MetricsMemory,
collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile(`^/sync/.*`)},
Copy link
Member

Choose a reason for hiding this comment

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

I'm not familiar with these metrics either.
Could you add some simple comments indicating the purpose or usage of each metric?

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.

Thanks @chaosi-zju .
I got it.
Add comments for each metric.

Please re-check it.

BTW, I will extend these metrics to all other components.

),
),
)
dongjiang1989 marked this conversation as resolved.
Show resolved Hide resolved
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"`
dongjiang1989 marked this conversation as resolved.
Show resolved Hide resolved
dongjiang1989 marked this conversation as resolved.
Show resolved Hide resolved
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,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can omit the component name?
Because the metric emitted from a component indicates it's build info.

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