fix(metrics): make currently active users actually work

This commit is contained in:
kolaente 2022-11-09 21:12:17 +01:00
parent a9e6776abf
commit 811514855b
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 13 additions and 7 deletions

View File

@ -28,7 +28,7 @@ import (
) )
// SecondsUntilInactive defines the seconds until a user is considered inactive // SecondsUntilInactive defines the seconds until a user is considered inactive
const SecondsUntilInactive = 60 const SecondsUntilInactive = 30
// ActiveUsersKey is the key used to store active users in redis // ActiveUsersKey is the key used to store active users in redis
const ActiveUsersKey = `activeusers` const ActiveUsersKey = `activeusers`
@ -55,12 +55,13 @@ func init() {
users: make(map[int64]*ActiveUser), users: make(map[int64]*ActiveUser),
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
} }
}
promauto.NewGaugeFunc(prometheus.GaugeOpts{ func setupActiveUsersMetric() {
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_active_users", Name: "vikunja_active_users",
Help: "The currently active users on this node", Help: "The number of users active within the last 30 seconds on this node",
}, func() float64 { }, func() float64 {
allActiveUsers, err := getActiveUsers() allActiveUsers, err := getActiveUsers()
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
@ -75,7 +76,10 @@ func init() {
} }
} }
return float64(activeUsersCount) return float64(activeUsersCount)
}) }))
if err != nil {
log.Criticalf("Could not register metrics for currently active users: %s", err)
}
} }
// SetUserActive sets a user as active and pushes it to redis // SetUserActive sets a user as active and pushes it to redis

View File

@ -113,7 +113,7 @@ func InitMetrics() {
log.Criticalf("Could not register metrics for %s: %s", TaskCountKey, err) log.Criticalf("Could not register metrics for %s: %s", TaskCountKey, err)
} }
// Register total user count metric // Register total teams count metric
err = registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{ err = registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_team_count", Name: "vikunja_team_count",
Help: "The total number of teams on this instance", Help: "The total number of teams on this instance",
@ -124,9 +124,11 @@ func InitMetrics() {
if err != nil { if err != nil {
log.Criticalf("Could not register metrics for %s: %s", TeamCountKey, err) log.Criticalf("Could not register metrics for %s: %s", TeamCountKey, err)
} }
setupActiveUsersMetric()
} }
// GetCount returns the current count from redis // GetCount returns the current count from keyvalue
func GetCount(key string) (count int64, err error) { func GetCount(key string) (count int64, err error) {
cnt, exists, err := keyvalue.Get(key) cnt, exists, err := keyvalue.Get(key)
if err != nil { if err != nil {