Skip to content

Commit e10927c

Browse files
committed
feat: add more metrics to prometheus exporter
1 parent 85f33c8 commit e10927c

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

internal/bot/bot.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"net/http"
1313
"net/url"
14+
"time"
1415

1516
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message"
1617

@@ -219,6 +220,7 @@ func showPriceagentDetail(b *gotgbot.Bot, ctx *ext.Context) error {
219220
},
220221
}
221222

223+
// Check if the initial message contained a photo, if yes, we're coming from the price history graph
222224
if len(cb.Message.Photo) > 0 {
223225
bot.DeleteMessage(ctx.EffectiveChat.Id, cb.Message.MessageId)
224226

@@ -345,6 +347,7 @@ func deletePriceagentHandler(b *gotgbot.Bot, ctx *ext.Context) error {
345347
}
346348

347349
func newUserHandler(_ *gotgbot.Bot, ctx *ext.Context) error {
350+
prometheus.TotalUserInteractions.Inc()
348351
// Create user in databse if they don't exist already
349352
if !ctx.EffectiveSender.IsUser() {
350353
return nil
@@ -484,6 +487,15 @@ func Start(botConfig config.Config) {
484487
if botConfig.Prometheus.Enabled {
485488
exportAddr := fmt.Sprintf("%s:%d", botConfig.Prometheus.ExportIP, botConfig.Prometheus.ExportPort)
486489
prometheus.StartPrometheusExporter(exportAddr)
490+
491+
// Periodically update the metrics from the database
492+
go func() {
493+
prometheus.TotalUniquePriceagentsValue = database.GetPriceAgentCount()
494+
prometheus.TotalUniqueUsersValue = database.GetUserCount()
495+
prometheus.TotalUniqueWishlistPriceagentsValue = database.GetPriceAgentWishlistCount()
496+
prometheus.TotalUniqueProductPriceagentsValue = database.GetPriceAgentProductCount()
497+
time.Sleep(time.Second * 60)
498+
}()
487499
}
488500

489501
updater.Idle()

internal/bot/pricehistory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"GoGeizhalsBot/internal/bot/models"
55
"GoGeizhalsBot/internal/database"
66
"GoGeizhalsBot/internal/geizhals"
7+
"GoGeizhalsBot/internal/prometheus"
78
"bytes"
89
"fmt"
910
"io"
@@ -145,6 +146,7 @@ func getPriceagentFromContext(ctx *ext.Context) (models.PriceAgent, error) {
145146

146147
// renderChart renders a price history chart to the given writer.
147148
func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, since time.Time, w io.Writer) {
149+
prometheus.GraphsRendered.Inc()
148150
darkFontColor := drawing.ColorFromHex("c2c2c2")
149151
fontColor := darkFontColor
150152

internal/geizhals/geizhals.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package geizhals
22

33
import (
4+
"GoGeizhalsBot/internal/prometheus"
45
"fmt"
56
"log"
67
"net/http"
@@ -72,9 +73,11 @@ func downloadHTML(entityURL string) (*goquery.Document, int, error) {
7273
log.Println("Using proxy: ", proxyURL)
7374
}
7475

76+
prometheus.GeizhalsHTTPRequests.Inc()
7577
resp, getErr := httpClient.Get(entityURL)
7678
if getErr != nil {
7779
log.Println(getErr)
80+
prometheus.HttpErrors.Inc()
7881
return nil, 0, fmt.Errorf("error while downloading content from Geizhals: %w", getErr)
7982
}
8083
// Cleanup when this function ends

internal/prometheus/prometheus.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
package prometheus
22

33
import (
4-
"GoGeizhalsBot/internal/database"
54
"net/http"
65

76
"github.com/VictoriaMetrics/metrics"
87
)
98

109
var (
11-
totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 {
12-
return float64(database.GetUserCount())
10+
TotalUniqueUsersValue int64
11+
totalUniqueUsers = metrics.NewGauge("gogeizhalsbot_unique_users_total", func() float64 {
12+
return float64(TotalUniqueUsersValue)
1313
})
14-
totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 {
15-
return float64(database.GetPriceAgentCount())
14+
15+
TotalUniquePriceagentsValue int64
16+
totalUniquePriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents", func() float64 {
17+
return float64(TotalUniquePriceagentsValue)
1618
})
17-
totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 {
18-
return float64(database.GetPriceAgentProductCount())
19+
20+
TotalUniqueProductPriceagentsValue int64
21+
totalUniqueProductPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"product\"}", func() float64 {
22+
return float64(TotalUniqueProductPriceagentsValue)
1923
})
20-
totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 {
21-
return float64(database.GetPriceAgentWishlistCount())
24+
25+
TotalUniqueWishlistPriceagentsValue int64
26+
totalUniqueWishlistPriceagents = metrics.NewGauge("gogeizhalsbot_unique_priceagents{type=\"wishlist\"}", func() float64 {
27+
return float64(TotalUniqueWishlistPriceagentsValue)
2228
})
23-
totalUniqueProductPriceagentsValue int64
2429

2530
TotalUserInteractions = metrics.NewCounter("gogeizhalsbot_user_interactions_total")
2631
GeizhalsHTTPRequests = metrics.NewCounter("gogeizhalsbot_geizhals_http_requests_total")
2732
PriceagentNotifications = metrics.NewCounter("gogeizhalsbot_priceagent_notifications_total")
28-
ProxyErrors = metrics.NewCounter("gogeizhalsbot_proxy_errors_total")
33+
HttpErrors = metrics.NewCounter("gogeizhalsbot_http_errors_total")
2934
GraphsRendered = metrics.NewCounter("gogeizhalsbot_graphs_rendered_total")
3035
)
3136

0 commit comments

Comments
 (0)