feat(metrics): add total number of files metric

This commit is contained in:
kolaente 2023-12-03 15:22:41 +01:00
parent 0ce110fa52
commit fd0b2d103d
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 41 additions and 64 deletions

View File

@ -17,6 +17,8 @@
package files package files
import ( import (
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/modules/keyvalue"
"errors" "errors"
"io" "io"
"os" "os"
@ -148,10 +150,15 @@ func (f *File) Delete() (err error) {
return err return err
} }
return return keyvalue.DecrBy(metrics.FilesCountKey, 1)
} }
// Save saves a file to storage // Save saves a file to storage
func (f *File) Save(fcontent io.Reader) error { func (f *File) Save(fcontent io.Reader) (err error) {
return afs.WriteReader(f.getFileName(), fcontent) err = afs.WriteReader(f.getFileName(), fcontent)
if err != nil {
return
}
return keyvalue.IncrBy(metrics.FilesCountKey, 1)
} }

View File

@ -28,17 +28,11 @@ import (
) )
const ( const (
// ProjectCountKey is the name of the key in which we save the project count ProjectCountKey = `project_count`
ProjectCountKey = `projectcount` UserCountKey = `user_count`
TaskCountKey = `task_count`
// UserCountKey is the name of the key we use to store total shares in redis TeamCountKey = `team_count`
UserCountKey = `usercount` FilesCountKey = `files_count`
// 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`
) )
var registry *prometheus.Registry var registry *prometheus.Registry
@ -53,57 +47,28 @@ func GetRegistry() *prometheus.Registry {
return registry return registry
} }
func registerPromMetric(key, description string) {
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_" + key,
Help: description,
}, func() float64 {
count, _ := GetCount(key)
return float64(count)
}))
if err != nil {
log.Criticalf("Could not register metrics for %s: %s", key, err)
}
}
// InitMetrics Initializes the metrics // InitMetrics Initializes the metrics
func InitMetrics() { func InitMetrics() {
GetRegistry() GetRegistry()
// Register total project count metric registerPromMetric(ProjectCountKey, "The number of projects on this instance")
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{ registerPromMetric(UserCountKey, "The total number of shares on this instance")
Name: "vikunja_project_count", registerPromMetric(TaskCountKey, "The total number of tasks on this instance")
Help: "The number of projects on this instance", registerPromMetric(TeamCountKey, "The total number of teams on this instance")
}, func() float64 { registerPromMetric(FilesCountKey, "The total number of files on this instance")
count, _ := GetCount(ProjectCountKey)
return float64(count)
}))
if err != nil {
log.Criticalf("Could not register metrics for %s: %s", ProjectCountKey, err)
}
// Register total user count metric
err = registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_user_count",
Help: "The total number of shares on this instance",
}, func() float64 {
count, _ := GetCount(UserCountKey)
return float64(count)
}))
if err != nil {
log.Criticalf("Could not register metrics for %s: %s", UserCountKey, err)
}
// Register total Tasks count metric
err = registry.Register(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)
}))
if err != nil {
log.Criticalf("Could not register metrics for %s: %s", TaskCountKey, err)
}
// Register total teams count metric
err = registry.Register(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)
}))
if err != nil {
log.Criticalf("Could not register metrics for %s: %s", TeamCountKey, err)
}
setupActiveUsersMetric() setupActiveUsersMetric()
setupActiveLinkSharesMetric() setupActiveLinkSharesMetric()

View File

@ -17,6 +17,7 @@
package routes package routes
import ( import (
"code.vikunja.io/api/pkg/files"
"crypto/subtle" "crypto/subtle"
"code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/config"
@ -38,8 +39,8 @@ func setupMetrics(a *echo.Group) {
metrics.InitMetrics() metrics.InitMetrics()
type countable struct { type countable struct {
Rediskey string Key string
Type interface{} Type interface{}
} }
for _, c := range []countable{ for _, c := range []countable{
@ -59,13 +60,17 @@ func setupMetrics(a *echo.Group) {
metrics.TeamCountKey, metrics.TeamCountKey,
models.Team{}, models.Team{},
}, },
{
metrics.FilesCountKey,
files.File{},
},
} { } {
// Set initial totals // Set initial totals
total, err := models.GetTotalCount(c.Type) total, err := models.GetTotalCount(c.Type)
if err != nil { if err != nil {
log.Fatalf("Could not get initial count for %v, error was %s", c.Type, err) log.Fatalf("Could not get initial count for %v, error was %s", c.Type, err)
} }
if err := metrics.SetCount(total, c.Rediskey); err != nil { if err := metrics.SetCount(total, c.Key); err != nil {
log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err) log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
} }
} }