diff --git a/cmd/yurt-manager/app/manager.go b/cmd/yurt-manager/app/manager.go index 93c4ae5647e..c6624afb27e 100644 --- a/cmd/yurt-manager/app/manager.go +++ b/cmd/yurt-manager/app/manager.go @@ -35,6 +35,7 @@ import ( "k8s.io/klog/v2/klogr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" + "sigs.k8s.io/controller-runtime/pkg/metrics" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" runtimewebhook "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -95,6 +96,7 @@ current state towards the desired state.`, if s.Generic.Version { return } + projectinfo.RegisterVersionInfo(metrics.Registry, projectinfo.GetYurtManagerName()) PrintFlags(cmd.Flags()) diff --git a/cmd/yurthub/app/start.go b/cmd/yurthub/app/start.go index 38324eeb088..4b2b7f7e4a4 100644 --- a/cmd/yurthub/app/start.go +++ b/cmd/yurthub/app/start.go @@ -62,6 +62,7 @@ func NewCmdStartYurtHub(ctx context.Context) *cobra.Command { return } fmt.Printf("%s version: %#v\n", projectinfo.GetHubName(), projectinfo.Get()) + projectinfo.RegisterVersionInfo(nil, projectinfo.GetHubName()) cmd.Flags().VisitAll(func(flag *pflag.Flag) { klog.V(1).Infof("FLAG: --%s=%q", flag.Name, flag.Value) diff --git a/pkg/projectinfo/metrics.go b/pkg/projectinfo/metrics.go new file mode 100644 index 00000000000..657a7cdd2df --- /dev/null +++ b/pkg/projectinfo/metrics.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 The OpenYurt 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 projectinfo + +import ( + "github.com/prometheus/client_golang/prometheus" + + yurtutil "github.com/openyurtio/openyurt/pkg/util" +) + +var ( + buildInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "openyurt", + Name: "version_info", + Help: "A metric with a constant '1' value labeled by git version, git commit, build date, Go version, compiler and nodepoollabelkey from which OpenYurt was built, and platform on which it is running.", + }, + []string{"component_name", "git_version", "git_commit", "build_date", "go_version", "compiler", "platform", "nodepoollabelkey"}, + ) +) + +func RegisterVersionInfo(reg prometheus.Registerer, componentName string) { + info := Get() + if yurtutil.IsNil(reg) { + prometheus.MustRegister(buildInfo) + } else { + reg.MustRegister(buildInfo) + } + buildInfo.WithLabelValues(componentName, info.GitVersion, info.GitCommit, info.BuildDate, info.GoVersion, info.Compiler, info.Platform, info.NodePoolLabelKey).Set(1) +}