track success/fails

This commit is contained in:
Stephen McQuay 2018-03-08 14:33:24 -08:00
parent a617999440
commit ea33973994
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5

View File

@ -9,11 +9,14 @@ import (
"sync" "sync"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"mcquay.me/hw" "mcquay.me/hw"
"mcquay.me/metrics" "mcquay.me/metrics"
) )
var success *prometheus.CounterVec
type v struct { type v struct {
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
V string `json:"version"` V string `json:"version"`
@ -38,9 +41,10 @@ func (s *state) update(target string) {
req.Close = true req.Close = true
httpResp, err := http.DefaultClient.Do(req) httpResp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Printf("get: %+v", err) success.WithLabelValues("false").Inc()
continue continue
} }
success.WithLabelValues("true").Inc()
rv := v{} rv := v{}
if err := json.NewDecoder(httpResp.Body).Decode(&rv); err != nil { if err := json.NewDecoder(httpResp.Body).Decode(&rv); err != nil {
panic(err) panic(err)
@ -67,6 +71,18 @@ func main() {
log.Fatalf("metrics: %v", err) log.Fatalf("metrics: %v", err)
} }
success = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "hwc_success_count",
Help: "counts successes and failures of fetch attempts",
},
[]string{"ok"},
)
if err := prometheus.Register(success); err != nil {
log.Fatalf("registering success: %v", err)
}
hn, err := os.Hostname() hn, err := os.Hostname()
if err != nil { if err != nil {
log.Fatalf("hostname: %+v", err) log.Fatalf("hostname: %+v", err)