Skip to content

Commit

Permalink
Merge pull request kubernetes#94526 from Danil-Grigorev/metrics-vcent…
Browse files Browse the repository at this point in the history
…er-version

Add vCenter info metric
  • Loading branch information
k8s-ci-robot authored Nov 10, 2020
2 parents d41f791 + a4bd326 commit 0f6d1ed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f6d1ed

Please sign in to comment.