-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"io"
"log"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tidwall/gjson"
)
func getResultFromAPI(theAttr string, theTarget string) {
go func() {
// Forever...
for {
// Get the response from the API and log + exit on any error
resp, err := http.Get(theTarget)
if err != nil {
log.Fatalln(err)
}
// If all okay so far, read the response body and log + exit on any error
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
// Close the HTTP response
resp.Body.Close()
// If all okay so far, convert the body to a string and grab the attribute
stringBody := string(body)
the_result := gjson.Get(stringBody, theAttr)
// Update the Prometheus gauge with the float value of the attribute
calicoNodePullCountGauge.Set(the_result.Float())
// Wait 5 minutes and do it all again
time.Sleep(3 * time.Minute)
}
}()
}
var (
calicoNodePullCountGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "caliconodedockerstats_gauge",
Help: "calico-node's pull_count from Docker Hub",
})
)
func main() {
// Grab from the API target details from the env
envAttr := os.Getenv("CALICONODEDOCKERSTATS_ATTR_NAME")
envTarget := os.Getenv("CALICONODEDOCKERSTATS_TARGET_NAME")
// Get the API result and update the Prometheus gauge with the value
getResultFromAPI(envAttr, envTarget)
// Wait 10 seconds to make sure the attribute has updated before starting listener
time.Sleep(10 * time.Second)
// Start the Prometheus metrics listener
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9088", nil)
}