From a4bd3262619474247940eb83fe24b649fc8efc89 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Fri, 4 Sep 2020 14:42:56 +0200 Subject: [PATCH] Add vCenter info metric Metric provides information about used vCenter version for every connected vCenter host. --- .../vsphere/vclib/connection.go | 2 + .../vsphere/vclib/vsphere_metrics.go | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection.go b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection.go index b726cc1aa49..ce6a4bdb153 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection.go +++ b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/connection.go @@ -65,6 +65,7 @@ func (connection *VSphereConnection) Connect(ctx context.Context) error { klog.Errorf("Failed to create govmomi client. err: %+v", err) return err } + setVCenterInfoMetric(connection) return nil } m := session.NewManager(connection.Client) @@ -83,6 +84,7 @@ func (connection *VSphereConnection) Connect(ctx context.Context) error { klog.Errorf("Failed to create govmomi client. err: %+v", err) return err } + setVCenterInfoMetric(connection) return nil } diff --git a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/vsphere_metrics.go b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/vsphere_metrics.go index 4b25a811078..b5eafc0038b 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/vsphere_metrics.go +++ b/staging/src/k8s.io/legacy-cloud-providers/vsphere/vclib/vsphere_metrics.go @@ -17,8 +17,10 @@ limitations under the License. package vclib import ( + "sync" "time" + "github.com/vmware/govmomi/vim25/types" "k8s.io/component-base/metrics" "k8s.io/component-base/metrics/legacyregistry" ) @@ -43,6 +45,15 @@ const ( OperationCreateVolumeWithRawVSANPolicy = "CreateVolumeWithRawVSANPolicyOperation" ) +var vCenterMetric *vcenterMetric + +func init() { + vCenterMetric = &vcenterMetric{ + vCenterInfos: make(map[string]types.AboutInfo), + mux: sync.Mutex{}, + } +} + // vsphereAPIMetric is for recording latency of Single API Call. var vsphereAPIMetric = metrics.NewHistogramVec( &metrics.HistogramOpts{ @@ -81,12 +92,55 @@ var vsphereOperationErrorMetric = metrics.NewCounterVec( []string{"operation"}, ) +var vsphereVersion = metrics.NewDesc( + "cloudprovider_vsphere_vcenter_versions", + "Versions for connected vSphere vCenters", + []string{"hostname", "version", "build"}, nil, + metrics.ALPHA, "") + // RegisterMetrics registers all the API and Operation metrics func RegisterMetrics() { legacyregistry.MustRegister(vsphereAPIMetric) legacyregistry.MustRegister(vsphereAPIErrorMetric) legacyregistry.MustRegister(vsphereOperationMetric) legacyregistry.MustRegister(vsphereOperationErrorMetric) + legacyregistry.CustomMustRegister(vCenterMetric) +} + +type vcenterMetric struct { + metrics.BaseStableCollector + + mux sync.Mutex + vCenterInfos map[string]types.AboutInfo +} + +func (collector *vcenterMetric) DescribeWithStability(ch chan<- *metrics.Desc) { + ch <- vsphereVersion +} + +func (collector *vcenterMetric) CollectWithStability(ch chan<- metrics.Metric) { + collector.mux.Lock() + defer collector.mux.Unlock() + + for vCenter, info := range collector.vCenterInfos { + ch <- metrics.NewLazyMetricWithTimestamp(time.Now(), + metrics.NewLazyConstMetric(vsphereVersion, + metrics.GaugeValue, + float64(1), + vCenter, + info.Version, + info.Build)) + } +} + +func (collector *vcenterMetric) setAbout(server string, info types.AboutInfo) { + collector.mux.Lock() + defer collector.mux.Unlock() + collector.vCenterInfos[server] = info +} + +func setVCenterInfoMetric(connection *VSphereConnection) { + vCenterMetric.setAbout(connection.Hostname, connection.Client.ServiceContent.About) } // RecordvSphereMetric records the vSphere API and Operation metrics