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

Commit

Permalink
Merge pull request #9 from Duologic/duologic/fix_nil_panic
Browse files Browse the repository at this point in the history
fix: don't panic when return is nil
  • Loading branch information
primeroz authored Jul 9, 2020
2 parents e508267 + 3359e03 commit aa4c343
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 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
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
func TestScrape(t *testing.T) {

// TestSuccessfulConnection
exporter, _ := NewExporter("credentials.json", os.Getenv("GCP_PROJECT"))
exporter, _ := NewExporter(os.Getenv("GCP_PROJECT"))
up, _, _ := exporter.scrape()
if up == 0 {
t.Errorf("TestSuccessfulConnection: up=%v, expected=1", up)
}

// TestFailedConnection
// Set the project name to "503" since the Google Compute API will append this to the end of the BasePath
exporter, _ = NewExporter("credentials.json", "503")
exporter, _ = NewExporter("503")
exporter.service.BasePath = "https://httpbin.org/status/"
up, _, _ = exporter.scrape()
if up != 0 {
Expand Down

0 comments on commit aa4c343

Please sign in to comment.