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
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)
}

View File

@ -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()

View File

@ -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)
}
}