58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
_ "github.com/joho/godotenv/autoload"
|
|
ginlogrus "github.com/toorop/gin-logrus"
|
|
"jonasled.dev/jonasled/dockerhub-ratelimit-exporter/dockerhub"
|
|
"jonasled.dev/jonasled/dockerhub-ratelimit-exporter/log"
|
|
"jonasled.dev/jonasled/dockerhub-ratelimit-exporter/metrics"
|
|
)
|
|
|
|
func main() {
|
|
log.Init()
|
|
r := gin.New()
|
|
r.Use(ginlogrus.Logger(log.Log), gin.Recovery())
|
|
r.GET("/ready", func(ctx *gin.Context) {
|
|
ctx.String(http.StatusOK, "OK")
|
|
})
|
|
r.GET("/", func(ctx *gin.Context) {
|
|
ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte("DockerHub Ratelimit exporter visit <a href=\"/metrics\">/metrics</a> for more information"))
|
|
})
|
|
metrics.InitPrometheus(r)
|
|
|
|
go metricsTask()
|
|
|
|
log.Log.Info("Start listening on port 8080")
|
|
if os.Getenv("TLS_CERT") == "" {
|
|
r.Run(":8080")
|
|
} else {
|
|
r.RunTLS(":8080", os.Getenv("TLS_CERT"), os.Getenv("TLS_KEY"))
|
|
}
|
|
|
|
}
|
|
|
|
func metricsTask() {
|
|
hub := dockerhub.Dockerhub{}
|
|
hub.GetLimits()
|
|
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 {
|
|
err := hub.GetLimits()
|
|
log.Log.Info("Retreived data for ", hub.LimitSource)
|
|
if err != nil {
|
|
metrics.MaximumRequests.WithLabelValues(hub.LimitSource).Set(float64(hub.Limit))
|
|
metrics.RemainingRequests.WithLabelValues(hub.LimitSource).Set(float64(hub.Remaining))
|
|
}
|
|
time.Sleep(time.Duration(checkInterval) * time.Second)
|
|
}
|
|
|
|
}
|