feat: use prometheus

Signed-off-by: martin <martin.labat92@gmail.com>
This commit is contained in:
martin 2023-02-13 11:33:45 +01:00
parent 5c230986d0
commit 3749b1eef9
No known key found for this signature in database
GPG key ID: 4544CC55835A3B9E
4 changed files with 28 additions and 17 deletions

View file

@ -14,6 +14,7 @@ import (
func Allrequests(r *prometheus.Registry) { func Allrequests(r *prometheus.Registry) {
serverversion(r) serverversion(r)
Analyze(r) Analyze(r)
} }
@ -21,7 +22,6 @@ func Analyze(r *prometheus.Registry) {
allusers, err := GetAllUsers() allusers, err := GetAllUsers()
users, err2 := users() users, err2 := users()
if err != nil && err2 != nil { if err != nil && err2 != nil {
} else { } else {
Sendbackmessagepreference(users, allusers, r) Sendbackmessagepreference(users, allusers, r)
} }
@ -53,7 +53,7 @@ func GetAllUsers() (*models.AllUsers, error) {
log.Println("Can not unmarshal JSON") log.Println("Can not unmarshal JSON")
} }
return &result, err return &result, nil
} }
} }

View file

@ -1,7 +1,6 @@
package immich package immich
import ( import (
"fmt"
"immich-exporter/src/models" "immich-exporter/src/models"
"strconv" "strconv"
@ -26,32 +25,49 @@ func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers, r
Name: "immich_app_number_users", Name: "immich_app_number_users",
Help: "The total number of users", Help: "The total number of users",
}) })
user_info := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "immich_user_info",
Help: "All infos about users",
}, []string{"videos", "photos", "uid", "usage", "firstname", "lastname"})
user_usage := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "immich_user_usage",
Help: "The usage of the user",
}, []string{"uid", "firstname", "lastname"})
user_photos := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "immich_user_photos",
Help: "The number of photo of the user",
}, []string{"uid", "firstname", "lastname"})
user_videos := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "immich_user_videos",
Help: "The number of videos of the user",
}, []string{"uid", "firstname", "lastname"})
r.MustRegister(user_info)
r.MustRegister(total_usage) r.MustRegister(total_usage)
r.MustRegister(total_videos) r.MustRegister(total_videos)
r.MustRegister(total_photos) r.MustRegister(total_photos)
r.MustRegister(total_users) r.MustRegister(total_users)
r.MustRegister(user_usage)
r.MustRegister(user_videos)
r.MustRegister(user_photos)
total_photos.Add(float64((*result).Photos)) total_photos.Add(float64((*result).Photos))
total_videos.Add(float64((*result).Videos)) total_videos.Add(float64((*result).Videos))
total_usage.Add(float64((*result).UsageRaw)) total_usage.Add(float64((*result).UsageRaw))
total_users.Add(float64(len((*result).UsageByUser))) total_users.Add(float64(len((*result).UsageByUser)))
immich_user_videos := "# HELP immich_app_user_videos The number of videos of the user\n# TYPE immich_app_user_videos gauge\n"
immich_user_photos := "# HELP immich_app_user_photos The number of photo of the user\n# TYPE immich_app_user_photos gauge\n"
immich_user_usageRaw := "# HELP immich_app_user_usage The usage of the user\n# TYPE immich_app_user_usage gauge\n"
immich_user_info := "# HELP immich_user_info All info for torrents\n# TYPE immich_user_info gauge\n"
for i := 0; i < len((*result).UsageByUser); i++ { for i := 0; i < len((*result).UsageByUser); i++ {
var myuser = GetName((*result).UsageByUser[i].UserID, result2) var myuser = GetName((*result).UsageByUser[i].UserID, result2)
immich_user_info = immich_user_info + `immich_user_info{videos="` + strconv.Itoa((*result).UsageByUser[i].Videos) + `",photos="` + strconv.Itoa((*result).UsageByUser[i].Photos) + `",uid="` + (*result).UsageByUser[i].UserID + `",usage="` + strconv.Itoa(int((*result).UsageByUser[i].UsageRaw)) + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `"} 1.0` + "\n" user_info.With(prometheus.Labels{"videos": strconv.Itoa((*result).UsageByUser[i].Videos), "photos": strconv.Itoa((*result).UsageByUser[i].Photos), "uid": (*result).UsageByUser[i].UserID, "usage": strconv.Itoa(int((*result).UsageByUser[i].UsageRaw)), "firstname": myuser.FirstName, "lastname": myuser.LastName}).Inc()
immich_user_usageRaw = immich_user_usageRaw + `immich_user_usage{uid="` + (*result).UsageByUser[i].UserID + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `",} ` + strconv.Itoa(int((*result).UsageByUser[i].UsageRaw)) + "\n" user_photos.With(prometheus.Labels{"uid": (*result).UsageByUser[i].UserID, "firstname": myuser.FirstName, "lastname": myuser.LastName}).Set(float64((*result).UsageByUser[i].Photos))
immich_user_photos = immich_user_photos + `immich_user_photos{uid="` + (*result).UsageByUser[i].UserID + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `",} ` + strconv.Itoa((*result).UsageByUser[i].Photos) + "\n" user_usage.With(prometheus.Labels{"uid": (*result).UsageByUser[i].UserID, "firstname": myuser.FirstName, "lastname": myuser.LastName}).Set(float64((*result).UsageByUser[i].UsageRaw))
immich_user_videos = immich_user_videos + `immich_user_videos{uid="` + (*result).UsageByUser[i].UserID + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `",} ` + strconv.Itoa((*result).UsageByUser[i].Videos) + "\n" user_videos.With(prometheus.Labels{"uid": (*result).UsageByUser[i].UserID, "firstname": myuser.FirstName, "lastname": myuser.LastName}).Set(float64((*result).UsageByUser[i].Videos))
} }
} }
func Sendbackmessageserverversion(result *models.ServerVersion, r *prometheus.Registry) { func Sendbackmessageserverversion(result *models.ServerVersion, r *prometheus.Registry) {
fmt.Println("test")
version := prometheus.NewGauge(prometheus.GaugeOpts{ version := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "version", Name: "version",

View file

@ -17,9 +17,6 @@ func main() {
log.Println("username :", models.GetUsername()) log.Println("username :", models.GetUsername())
log.Println("password :", models.Getpasswordmasked()) log.Println("password :", models.Getpasswordmasked())
log.Println("Started") log.Println("Started")
r := prometheus.NewRegistry()
immich.Allrequests(r)
http.HandleFunc("/metrics", test) http.HandleFunc("/metrics", test)
http.ListenAndServe(":8090", nil) http.ListenAndServe(":8090", nil)

View file

@ -2,8 +2,6 @@ package models
import "github.com/prometheus/client_golang/prometheus" import "github.com/prometheus/client_golang/prometheus"
var r = prometheus.NewRegistry()
var ( var (
appVersion string appVersion string
version = prometheus.NewGauge(prometheus.GaugeOpts{ version = prometheus.NewGauge(prometheus.GaugeOpts{