Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
fix: don't panic when return is nil
Browse files Browse the repository at this point in the history
If the scrape fails, the return is nil for either project or regionList.
We should not try to access project.Quotas or regionList.Items in that
case.

Bonus: Even if the regionList call fails, we can still return the
project level results.

fixes #8
  • Loading branch information
Duologic committed Jul 3, 2020
1 parent e508267 commit 8bd2921
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (e *Exporter) scrape() (up float64, prj *compute.Project, rgl *compute.Regi
regionList, err := e.service.Regions.List(e.project).Do()
if err != nil {
log.Errorf("Failure when querying region quotas: %v", err)
return 0, nil, nil
return 0, project, nil
}

return 1, project, regionList
Expand All @@ -91,16 +91,20 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

up, project, regionList := e.scrape()

for _, quota := range project.Quotas {
ch <- prometheus.MustNewConstMetric(limitDesc, prometheus.GaugeValue, quota.Limit, e.project, "", quota.Metric)
ch <- prometheus.MustNewConstMetric(usageDesc, prometheus.GaugeValue, quota.Usage, e.project, "", quota.Metric)
if project != nil {
for _, quota := range project.Quotas {
ch <- prometheus.MustNewConstMetric(limitDesc, prometheus.GaugeValue, quota.Limit, e.project, "", quota.Metric)
ch <- prometheus.MustNewConstMetric(usageDesc, prometheus.GaugeValue, quota.Usage, e.project, "", quota.Metric)
}
}

for _, region := range regionList.Items {
regionName := region.Name
for _, quota := range region.Quotas {
ch <- prometheus.MustNewConstMetric(limitDesc, prometheus.GaugeValue, quota.Limit, e.project, regionName, quota.Metric)
ch <- prometheus.MustNewConstMetric(usageDesc, prometheus.GaugeValue, quota.Usage, e.project, regionName, quota.Metric)
if regionList != nil {
for _, region := range regionList.Items {
regionName := region.Name
for _, quota := range region.Quotas {
ch <- prometheus.MustNewConstMetric(limitDesc, prometheus.GaugeValue, quota.Limit, e.project, regionName, quota.Metric)
ch <- prometheus.MustNewConstMetric(usageDesc, prometheus.GaugeValue, quota.Usage, e.project, regionName, quota.Metric)
}
}
}

Expand Down

0 comments on commit 8bd2921

Please sign in to comment.