Skip to content

Commit

Permalink
Log consistency
Browse files Browse the repository at this point in the history
Use `googleapi.Error` consistently
  • Loading branch information
DazWilkin committed Aug 9, 2023
1 parent ccf2362 commit aa24021
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
4 changes: 4 additions & 0 deletions collector/artifactregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func (c *ArtifactRegistryCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Artifact Registry API has not been enabled for Project (p)
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down
8 changes: 8 additions & 0 deletions collector/cloudrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func (c *CloudRunCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Cloud Run Admin API has not been used in this project
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down Expand Up @@ -143,7 +147,11 @@ func (c *CloudRunCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Cloud Run Admin API has not been used in this project
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down
3 changes: 3 additions & 0 deletions collector/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ func (c *EndpointsCollector) Collect(ch chan<- prometheus.Metric) {
// Probably Service Management API has not been enabled for Project (p)
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down
17 changes: 10 additions & 7 deletions collector/eventarc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/DazWilkin/gcp-exporter/gcp"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/eventarc/v1"
"google.golang.org/api/googleapi"
)
Expand Down Expand Up @@ -70,7 +69,7 @@ func (c *EventarcCollector) Collect(ch chan<- prometheus.Metric) {

// Channels
wg.Add(1)
go func(p *cloudresourcemanager.Project) {
go func() {
defer wg.Done()

rqst := eventarcService.Projects.Locations.Channels.List(parent)
Expand All @@ -81,7 +80,9 @@ func (c *EventarcCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Eventarc API has not enabled in this Project
return
}
log.Printf("Google API Error: %d (%s)", e.Code, e.Message)

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
Expand All @@ -102,11 +103,11 @@ func (c *EventarcCollector) Collect(ch chan<- prometheus.Metric) {
}...,
)
}
}(p)
}()

// Triggers
wg.Add(1)
go func(p *cloudresourcemanager.Project) {
go func() {
defer wg.Done()

rqst := eventarcService.Projects.Locations.Triggers.List(parent)
Expand All @@ -117,7 +118,9 @@ func (c *EventarcCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Eventarc API has not enabled in this Project
return
}
log.Printf("Google API Error: %d (%s)", e.Code, e.Message)

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
Expand Down Expand Up @@ -152,7 +155,7 @@ func (c *EventarcCollector) Collect(ch chan<- prometheus.Metric) {
}...,
)
}
}(p)
}()
}
wg.Wait()
}
Expand Down
3 changes: 3 additions & 0 deletions collector/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ func (c *FunctionsCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Cloud Functions API has not been enabled for Project (p)
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions collector/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (c *KubernetesCollector) Collect(ch chan<- prometheus.Metric) {
// Probably (!) Kubernetes Engine API has not been enabled for Project (p)
return
}

log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return
}

log.Println(err)
Expand Down
17 changes: 17 additions & 0 deletions collector/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"fmt"
"log"
"net/http"
"sync"

"github.com/DazWilkin/gcp-exporter/gcp"
"github.com/prometheus/client_golang/prometheus"

cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
cloudscheduler "google.golang.org/api/cloudscheduler/v1"
"google.golang.org/api/googleapi"
)

var (
Expand Down Expand Up @@ -77,12 +79,27 @@ func (c *SchedulerCollector) Collect(ch chan<- prometheus.Metric) {
// }
return nil
}); err != nil {
if e, ok := err.(*googleapi.Error); ok {
log.Printf("Google API Error: %d [%s]", e.Code, e.Message)
return nil
}

log.Println(err)
return nil
}
}
return nil
}); err != nil {
if e, ok := err.(*googleapi.Error); ok {
if e.Code == http.StatusForbidden {
// Probably (!) Cloud Scheduler API has not been enabled for Project (p)
return
}

log.Printf("Googe API Error: %d (%s)", e.Code, e.Message)
return
}

log.Println(err)
return
}
Expand Down

0 comments on commit aa24021

Please sign in to comment.