From 62d9a282ceb974cde621c8f18606e66ed85de646 Mon Sep 17 00:00:00 2001 From: Rico Date: Thu, 24 Mar 2022 02:17:05 +0100 Subject: [PATCH 1/8] feat: implement first iteration of prometheus support --- go.mod | 1 + go.sum | 6 +++++ internal/bot/bot.go | 6 +++++ internal/bot/notification.go | 2 ++ internal/database/database.go | 30 +++++++++++++++++++++++ internal/prometheus/prometheus.go | 40 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 internal/prometheus/prometheus.go diff --git a/go.mod b/go.mod index 01da8d3..737879d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4 github.com/PuerkitoBio/goquery v1.8.0 + github.com/VictoriaMetrics/metrics v1.18.1 github.com/davecgh/go-spew v1.1.1 // indirect github.com/glebarez/sqlite v1.3.3 github.com/jinzhu/now v1.1.4 // indirect diff --git a/go.sum b/go.sum index 4ad1db4..323102c 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4 h1:1hKv9DvEwn//VsJgcCGt9+IAq4n+P github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= +github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0= +github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -33,6 +35,10 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qq github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= +github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= +github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= +github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY= github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I= github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/internal/bot/bot.go b/internal/bot/bot.go index b35d4f9..aa3e9c4 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -6,6 +6,7 @@ import ( "GoGeizhalsBot/internal/config" "GoGeizhalsBot/internal/database" "GoGeizhalsBot/internal/geizhals" + "GoGeizhalsBot/internal/prometheus" "fmt" "log" "net/http" @@ -481,5 +482,10 @@ func Start(botConfig config.Config) { log.Printf("Bot has been started as @%s...\n", bot.User.Username) + if botConfig.Prometheus.Enabled { + exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort) + prometheus.StartPrometheusExporter(exportAddr) + } + updater.Idle() } diff --git a/internal/bot/notification.go b/internal/bot/notification.go index 6e88670..7b35fe5 100644 --- a/internal/bot/notification.go +++ b/internal/bot/notification.go @@ -4,6 +4,7 @@ import ( "GoGeizhalsBot/internal/bot/models" "GoGeizhalsBot/internal/database" "GoGeizhalsBot/internal/geizhals" + "GoGeizhalsBot/internal/prometheus" "fmt" "log" "time" @@ -84,6 +85,7 @@ func notifyUsers(priceAgent models.PriceAgent, oldEntity, updatedEntity geizhals } log.Println("Sending notification to user:", user.ID) + prometheus.PriceagentNotifications.Inc() markup := gotgbot.InlineKeyboardMarkup{ InlineKeyboard: [][]gotgbot.InlineKeyboardButton{ diff --git a/internal/database/database.go b/internal/database/database.go index bfce304..8c82f8e 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -53,11 +53,41 @@ func GetPriceAgentCountForUser(userID int64) int64 { return count } +func GetPriceAgentCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Count(&count) + return count +} + +func GetPriceAgentProductCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Joins("JOIN entities on price_agents.entity_id = entities.id").Where("entities.type = ?", geizhals.Product).Count(&count) + return count +} + +func GetPriceAgentWishlistCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Joins("JOIN entities on price_agents.entity_id = entities.id").Where("entities.type = ?", geizhals.Wishlist).Count(&count) + return count +} + +func GetUserCount() int64 { + var count int64 + db.Model(&models.User{}).Count(&count) + return count +} + func CreateUser(user models.User) error { tx := db.Create(&user) return tx.Error } +func GetAllUsers() []models.User { + var users []models.User + db.Find(&users) + return users +} + func DeletePriceAgentForUser(priceAgent models.PriceAgent) error { log.Println("Delete priceagent!") diff --git a/internal/prometheus/prometheus.go b/internal/prometheus/prometheus.go new file mode 100644 index 0000000..75ca541 --- /dev/null +++ b/internal/prometheus/prometheus.go @@ -0,0 +1,40 @@ +package prometheus + +import ( + "GoGeizhalsBot/internal/database" + "net/http" + + "github.com/VictoriaMetrics/metrics" +) + +var ( + totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 { + return float64(database.GetUserCount()) + }) + totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 { + return float64(database.GetPriceAgentCount()) + }) + totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 { + return float64(database.GetPriceAgentProductCount()) + }) + totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 { + return float64(database.GetPriceAgentWishlistCount()) + }) + totalUniqueProductPriceagentsValue int64 + + TotalUserInteractions = metrics.NewCounter("gogeizhalsbot_user_interactions_total") + GeizhalsHTTPRequests = metrics.NewCounter("gogeizhalsbot_geizhals_http_requests_total") + PriceagentNotifications = metrics.NewCounter("gogeizhalsbot_priceagent_notifications_total") + ProxyErrors = metrics.NewCounter("gogeizhalsbot_proxy_errors_total") + GraphsRendered = metrics.NewCounter("gogeizhalsbot_graphs_rendered_total") +) + +// var backgroundUpdateChecks = metrics.NewSummary("gogeizhalsbot_total_requests") + +func StartPrometheusExporter(addr string) { + // Expose the registered metrics at `/metrics` path. + http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) { + metrics.WritePrometheus(w, true) + }) + http.ListenAndServe(addr, nil) +} From cc073fcd1b7278a7801cfbc22c87a1d08a23bf94 Mon Sep 17 00:00:00 2001 From: Rico Date: Thu, 24 Mar 2022 02:17:05 +0100 Subject: [PATCH 2/8] feat: implement first iteration of prometheus support --- go.mod | 1 + go.sum | 6 +++++ internal/bot/bot.go | 6 +++++ internal/bot/notification.go | 2 ++ internal/database/database.go | 30 +++++++++++++++++++++++ internal/prometheus/prometheus.go | 40 +++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 internal/prometheus/prometheus.go diff --git a/go.mod b/go.mod index 01da8d3..737879d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4 github.com/PuerkitoBio/goquery v1.8.0 + github.com/VictoriaMetrics/metrics v1.18.1 github.com/davecgh/go-spew v1.1.1 // indirect github.com/glebarez/sqlite v1.3.3 github.com/jinzhu/now v1.1.4 // indirect diff --git a/go.sum b/go.sum index 4ad1db4..323102c 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4 h1:1hKv9DvEwn//VsJgcCGt9+IAq4n+P github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.4/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= +github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0= +github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -33,6 +35,10 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qq github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= +github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= +github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= +github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY= github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I= github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/internal/bot/bot.go b/internal/bot/bot.go index d379b19..5dafad7 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -6,6 +6,7 @@ import ( "GoGeizhalsBot/internal/config" "GoGeizhalsBot/internal/database" "GoGeizhalsBot/internal/geizhals" + "GoGeizhalsBot/internal/prometheus" "fmt" "log" "net/http" @@ -480,5 +481,10 @@ func Start(botConfig config.Config) { log.Printf("Bot has been started as @%s...\n", bot.User.Username) + if botConfig.Prometheus.Enabled { + exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort) + prometheus.StartPrometheusExporter(exportAddr) + } + updater.Idle() } diff --git a/internal/bot/notification.go b/internal/bot/notification.go index 6e88670..7b35fe5 100644 --- a/internal/bot/notification.go +++ b/internal/bot/notification.go @@ -4,6 +4,7 @@ import ( "GoGeizhalsBot/internal/bot/models" "GoGeizhalsBot/internal/database" "GoGeizhalsBot/internal/geizhals" + "GoGeizhalsBot/internal/prometheus" "fmt" "log" "time" @@ -84,6 +85,7 @@ func notifyUsers(priceAgent models.PriceAgent, oldEntity, updatedEntity geizhals } log.Println("Sending notification to user:", user.ID) + prometheus.PriceagentNotifications.Inc() markup := gotgbot.InlineKeyboardMarkup{ InlineKeyboard: [][]gotgbot.InlineKeyboardButton{ diff --git a/internal/database/database.go b/internal/database/database.go index 198cf39..f17df32 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -53,11 +53,41 @@ func GetPriceAgentCountForUser(userID int64) int64 { return count } +func GetPriceAgentCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Count(&count) + return count +} + +func GetPriceAgentProductCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Joins("JOIN entities on price_agents.entity_id = entities.id").Where("entities.type = ?", geizhals.Product).Count(&count) + return count +} + +func GetPriceAgentWishlistCount() int64 { + var count int64 + db.Model(&models.PriceAgent{}).Joins("JOIN entities on price_agents.entity_id = entities.id").Where("entities.type = ?", geizhals.Wishlist).Count(&count) + return count +} + +func GetUserCount() int64 { + var count int64 + db.Model(&models.User{}).Count(&count) + return count +} + func CreateUser(user models.User) error { tx := db.Create(&user) return tx.Error } +func GetAllUsers() []models.User { + var users []models.User + db.Find(&users) + return users +} + func DeletePriceAgentForUser(priceAgent models.PriceAgent) error { log.Println("Delete priceagent!") diff --git a/internal/prometheus/prometheus.go b/internal/prometheus/prometheus.go new file mode 100644 index 0000000..75ca541 --- /dev/null +++ b/internal/prometheus/prometheus.go @@ -0,0 +1,40 @@ +package prometheus + +import ( + "GoGeizhalsBot/internal/database" + "net/http" + + "github.com/VictoriaMetrics/metrics" +) + +var ( + totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 { + return float64(database.GetUserCount()) + }) + totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 { + return float64(database.GetPriceAgentCount()) + }) + totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 { + return float64(database.GetPriceAgentProductCount()) + }) + totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 { + return float64(database.GetPriceAgentWishlistCount()) + }) + totalUniqueProductPriceagentsValue int64 + + TotalUserInteractions = metrics.NewCounter("gogeizhalsbot_user_interactions_total") + GeizhalsHTTPRequests = metrics.NewCounter("gogeizhalsbot_geizhals_http_requests_total") + PriceagentNotifications = metrics.NewCounter("gogeizhalsbot_priceagent_notifications_total") + ProxyErrors = metrics.NewCounter("gogeizhalsbot_proxy_errors_total") + GraphsRendered = metrics.NewCounter("gogeizhalsbot_graphs_rendered_total") +) + +// var backgroundUpdateChecks = metrics.NewSummary("gogeizhalsbot_total_requests") + +func StartPrometheusExporter(addr string) { + // Expose the registered metrics at `/metrics` path. + http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) { + metrics.WritePrometheus(w, true) + }) + http.ListenAndServe(addr, nil) +} From e10927ca433f11036c590debb27f0b2281a208dd Mon Sep 17 00:00:00 2001 From: Rico Date: Sun, 27 Mar 2022 17:15:56 +0200 Subject: [PATCH 3/8] feat: add more metrics to prometheus exporter --- internal/bot/bot.go | 12 ++++++++++++ internal/bot/pricehistory.go | 2 ++ internal/geizhals/geizhals.go | 3 +++ internal/prometheus/prometheus.go | 27 ++++++++++++++++----------- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 5dafad7..2d396f7 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -11,6 +11,7 @@ import ( "log" "net/http" "net/url" + "time" "github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message" @@ -219,6 +220,7 @@ func showPriceagentDetail(b *gotgbot.Bot, ctx *ext.Context) error { }, } + // Check if the initial message contained a photo, if yes, we're coming from the price history graph if len(cb.Message.Photo) > 0 { bot.DeleteMessage(ctx.EffectiveChat.Id, cb.Message.MessageId) @@ -345,6 +347,7 @@ func deletePriceagentHandler(b *gotgbot.Bot, ctx *ext.Context) error { } func newUserHandler(_ *gotgbot.Bot, ctx *ext.Context) error { + prometheus.TotalUserInteractions.Inc() // Create user in databse if they don't exist already if !ctx.EffectiveSender.IsUser() { return nil @@ -484,6 +487,15 @@ func Start(botConfig config.Config) { if botConfig.Prometheus.Enabled { exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort) prometheus.StartPrometheusExporter(exportAddr) + + // Periodically update the metrics from the database + go func() { + prometheus.TotalUniquePriceagentsValue = database.GetPriceAgentCount() + prometheus.TotalUniqueUsersValue = database.GetUserCount() + prometheus.TotalUniqueWishlistPriceagentsValue = database.GetPriceAgentWishlistCount() + prometheus.TotalUniqueProductPriceagentsValue = database.GetPriceAgentProductCount() + time.Sleep(time.Second * 60) + }() } updater.Idle() diff --git a/internal/bot/pricehistory.go b/internal/bot/pricehistory.go index 51000fe..ceb2eb0 100644 --- a/internal/bot/pricehistory.go +++ b/internal/bot/pricehistory.go @@ -4,6 +4,7 @@ import ( "GoGeizhalsBot/internal/bot/models" "GoGeizhalsBot/internal/database" "GoGeizhalsBot/internal/geizhals" + "GoGeizhalsBot/internal/prometheus" "bytes" "fmt" "io" @@ -145,6 +146,7 @@ func getPriceagentFromContext(ctx *ext.Context) (models.PriceAgent, error) { // renderChart renders a price history chart to the given writer. func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, since time.Time, w io.Writer) { + prometheus.GraphsRendered.Inc() darkFontColor := drawing.ColorFromHex("c2c2c2") fontColor := darkFontColor diff --git a/internal/geizhals/geizhals.go b/internal/geizhals/geizhals.go index d40e991..d3d9a80 100644 --- a/internal/geizhals/geizhals.go +++ b/internal/geizhals/geizhals.go @@ -1,6 +1,7 @@ package geizhals import ( + "GoGeizhalsBot/internal/prometheus" "fmt" "log" "net/http" @@ -72,9 +73,11 @@ func downloadHTML(entityURL string) (*goquery.Document, int, error) { log.Println("Using proxy: ", proxyURL) } + prometheus.GeizhalsHTTPRequests.Inc() resp, getErr := httpClient.Get(entityURL) if getErr != nil { log.Println(getErr) + prometheus.HttpErrors.Inc() return nil, 0, fmt.Errorf("error while downloading content from Geizhals: %w", getErr) } // Cleanup when this function ends diff --git a/internal/prometheus/prometheus.go b/internal/prometheus/prometheus.go index 75ca541..82d4692 100644 --- a/internal/prometheus/prometheus.go +++ b/internal/prometheus/prometheus.go @@ -1,31 +1,36 @@ package prometheus import ( - "GoGeizhalsBot/internal/database" "net/http" "github.com/VictoriaMetrics/metrics" ) var ( - totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 { - return float64(database.GetUserCount()) + TotalUniqueUsersValue int64 + totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 { + return float64(TotalUniqueUsersValue) }) - totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 { - return float64(database.GetPriceAgentCount()) + + TotalUniquePriceagentsValue int64 + totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 { + return float64(TotalUniquePriceagentsValue) }) - totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 { - return float64(database.GetPriceAgentProductCount()) + + TotalUniqueProductPriceagentsValue int64 + totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 { + return float64(TotalUniqueProductPriceagentsValue) }) - totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 { - return float64(database.GetPriceAgentWishlistCount()) + + TotalUniqueWishlistPriceagentsValue int64 + totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 { + return float64(TotalUniqueWishlistPriceagentsValue) }) - totalUniqueProductPriceagentsValue int64 TotalUserInteractions = metrics.NewCounter("gogeizhalsbot_user_interactions_total") GeizhalsHTTPRequests = metrics.NewCounter("gogeizhalsbot_geizhals_http_requests_total") PriceagentNotifications = metrics.NewCounter("gogeizhalsbot_priceagent_notifications_total") - ProxyErrors = metrics.NewCounter("gogeizhalsbot_proxy_errors_total") + HttpErrors = metrics.NewCounter("gogeizhalsbot_http_errors_total") GraphsRendered = metrics.NewCounter("gogeizhalsbot_graphs_rendered_total") ) From 82f94cb6f170c4f96373667ed2fc1a8f0b680b58 Mon Sep 17 00:00:00 2001 From: Rico Date: Mon, 28 Mar 2022 00:52:29 +0200 Subject: [PATCH 4/8] build: bump version to 1.1.0 --- internal/bot/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/bot/version.go b/internal/bot/version.go index 7abe4ed..5ce40a8 100644 --- a/internal/bot/version.go +++ b/internal/bot/version.go @@ -1,3 +1,3 @@ package bot -var version = "1.0.0-dev (compiled manually)" +var version = "1.1.0-dev (compiled manually)" From dd42076c16c6b7bc7258b7dd33920fbf666760ee Mon Sep 17 00:00:00 2001 From: Rico Date: Mon, 28 Mar 2022 01:12:23 +0200 Subject: [PATCH 5/8] docs: add changelog --- CHANGELOG.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3b251a6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,24 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] +### Added +### Changed +### Fixed +### Docs + +## [1.1.0] - 2022-03-28 + +### Added +- Support for prometheus database + +## [1.0.0] - 2022-02-14 +Initial release! First stable version of GoGeizhalsBot is published as v1.0.0 + +[unreleased]: https://github.com/d-Rickyy-b/GoGeizhalsBot/compare/v1.1.0...HEAD +[1.1.0]: https://github.com/d-Rickyy-b/GoGeizhalsBot/compare/v1.0.0...v1.1.0 +[1.0.0]: https://github.com/d-Rickyy-b/GoGeizhalsBot/tree/v1.0.0 From 7631e7add129a9d9efc41bb8ccd503ab0e7be80e Mon Sep 17 00:00:00 2001 From: Rico Date: Mon, 28 Mar 2022 01:43:35 +0200 Subject: [PATCH 6/8] fix: periodically update metrics from database --- internal/bot/bot.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 2d396f7..15b14e3 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -490,11 +490,13 @@ func Start(botConfig config.Config) { // Periodically update the metrics from the database go func() { - prometheus.TotalUniquePriceagentsValue = database.GetPriceAgentCount() - prometheus.TotalUniqueUsersValue = database.GetUserCount() - prometheus.TotalUniqueWishlistPriceagentsValue = database.GetPriceAgentWishlistCount() - prometheus.TotalUniqueProductPriceagentsValue = database.GetPriceAgentProductCount() - time.Sleep(time.Second * 60) + for { + prometheus.TotalUniquePriceagentsValue = database.GetPriceAgentCount() + prometheus.TotalUniqueUsersValue = database.GetUserCount() + prometheus.TotalUniqueWishlistPriceagentsValue = database.GetPriceAgentWishlistCount() + prometheus.TotalUniqueProductPriceagentsValue = database.GetPriceAgentProductCount() + time.Sleep(time.Second * 60) + } }() } From babde7fa3159206ab5be9f5d5f92886f671ca834 Mon Sep 17 00:00:00 2001 From: Rico Date: Mon, 28 Mar 2022 01:50:45 +0200 Subject: [PATCH 7/8] refactor: log prometheus export address --- internal/bot/bot.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 15b14e3..5d0c9dc 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -485,9 +485,6 @@ func Start(botConfig config.Config) { log.Printf("Bot has been started as @%s...\n", bot.User.Username) if botConfig.Prometheus.Enabled { - exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort) - prometheus.StartPrometheusExporter(exportAddr) - // Periodically update the metrics from the database go func() { for { @@ -498,6 +495,10 @@ func Start(botConfig config.Config) { time.Sleep(time.Second * 60) } }() + + exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort) + log.Printf("Starting prometheus exporter on %s...\n", exportAddr) + prometheus.StartPrometheusExporter(exportAddr) } updater.Idle() From dd20ea1bc58d8f4e2e57a9575de1542a59d1f0d6 Mon Sep 17 00:00:00 2001 From: Rico Date: Mon, 28 Mar 2022 01:51:17 +0200 Subject: [PATCH 8/8] refactor: log entity url before updating prices --- internal/bot/notification.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/bot/notification.go b/internal/bot/notification.go index 7b35fe5..187e83f 100644 --- a/internal/bot/notification.go +++ b/internal/bot/notification.go @@ -24,7 +24,7 @@ func updateEntityPrices() { // For each price agent, update prices and store updated prices in the entity in the database. // Also update price history with the new prices. for _, entity := range allEntities { - log.Println("Updating prices for:", entity.Name) + log.Println("Updating prices for:", entity.URL) // If there are two price agents with the same entity, we currently fetch it twice updatedEntity, updateErr := geizhals.UpdateEntity(entity)