44 "encoding/json"
55 "github.com/derwolfe/ticktock/parsing"
66 "github.com/derwolfe/ticktock/state"
7+ "github.com/prometheus/client_golang/prometheus"
8+ "github.com/prometheus/client_golang/prometheus/promhttp"
79 "io/ioutil"
810 "log"
911 "net/http"
@@ -20,7 +22,20 @@ const (
2022)
2123
2224// GLOBAL :barf:
23- var DataStore = state .NewStore ()
25+ var (
26+ DataStore = state .NewStore ()
27+ inflightStatus = prometheus .NewGauge (
28+ prometheus.GaugeOpts {
29+ Namespace : "ticktock" ,
30+ Subsystem : "status_checks" ,
31+ Name : "in_flight" ,
32+ Help : "Number of in flight status checks." ,
33+ })
34+ )
35+
36+ func metricsInit () {
37+ prometheus .MustRegister (inflightStatus )
38+ }
2439
2540func statusFetch (url string ) (* []byte , error ) {
2641 timeout := time .Duration (TIMEOUT * time .Second )
@@ -47,10 +62,14 @@ func updateState(store *state.Store) {
4762
4863 for _ , url := range sources {
4964 go func (url string ) {
65+ inflightStatus .Inc ()
66+ defer inflightStatus .Dec ()
5067 defer wg .Done ()
5168 body , err := statusFetch (url )
69+ // bail out after a few attempts if we've encountered a few errors
5270 if err != nil {
53- panic (err )
71+ log .Printf ("Error fetching: %s, %s" , url , err )
72+ return
5473 }
5574
5675 var good bool
@@ -87,6 +106,7 @@ func status(w http.ResponseWriter, r *http.Request) {
87106}
88107
89108func main () {
109+ metricsInit ()
90110 ticker := time .NewTicker (1 * time .Minute )
91111
92112 updateState (DataStore )
@@ -100,6 +120,7 @@ func main() {
100120 }
101121 }()
102122
123+ http .Handle ("/metrics" , promhttp .Handler ())
103124 http .HandleFunc ("/" , status ) // set router
104125 http .ListenAndServe (":9090" , nil )
105126
0 commit comments