export metrics via prometheus
This commit is contained in:
parent
79c275fbf8
commit
93488bc172
2 changed files with 38 additions and 6 deletions
|
|
@ -27,14 +27,14 @@ func (d *Dockerhub) GetLimits() error {
|
|||
log.Log.Error("Retreived invalid status code, while fetching limit: ", resp.StatusCode())
|
||||
return errors.New("Retreived invalid status code")
|
||||
}
|
||||
if containsKey(resp.Header(), "RateLimit-Remaining") {
|
||||
if containsKey(resp.Header(), "Ratelimit-Remaining") {
|
||||
d.Remaining = extractLimit(resp.Header()["Ratelimit-Remaining"][0])
|
||||
}
|
||||
if containsKey(resp.Header(), "RateLimit-Limit") {
|
||||
if containsKey(resp.Header(), "Ratelimit-Limit") {
|
||||
d.Limit = extractLimit(resp.Header()["Ratelimit-Limit"][0])
|
||||
}
|
||||
if containsKey(resp.Header(), "Docker-RateLimit-Source") {
|
||||
d.LimitSource = resp.Header()["Docker-RateLimit-Source"][0]
|
||||
if containsKey(resp.Header(), "Docker-Ratelimit-Source") {
|
||||
d.LimitSource = resp.Header()["Docker-Ratelimit-Source"][0]
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
36
main.go
36
main.go
|
|
@ -2,8 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"jonasled.dev/jonasled/dockerhub-ratelimit-exporter/dockerhub"
|
||||
"jonasled.dev/jonasled/dockerhub-ratelimit-exporter/log"
|
||||
|
|
@ -14,9 +19,36 @@ func main() {
|
|||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
hub := dockerhub.Dockerhub{}
|
||||
go hub.GetLimits()
|
||||
go metricsTask()
|
||||
|
||||
log.Log.Info("Listening on :8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
var (
|
||||
remainingRequests = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "dockerhub_limit_remaining_requests_total",
|
||||
Help: "Docker Hub Rate Limit Remaining Requests",
|
||||
}, []string{"source_ip"})
|
||||
maximumRequests = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Name: "dockerhub_limit_max_requests_total",
|
||||
Help: "Docker Hub Rate Limit Maximum Requests",
|
||||
}, []string{"source_ip"})
|
||||
)
|
||||
|
||||
func metricsTask() {
|
||||
hub := dockerhub.Dockerhub{}
|
||||
checkInterval, err := strconv.Atoi(os.Getenv("CHECK_INTERVAL"))
|
||||
if err != nil || checkInterval == 0 {
|
||||
log.Log.Warn("Invalid or no CHECK_INTERVAL defined, using default value of 60s")
|
||||
checkInterval = 60
|
||||
}
|
||||
for {
|
||||
hub.GetLimits()
|
||||
log.Log.Info("Retreived data for ", hub.LimitSource)
|
||||
maximumRequests.WithLabelValues(hub.LimitSource).Set(float64(hub.Limit))
|
||||
remainingRequests.WithLabelValues(hub.LimitSource).Set(float64(hub.Remaining))
|
||||
time.Sleep(time.Duration(checkInterval) * time.Second)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue