Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit 81896b0

Browse files
committed
pkg/metrics/cinder.go only publish new metrics on successful openstack calls
1 parent 9ba5f4b commit 81896b0

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

pkg/metrics/cinder.go

+25-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/gophercloud/gophercloud"
77
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
88
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
9-
"github.com/gophercloud/gophercloud/pagination"
109
"github.com/prometheus/client_golang/prometheus"
1110
corev1 "k8s.io/api/core/v1"
1211
"k8s.io/client-go/kubernetes"
@@ -93,39 +92,48 @@ func registerCinderMetrics() {
9392
// PublishCinderMetrics makes the list request to the blockstorage api and passes
9493
// the result to a publish function.
9594
func PublishCinderMetrics(client *gophercloud.ServiceClient, clientset *kubernetes.Clientset, tenantID string) error {
95+
// first step: gather the data
96+
9697
// get the cinder pvs to add metadata
9798
pvs, err := getPVsByCinderID(clientset)
9899
if err != nil {
99100
return err
100101
}
101102

102-
// cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
103-
cinderVolumeCreated.Reset()
104-
cinderVolumeUpdatedAt.Reset()
105-
cinderVolumeStatus.Reset()
106-
cinderVolumeSize.Reset()
107-
cinderVolumeAttachedAt.Reset()
108-
109-
// get all volumes and publish them
103+
// get all volumes from openstack
110104
mc := newOpenStackMetric("volume", "list")
111-
pager := volumes.List(client, volumes.ListOpts{})
112-
err = pager.EachPage(func(page pagination.Page) (bool, error) {
113-
return publishVolumesPage(page, pvs)
114-
})
105+
pages, err := volumes.List(client, volumes.ListOpts{}).AllPages()
115106
if mc.Observe(err) != nil {
116107
// only warn, maybe the next list will work.
117108
klog.Warningf("Unable to list volumes: %v", err)
118109
return err
119110
}
111+
volumesList, err := volumes.ExtractVolumes(pages)
112+
if err != nil {
113+
return err
114+
}
120115

116+
// get quotas form openstack
121117
mc = newOpenStackMetric("volume_quotasets_usage", "get")
122-
q, err := quotasets.GetUsage(client, tenantID).Extract()
118+
quotas, err := quotasets.GetUsage(client, tenantID).Extract()
123119
if mc.Observe(err) != nil {
124120
// only warn, maybe the next get will work.
125121
klog.Warningf("Unable to get quotas: %v", err)
126122
return err
127123
}
128-
publishCinderQuotas(q)
124+
125+
// second step: reset the old metrics
126+
// cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
127+
cinderVolumeCreated.Reset()
128+
cinderVolumeUpdatedAt.Reset()
129+
cinderVolumeStatus.Reset()
130+
cinderVolumeSize.Reset()
131+
cinderVolumeAttachedAt.Reset()
132+
133+
// third step: publish the metrics
134+
publishVolumes(volumesList, pvs)
135+
136+
publishCinderQuotas(quotas)
129137
return nil
130138
}
131139

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

145-
// publishVolumesPage iterates over a page, the result of a list request
146-
func publishVolumesPage(page pagination.Page, pvs map[string]corev1.PersistentVolume) (bool, error) {
147-
vList, err := volumes.ExtractVolumes(page)
148-
if err != nil {
149-
return false, err
150-
}
151-
153+
// publishVolumes iterates over a page, the result of a list request
154+
func publishVolumes(vList []volumes.Volume, pvs map[string]corev1.PersistentVolume) {
152155
for _, v := range vList {
153156
if pv, ok := pvs[v.ID]; ok {
154157
publishVolumeMetrics(v, &pv)
155158
} else {
156159
publishVolumeMetrics(v, nil)
157160
}
158161
}
159-
return true, nil
160162
}
161163

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

0 commit comments

Comments
 (0)