Handle non-existent screenname gracefully

This commit is contained in:
Sam W 2022-08-04 02:04:16 +03:00
parent 272e459380
commit b45aa84b1d
1 changed files with 16 additions and 1 deletions

17
main.go
View File

@ -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))
}