From fd0b2d103dc147ab7ffb03942e096e63f31789f0 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 3 Dec 2023 15:22:41 +0100 Subject: [PATCH] feat(metrics): add total number of files metric --- pkg/files/files.go | 13 +++++-- pkg/metrics/metrics.go | 81 ++++++++++++------------------------------ pkg/routes/metrics.go | 11 ++++-- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/pkg/files/files.go b/pkg/files/files.go index 6910c72ae5a..f30f91b5ce0 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -17,6 +17,8 @@ package files import ( + "code.vikunja.io/api/pkg/metrics" + "code.vikunja.io/api/pkg/modules/keyvalue" "errors" "io" "os" @@ -148,10 +150,15 @@ func (f *File) Delete() (err error) { return err } - return + return keyvalue.DecrBy(metrics.FilesCountKey, 1) } // Save saves a file to storage -func (f *File) Save(fcontent io.Reader) error { - return afs.WriteReader(f.getFileName(), fcontent) +func (f *File) Save(fcontent io.Reader) (err error) { + err = afs.WriteReader(f.getFileName(), fcontent) + if err != nil { + return + } + + return keyvalue.IncrBy(metrics.FilesCountKey, 1) } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index fe02d28f708..fb838273fac 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -28,17 +28,11 @@ import ( ) const ( - // ProjectCountKey is the name of the key in which we save the project count - ProjectCountKey = `projectcount` - - // UserCountKey is the name of the key we use to store total shares in redis - UserCountKey = `usercount` - - // 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` + ProjectCountKey = `project_count` + UserCountKey = `user_count` + TaskCountKey = `task_count` + TeamCountKey = `team_count` + FilesCountKey = `files_count` ) var registry *prometheus.Registry @@ -53,57 +47,28 @@ func GetRegistry() *prometheus.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 func InitMetrics() { GetRegistry() - // Register total project count metric - err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "vikunja_project_count", - Help: "The number of projects on this instance", - }, func() float64 { - 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) - } + registerPromMetric(ProjectCountKey, "The number of projects on this instance") + registerPromMetric(UserCountKey, "The total number of shares on this instance") + registerPromMetric(TaskCountKey, "The total number of tasks on this instance") + registerPromMetric(TeamCountKey, "The total number of teams on this instance") + registerPromMetric(FilesCountKey, "The total number of files on this instance") setupActiveUsersMetric() setupActiveLinkSharesMetric() diff --git a/pkg/routes/metrics.go b/pkg/routes/metrics.go index 9f60a6e1fa2..accbc39e103 100644 --- a/pkg/routes/metrics.go +++ b/pkg/routes/metrics.go @@ -17,6 +17,7 @@ package routes import ( + "code.vikunja.io/api/pkg/files" "crypto/subtle" "code.vikunja.io/api/pkg/config" @@ -38,8 +39,8 @@ func setupMetrics(a *echo.Group) { metrics.InitMetrics() type countable struct { - Rediskey string - Type interface{} + Key string + Type interface{} } for _, c := range []countable{ @@ -59,13 +60,17 @@ func setupMetrics(a *echo.Group) { metrics.TeamCountKey, models.Team{}, }, + { + metrics.FilesCountKey, + files.File{}, + }, } { // 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) } - 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) } }