-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (123 loc) · 3.35 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"encoding/json"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
dockerioRateLimitLimit = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "dockerio_ratelimit_limit",
Help: "Allowed pulls",
})
dockerioRateLimitRemaining = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "dockerio_ratelimit_remaining",
Help: "Remaining pulls",
})
)
type Token struct {
Token string `json:"token"`
}
func checkingRateLimit() {
log.Print("Checking Rate Limit Status")
r, err := http.Get("https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull")
if err != nil {
log.Print("Could not get token")
return
}
var token Token
err = json.NewDecoder(r.Body).Decode(&token)
if err != nil {
log.Print("Could not decode JSON from token request")
return
}
req, err := http.NewRequest("HEAD", "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest", nil)
if err != nil {
log.Print("Could not create rate limit request")
return
}
req.Header.Set("Authorization", "Bearer " + token.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Print("Could not get rate limit request")
return
}
limit, err := strconv.ParseFloat(strings.Split(resp.Header.Get("RateLimit-Limit"), ";")[0], 64)
remaining, err := strconv.ParseFloat(strings.Split(resp.Header.Get("RateLimit-Remaining"), ";")[0], 64)
if err != nil {
// Save a copy of this request for debugging.
requestDump, _ := httputil.DumpResponse(resp, true)
log.Print("Could not parse RateLimit headers")
log.Print(string(requestDump))
return
}
dockerioRateLimitLimit.Set(limit)
dockerioRateLimitRemaining.Set(remaining)
}
func main() {
ctx, cancelContext := context.WithCancel(context.Background())
r := prometheus.NewRegistry()
r.MustRegister(dockerioRateLimitLimit)
r.MustRegister(dockerioRateLimitRemaining)
ticker := time.NewTicker(10 * time.Second)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
checkingRateLimit()
go func() {
for {
select {
case <- ticker.C:
checkingRateLimit()
}
}
}()
mux := http.NewServeMux()
handler := promhttp.HandlerFor(r, promhttp.HandlerOpts{})
mux.Handle("/metrics", handler)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Pull Rate Limit Exporter</title></head>
<body>
<h1>Pull Rate Limit Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>
`))
})
httpServer := &http.Server{
Addr: ":2342",
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
go func() {
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
// it is fine to use Fatal here because it is not main gorutine
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}()
shutdownCtx, cancelShutdownContext := context.WithTimeout(context.Background(), 5 * time.Second)
for {
select {
case <-sigs:
log.Print("Handling sigs")
cancelContext()
ticker.Stop()
httpServer.RegisterOnShutdown(cancelShutdownContext)
_ = httpServer.Shutdown(shutdownCtx)
return
}
}
os.Exit(0)
}