vikunja-api/pkg/routes/metrics.go

104 lines
2.6 KiB
Go
Raw Normal View History

2020-12-29 02:04:20 +01:00
// Vikunja is a to-do list application to facilitate your life.
2021-02-02 20:19:13 +01:00
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
2019-07-21 23:44:12 +02:00
//
2020-12-29 02:04:20 +01:00
// This program is free software: you can redistribute it and/or modify
2020-12-23 16:41:52 +01:00
// it under the terms of the GNU Affero General Public Licensee as published by
2019-07-21 23:44:12 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-29 02:04:20 +01:00
// This program is distributed in the hope that it will be useful,
2019-07-21 23:44:12 +02:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020-12-23 16:41:52 +01:00
// GNU Affero General Public Licensee for more details.
2019-07-21 23:44:12 +02:00
//
2020-12-23 16:41:52 +01:00
// You should have received a copy of the GNU Affero General Public Licensee
2020-12-29 02:04:20 +01:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-07-21 23:44:12 +02:00
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"
auth2 "code.vikunja.io/api/pkg/modules/auth"
"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"
)
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
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
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 := auth2.GetAuthFromClaims(c)
if err != nil {
return
}
2020-05-15 14:42:32 +02:00
return metrics.SetUserActive(auth)
}