From de24fcc2f8f7c3baa60c1cc3e9c0880d15e49785 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 12 May 2019 16:49:16 +0200 Subject: [PATCH] Fixed metrics endpoint not working --- pkg/cmd/cmd.go | 31 ++++++++++++++++++++++++++++++- pkg/cmd/web.go | 18 ------------------ pkg/metrics/metrics.go | 8 ++++++-- pkg/red/redis.go | 5 +++-- pkg/routes/routes.go | 2 ++ 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 066a797fae9..fd4668b12bc 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -18,6 +18,11 @@ package cmd import ( "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/mail" + "code.vikunja.io/api/pkg/migration" + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/red" "fmt" "github.com/spf13/cobra" "os" @@ -27,7 +32,7 @@ import ( var Version = "0.1" func init() { - cobra.OnInitialize(config.InitConfig) + cobra.OnInitialize(initialize) } var rootCmd = &cobra.Command{ @@ -52,3 +57,27 @@ func Execute() { os.Exit(1) } } + +// Initializes all kinds of things in the right order +func initialize() { + // Init the config + config.InitConfig() + + // Init redis + red.InitRedis() + + // Set logger + log.InitLogger() + + // Run the migrations + migration.Migrate(nil) + + // Set Engine + err := models.SetEngine() + if err != nil { + log.Log.Fatal(err.Error()) + } + + // Start the mail daemon + mail.StartMailDaemon() +} diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 1d15a94447d..067f83204d7 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -18,9 +18,6 @@ package cmd import ( "code.vikunja.io/api/pkg/log" - "code.vikunja.io/api/pkg/mail" - "code.vikunja.io/api/pkg/migration" - "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/routes" "code.vikunja.io/api/pkg/swagger" "context" @@ -41,21 +38,6 @@ var webCmd = &cobra.Command{ Short: "Starts the rest api web server", Run: func(cmd *cobra.Command, args []string) { - // Set logger - log.InitLogger() - - // Run the migrations - migration.Migrate(nil) - - // Set Engine - err := models.SetEngine() - if err != nil { - log.Log.Fatal(err.Error()) - } - - // Start the mail daemon - mail.StartMailDaemon() - // Version notification fmt.Printf("Vikunja version %s\n", Version) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 6a2b3e9c31d..f304c242857 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -19,12 +19,13 @@ package metrics import ( "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/red" + "github.com/go-redis/redis" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/spf13/viper" ) -var r = red.GetRedis() +var r *redis.Client const ( // ListCountKey is the name of the key in which we save the list count @@ -43,7 +44,10 @@ const ( TeamCountKey = `teamcount` ) -func init() { +// InitMetrics Initializes the metrics +func InitMetrics() { + r = red.GetRedis() + // Register total list count metric promauto.NewGaugeFunc(prometheus.GaugeOpts{ Name: "vikunja_list_count", diff --git a/pkg/red/redis.go b/pkg/red/redis.go index 895fbe2388b..041138f356d 100644 --- a/pkg/red/redis.go +++ b/pkg/red/redis.go @@ -24,8 +24,8 @@ import ( var r *redis.Client -// SetRedis initializes a redis connection -func init() { +// InitRedis initializes a redis connection +func InitRedis() { if !viper.GetBool("redis.enabled") { return } @@ -44,6 +44,7 @@ func init() { if err != nil { log.Log.Fatal(err.Error()) } + log.Log.Debug("Redis initialized") } // GetRedis returns a pointer to a redis client diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index f35e4facdf5..aff87d4aac3 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -138,6 +138,8 @@ func RegisterRoutes(e *echo.Echo) { log.Log.Fatal("You have to enable redis in order to use metrics") } + metrics.InitMetrics() + type countable struct { Rediskey string Type interface{}