git » prometheus-expvar-exporter » commit e771723

Do not panic on errors scraping

author Alberto Bertogli
2019-10-31 21:10:32 UTC
committer Alberto Bertogli
2019-10-31 21:10:32 UTC
parent bc1cd03ba8384a6dbe5106910c9c1c04a2fa9e44

Do not panic on errors scraping

main.go +6 -3

diff --git a/main.go b/main.go
index ed4f659..f3e743a 100644
--- a/main.go
+++ b/main.go
@@ -97,12 +97,14 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
 func (c *Collector) Collect(ch chan<- prometheus.Metric) {
 	resp, err := http.Get(c.url)
 	if err != nil {
-		panic(err)
+		log.Printf("Error scraping %q: %v", c.url, err)
+		return
 	}
 
 	body, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
-		panic(err)
+		log.Printf("Error reading body of %q: %v", c.url, err)
+		return
 	}
 
 	// Replace "\xNN" with "?" because the default parser doesn't handle them
@@ -115,7 +117,8 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
 	var vs map[string]interface{}
 	err = json.Unmarshal(body, &vs)
 	if err != nil {
-		panic(err)
+		log.Printf("Error unmarshalling json from %q: %v", c.url, err)
+		return
 	}
 
 	for k, v := range vs {