Skip to content

Commit

Permalink
pkg/metrics/cinder.go only publish new metrics on successful openstac…
Browse files Browse the repository at this point in the history
…k calls
  • Loading branch information
chrischdi committed Aug 31, 2020
1 parent 9ba5f4b commit 81896b0
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions pkg/metrics/cinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
"github.com/gophercloud/gophercloud/pagination"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -93,39 +92,48 @@ func registerCinderMetrics() {
// PublishCinderMetrics makes the list request to the blockstorage api and passes
// the result to a publish function.
func PublishCinderMetrics(client *gophercloud.ServiceClient, clientset *kubernetes.Clientset, tenantID string) error {
// first step: gather the data

// get the cinder pvs to add metadata
pvs, err := getPVsByCinderID(clientset)
if err != nil {
return err
}

// cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
cinderVolumeCreated.Reset()
cinderVolumeUpdatedAt.Reset()
cinderVolumeStatus.Reset()
cinderVolumeSize.Reset()
cinderVolumeAttachedAt.Reset()

// get all volumes and publish them
// get all volumes from openstack
mc := newOpenStackMetric("volume", "list")
pager := volumes.List(client, volumes.ListOpts{})
err = pager.EachPage(func(page pagination.Page) (bool, error) {
return publishVolumesPage(page, pvs)
})
pages, err := volumes.List(client, volumes.ListOpts{}).AllPages()
if mc.Observe(err) != nil {
// only warn, maybe the next list will work.
klog.Warningf("Unable to list volumes: %v", err)
return err
}
volumesList, err := volumes.ExtractVolumes(pages)
if err != nil {
return err
}

// get quotas form openstack
mc = newOpenStackMetric("volume_quotasets_usage", "get")
q, err := quotasets.GetUsage(client, tenantID).Extract()
quotas, err := quotasets.GetUsage(client, tenantID).Extract()
if mc.Observe(err) != nil {
// only warn, maybe the next get will work.
klog.Warningf("Unable to get quotas: %v", err)
return err
}
publishCinderQuotas(q)

// second step: reset the old metrics
// cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
cinderVolumeCreated.Reset()
cinderVolumeUpdatedAt.Reset()
cinderVolumeStatus.Reset()
cinderVolumeSize.Reset()
cinderVolumeAttachedAt.Reset()

// third step: publish the metrics
publishVolumes(volumesList, pvs)

publishCinderQuotas(quotas)
return nil
}

Expand All @@ -142,21 +150,15 @@ func publishCinderQuotas(q quotasets.QuotaUsageSet) {
cinderQuotaVolumesGigabyte.WithLabelValues("allocated").Set(float64(q.Gigabytes.Allocated))
}

// publishVolumesPage iterates over a page, the result of a list request
func publishVolumesPage(page pagination.Page, pvs map[string]corev1.PersistentVolume) (bool, error) {
vList, err := volumes.ExtractVolumes(page)
if err != nil {
return false, err
}

// publishVolumes iterates over a page, the result of a list request
func publishVolumes(vList []volumes.Volume, pvs map[string]corev1.PersistentVolume) {
for _, v := range vList {
if pv, ok := pvs[v.ID]; ok {
publishVolumeMetrics(v, &pv)
} else {
publishVolumeMetrics(v, nil)
}
}
return true, nil
}

// publishVolumeMetrics extracts data from a volume and exposes the metrics via prometheus
Expand Down

0 comments on commit 81896b0

Please sign in to comment.