2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +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-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-12 23:50:35 +01:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2019-07-06 22:12:26 +02:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
2018-12-12 23:50:35 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2020-10-10 18:53:59 +02:00
|
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
|
|
|
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
2018-12-12 23:50:35 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ListCountKey is the name of the key in which we save the list count
|
|
|
|
ListCountKey = `listcount`
|
|
|
|
|
|
|
|
// UserCountKey is the name of the key we use to store total users in redis
|
|
|
|
UserCountKey = `usercount`
|
|
|
|
|
|
|
|
// NamespaceCountKey is the name of the key we use to store the amount of total namespaces in redis
|
|
|
|
NamespaceCountKey = `namespacecount`
|
|
|
|
|
|
|
|
// TaskCountKey is the name of the key we use to store the amount of total tasks in redis
|
|
|
|
TaskCountKey = `taskcount`
|
|
|
|
|
|
|
|
// TeamCountKey is the name of the key we use to store the amount of total teams in redis
|
|
|
|
TeamCountKey = `teamcount`
|
|
|
|
)
|
|
|
|
|
2019-05-12 16:49:16 +02:00
|
|
|
// InitMetrics Initializes the metrics
|
|
|
|
func InitMetrics() {
|
2020-05-15 14:42:32 +02:00
|
|
|
// init active users, sometimes we'll have garbage from previous runs in redis instead
|
|
|
|
if err := PushActiveUsers(); err != nil {
|
|
|
|
log.Fatalf("Could not set initial count for active users, error was %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
// Register total list count metric
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_list_count",
|
|
|
|
Help: "The number of lists on this instance",
|
|
|
|
}, func() float64 {
|
|
|
|
count, _ := GetCount(ListCountKey)
|
|
|
|
return float64(count)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register total user count metric
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_user_count",
|
|
|
|
Help: "The total number of users on this instance",
|
|
|
|
}, func() float64 {
|
|
|
|
count, _ := GetCount(UserCountKey)
|
|
|
|
return float64(count)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register total Namespaces count metric
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_namespcae_count",
|
|
|
|
Help: "The total number of namespaces on this instance",
|
|
|
|
}, func() float64 {
|
|
|
|
count, _ := GetCount(NamespaceCountKey)
|
|
|
|
return float64(count)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register total Tasks count metric
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_task_count",
|
|
|
|
Help: "The total number of tasks on this instance",
|
|
|
|
}, func() float64 {
|
|
|
|
count, _ := GetCount(TaskCountKey)
|
|
|
|
return float64(count)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register total user count metric
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_team_count",
|
|
|
|
Help: "The total number of teams on this instance",
|
|
|
|
}, func() float64 {
|
|
|
|
count, _ := GetCount(TeamCountKey)
|
|
|
|
return float64(count)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCount returns the current count from redis
|
|
|
|
func GetCount(key string) (count int64, err error) {
|
2020-10-10 18:53:59 +02:00
|
|
|
cnt, err := keyvalue.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
if e.IsErrValueNotFoundForKey(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, err
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|
2020-10-10 18:53:59 +02:00
|
|
|
count = cnt.(int64)
|
2018-12-12 23:50:35 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCount sets the list count to a given value
|
|
|
|
func SetCount(count int64, key string) error {
|
2020-10-10 18:53:59 +02:00
|
|
|
return keyvalue.Put(key, count)
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCount updates a count with a given amount
|
|
|
|
func UpdateCount(update int64, key string) {
|
2019-07-06 22:12:26 +02:00
|
|
|
if !config.ServiceEnableMetrics.GetBool() {
|
2018-12-12 23:50:35 +01:00
|
|
|
return
|
|
|
|
}
|
2020-01-19 18:26:26 +01:00
|
|
|
if update > 0 {
|
2020-10-10 18:53:59 +02:00
|
|
|
err := keyvalue.IncrBy(key, update)
|
2020-01-19 18:26:26 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|
2020-01-19 18:26:26 +01:00
|
|
|
if update < 0 {
|
2020-10-10 18:53:59 +02:00
|
|
|
err := keyvalue.DecrBy(key, update)
|
2020-01-19 18:26:26 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|
|
|
|
}
|