vikunja-api/pkg/routes/metrics.go

132 lines
3.2 KiB
Go
Raw Permalink Normal View History

2020-01-09 18:33:22 +01:00
// Copyright 2018-2020 Vikunja and contriubtors. All rights reserved.
2019-07-21 23:44:12 +02:00
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package routes
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/models"
v1 "code.vikunja.io/api/pkg/routes/api/v1"
"code.vikunja.io/api/pkg/user"
2019-07-21 23:44:12 +02:00
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"time"
2019-07-21 23:44:12 +02:00
)
func setupMetrics(a *echo.Group) {
2019-09-01 18:39:03 +02:00
if !config.ServiceEnableMetrics.GetBool() {
return
}
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
if !config.RedisEnabled.GetBool() {
log.Fatal("You have to enable redis in order to use metrics")
}
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
metrics.InitMetrics()
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
type countable struct {
Rediskey string
Type interface{}
}
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
for _, c := range []countable{
{
metrics.ListCountKey,
models.List{},
},
{
metrics.UserCountKey,
user.User{},
2019-09-01 18:39:03 +02:00
},
{
metrics.NamespaceCountKey,
models.Namespace{},
},
{
metrics.TaskCountKey,
models.Task{},
},
{
metrics.TeamCountKey,
models.Team{},
},
} {
// Set initial totals
total, err := models.GetTotalCount(c.Type)
if err != nil {
log.Fatalf("Could not get initial count for %v, error was %s", c.Type, err)
2019-07-21 23:44:12 +02:00
}
2019-09-01 18:39:03 +02:00
if err := metrics.SetCount(total, c.Rediskey); err != nil {
log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
2019-07-21 23:44:12 +02:00
}
2019-09-01 18:39:03 +02:00
}
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
// init active users, sometimes we'll have garbage from previous runs in redis instead
if err := metrics.SetActiveUsers([]*metrics.ActiveUser{}); err != nil {
log.Fatalf("Could not set initial count for active users, error was %s", err)
2019-07-21 23:44:12 +02:00
}
2019-09-01 18:39:03 +02:00
a.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
2019-07-21 23:44:12 +02:00
}
func setupMetricsMiddleware(a *echo.Group) {
2019-09-01 18:39:03 +02:00
if !config.ServiceEnableMetrics.GetBool() {
return
}
2019-07-21 23:44:12 +02:00
2019-09-01 18:39:03 +02:00
a.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Update currently active users
if err := updateActiveUsersFromContext(c); err != nil {
2019-09-01 18:39:03 +02:00
log.Error(err)
2019-07-21 23:44:12 +02:00
return next(c)
}
2019-09-01 18:39:03 +02:00
return next(c)
}
})
2019-07-21 23:44:12 +02:00
}
// updateActiveUsersFromContext updates the currently active users in redis
func updateActiveUsersFromContext(c echo.Context) (err error) {
auth, err := v1.GetAuthFromClaims(c)
if err != nil {
return
}
allActiveUsers, err := metrics.GetActiveUsers()
if err != nil {
return
}
var uupdated bool
for in, u := range allActiveUsers {
if u.UserID == auth.GetID() {
allActiveUsers[in].LastSeen = time.Now()
uupdated = true
}
}
if !uupdated {
allActiveUsers = append(allActiveUsers, &metrics.ActiveUser{UserID: auth.GetID(), LastSeen: time.Now()})
}
return metrics.SetActiveUsers(allActiveUsers)
}