Add metrics

This commit is contained in:
Kasper Juul Hermansen 2022-02-14 01:42:18 +01:00
parent cb0917d916
commit 80eb11b0c9
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
3 changed files with 24 additions and 9 deletions

25
main.go
View File

@ -6,9 +6,11 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/eko/gocache/cache" "github.com/eko/gocache/cache"
"github.com/eko/gocache/metrics"
"github.com/eko/gocache/store" "github.com/eko/gocache/store"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"io/ioutil" "io/ioutil"
@ -77,11 +79,20 @@ func BasicAuthMiddleware(l *zap.Logger, us *users.Service) gin.HandlerFunc {
c.Next() c.Next()
} }
} }
func setupApi(l *zap.Logger, cc *cache.Cache, us *users.Service, ps *projects.Service) { func setupApi(l *zap.Logger, cc *cache.MetricCache, us *users.Service, ps *projects.Service) {
l.Info("Setting up serverctl setupApi (using gin)") l.Info("Setting up serverctl setupApi (using gin)")
r := gin.Default() r := gin.Default()
promHandler := func() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
r.GET("/metrics", promHandler())
r.POST("/auth/register", func(c *gin.Context) { r.POST("/auth/register", func(c *gin.Context) {
type RegisterUser struct { type RegisterUser struct {
Email string `json:"email" binding:"required"` Email string `json:"email" binding:"required"`
@ -185,22 +196,26 @@ func setupDocker(l *zap.Logger) *client.Client {
return cli return cli
} }
func setupCache(l *zap.Logger) *cache.Cache { func setupCache(l *zap.Logger) *cache.MetricCache {
l.Info("Setting up cache") l.Info("Setting up cache")
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{ ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1000, NumCounters: 1000,
MaxCost: 100_000_000, MaxCost: 100_000_000,
BufferItems: 64, BufferItems: 64,
}) })
promMetrics := metrics.NewPrometheus("serverctl")
if err != nil { if err != nil {
panic(err) panic(err)
} }
ristrettoStore := store.NewRistretto(ristrettoCache, nil) ristrettoStore := store.NewRistretto(ristrettoCache, nil)
cacheManager := cache.New(ristrettoStore)
return cacheManager cacheManager := cache.New(ristrettoStore)
metricsCache := cache.NewMetric(promMetrics, cacheManager)
return metricsCache
} }
func setupCron(l *zap.Logger, cm *cache.Cache, cc *client.Client) { func setupCron(l *zap.Logger, cm *cache.MetricCache, cc *client.Client) {
l.Info("Setting up job scheduler (cron)") l.Info("Setting up job scheduler (cron)")
s := gocron.NewScheduler(time.UTC) s := gocron.NewScheduler(time.UTC)

View File

@ -10,10 +10,10 @@ import (
type Service struct { type Service struct {
projectsRepository Repository projectsRepository Repository
logger *zap.Logger logger *zap.Logger
cache *cache.Cache cache *cache.MetricCache
} }
func NewService(logger *zap.Logger, projectsRepository Repository, cache *cache.Cache) *Service { func NewService(logger *zap.Logger, projectsRepository Repository, cache *cache.MetricCache) *Service {
return &Service{ return &Service{
logger: logger, logger: logger,
projectsRepository: projectsRepository, projectsRepository: projectsRepository,

View File

@ -9,12 +9,12 @@ import (
type Service struct { type Service struct {
logger *zap.Logger logger *zap.Logger
cache *cache.Cache cache *cache.MetricCache
repository Repository repository Repository
passwordHasher PasswordHasher passwordHasher PasswordHasher
} }
func NewService(l *zap.Logger, ur Repository, c *cache.Cache) *Service { func NewService(l *zap.Logger, ur Repository, c *cache.MetricCache) *Service {
return &Service{ return &Service{
logger: l, logger: l,
repository: ur, repository: ur,