Handle non-existent screenname gracefully
This commit is contained in:
parent
272e459380
commit
b45aa84b1d
17
main.go
17
main.go
|
@ -36,6 +36,14 @@ func probeTwitter(ctx context.Context, target string, registry *prometheus.Regis
|
|||
log.Errorf("decoding json: %s", err)
|
||||
return false
|
||||
}
|
||||
switch l := len(data); {
|
||||
case l == 0:
|
||||
log.Errorf("No data returned. Does screen name exist?")
|
||||
return false
|
||||
case l > 1:
|
||||
log.Errorf("Unexpected (>1) results returned. WTF twitter??")
|
||||
return false
|
||||
}
|
||||
followersGauge.With(prometheus.Labels{"screen_name": target}).Set(float64(data[0].FollowersCount))
|
||||
return true
|
||||
}
|
||||
|
@ -60,6 +68,13 @@ func twitterHandler(w http.ResponseWriter, r *http.Request) {
|
|||
h.ServeHTTP(w, r)
|
||||
|
||||
}
|
||||
|
||||
func bail(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var listenAddr string
|
||||
flag.StringVar(&listenAddr, "listen", "0.0.0.0:9700", "Address to listen on")
|
||||
|
@ -69,5 +84,5 @@ func main() {
|
|||
twitterHandler(w, r)
|
||||
})
|
||||
log.Infof("Listening on %s", listenAddr)
|
||||
http.ListenAndServe(listenAddr, nil)
|
||||
bail(http.ListenAndServe(listenAddr, nil))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue