Fixed metrics endpoint not working

This commit is contained in:
konrad 2019-05-12 16:49:16 +02:00
parent 3d7fd9ca20
commit de24fcc2f8
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 41 additions and 23 deletions

View file

@ -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()
}

View file

@ -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)

View file

@ -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",

View file

@ -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

View file

@ -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{}