Skip to content

Commit 0add4a8

Browse files
committed
add prom
1 parent 7302502 commit 0add4a8

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

ticktock.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
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

2540
func 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

89108
func 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

Comments
 (0)