From 80eb11b0c97fdaa43a3e9694783b984dcb44b0b7 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Mon, 14 Feb 2022 01:42:18 +0100 Subject: [PATCH] Add metrics --- main.go | 25 ++++++++++++++++++++----- pkg/application/projects/service.go | 4 ++-- pkg/application/users/service.go | 4 ++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 23fec11..87fe703 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,11 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/client" "github.com/eko/gocache/cache" + "github.com/eko/gocache/metrics" "github.com/eko/gocache/store" "github.com/gin-gonic/gin" "github.com/go-co-op/gocron" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" "go.uber.org/zap/zapcore" "io/ioutil" @@ -77,11 +79,20 @@ func BasicAuthMiddleware(l *zap.Logger, us *users.Service) gin.HandlerFunc { 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)") 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) { type RegisterUser struct { Email string `json:"email" binding:"required"` @@ -185,22 +196,26 @@ func setupDocker(l *zap.Logger) *client.Client { return cli } -func setupCache(l *zap.Logger) *cache.Cache { +func setupCache(l *zap.Logger) *cache.MetricCache { l.Info("Setting up cache") ristrettoCache, err := ristretto.NewCache(&ristretto.Config{ NumCounters: 1000, MaxCost: 100_000_000, BufferItems: 64, }) + promMetrics := metrics.NewPrometheus("serverctl") + if err != nil { panic(err) } 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)") s := gocron.NewScheduler(time.UTC) diff --git a/pkg/application/projects/service.go b/pkg/application/projects/service.go index 7967158..e1c9839 100644 --- a/pkg/application/projects/service.go +++ b/pkg/application/projects/service.go @@ -10,10 +10,10 @@ import ( type Service struct { projectsRepository Repository 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{ logger: logger, projectsRepository: projectsRepository, diff --git a/pkg/application/users/service.go b/pkg/application/users/service.go index bf7c192..77171bf 100644 --- a/pkg/application/users/service.go +++ b/pkg/application/users/service.go @@ -9,12 +9,12 @@ import ( type Service struct { logger *zap.Logger - cache *cache.Cache + cache *cache.MetricCache repository Repository 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{ logger: l, repository: ur,