vikunja/pkg/routes/metrics.go

129 lines
3.3 KiB
Go
Raw Normal View History

2020-12-29 01:04:20 +00:00
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
2019-07-21 21:44:12 +00:00
//
2020-12-29 01:04:20 +00:00
// This program is free software: you can redistribute it and/or modify
2020-12-23 15:41:52 +00:00
// it under the terms of the GNU Affero General Public Licensee as published by
2019-07-21 21:44:12 +00:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-29 01:04:20 +00:00
// This program is distributed in the hope that it will be useful,
2019-07-21 21:44:12 +00:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020-12-23 15:41:52 +00:00
// GNU Affero General Public Licensee for more details.
2019-07-21 21:44:12 +00:00
//
2020-12-23 15:41:52 +00:00
// You should have received a copy of the GNU Affero General Public Licensee
2020-12-29 01:04:20 +00:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-07-21 21:44:12 +00:00
package routes
import (
2021-02-28 10:29:53 +00:00
"crypto/subtle"
2019-07-21 21:44:12 +00:00
"code.vikunja.io/api/pkg/config"
2023-12-06 13:01:09 +00:00
"code.vikunja.io/api/pkg/files"
2019-07-21 21:44:12 +00:00
"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"
2023-12-06 13:01:09 +00:00
2019-07-21 21:44:12 +00:00
"github.com/labstack/echo/v4"
2021-02-28 10:29:53 +00:00
"github.com/labstack/echo/v4/middleware"
2019-07-21 21:44:12 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func setupMetrics(a *echo.Group) {
2021-02-28 10:29:53 +00:00
if !config.MetricsEnabled.GetBool() {
2019-09-01 16:39:03 +00:00
return
}
2019-07-21 21:44:12 +00:00
2019-09-01 16:39:03 +00:00
metrics.InitMetrics()
2019-07-21 21:44:12 +00:00
2019-09-01 16:39:03 +00:00
type countable struct {
Key string
Type interface{}
2019-09-01 16:39:03 +00:00
}
2019-07-21 21:44:12 +00:00
2019-09-01 16:39:03 +00:00
for _, c := range []countable{
{
2022-11-13 16:07:01 +00:00
metrics.ProjectCountKey,
models.Project{},
2019-09-01 16:39:03 +00:00
},
{
metrics.UserCountKey,
user.User{},
2019-09-01 16:39:03 +00:00
},
{
metrics.TaskCountKey,
models.Task{},
},
{
metrics.TeamCountKey,
models.Team{},
},
{
metrics.FilesCountKey,
files.File{},
},
{
metrics.AttachmentsCountKey,
models.TaskAttachment{},
},
2019-09-01 16:39:03 +00:00
} {
// 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 21:44:12 +00:00
}
if err := metrics.SetCount(total, c.Key); err != nil {
2019-09-01 16:39:03 +00:00
log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
2019-07-21 21:44:12 +00:00
}
2019-09-01 16:39:03 +00:00
}
2019-07-21 21:44:12 +00:00
2021-02-28 10:29:53 +00:00
r := a.Group("/metrics")
if config.MetricsUsername.GetString() != "" && config.MetricsPassword.GetString() != "" {
r.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if subtle.ConstantTimeCompare([]byte(username), []byte(config.MetricsUsername.GetString())) == 1 &&
subtle.ConstantTimeCompare([]byte(password), []byte(config.MetricsPassword.GetString())) == 1 {
return true, nil
}
return false, nil
}))
}
r.GET("", echo.WrapHandler(promhttp.HandlerFor(metrics.GetRegistry(), promhttp.HandlerOpts{})))
2019-07-21 21:44:12 +00:00
}
func setupMetricsMiddleware(a *echo.Group) {
2021-02-28 10:29:53 +00:00
if !config.MetricsEnabled.GetBool() {
2019-09-01 16:39:03 +00:00
return
}
2019-07-21 21:44:12 +00:00
2019-09-01 16:39:03 +00: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 16:39:03 +00:00
log.Error(err)
2019-07-21 21:44:12 +00:00
return next(c)
}
2019-09-01 16:39:03 +00:00
return next(c)
}
})
2019-07-21 21:44:12 +00: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
}
if _, is := auth.(*models.LinkSharing); is {
return metrics.SetLinkShareActive(auth)
}
2020-05-15 12:42:32 +00:00
return metrics.SetUserActive(auth)
}