2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
|
|
|
// Copyright 2018-2020 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"
|
2020-11-21 17:38:58 +01:00
|
|
|
auth2 "code.vikunja.io/api/pkg/modules/auth"
|
2020-01-26 18:08:06 +01:00
|
|
|
"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,
|
2020-01-26 18:08:06 +01:00
|
|
|
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
|
2019-10-19 22:34:33 +02:00
|
|
|
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
|
|
|
}
|
2019-10-19 22:34:33 +02:00
|
|
|
|
|
|
|
// updateActiveUsersFromContext updates the currently active users in redis
|
|
|
|
func updateActiveUsersFromContext(c echo.Context) (err error) {
|
2020-11-21 17:38:58 +01:00
|
|
|
auth, err := auth2.GetAuthFromClaims(c)
|
2019-10-19 22:34:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:42:32 +02:00
|
|
|
return metrics.SetUserActive(auth)
|
2019-10-19 22:34:33 +02:00
|
|
|
}
|