Skip to content

Commit 5282d46

Browse files
committed
feat: better telemetry
1 parent 61f4497 commit 5282d46

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

internal/adapters/notifier/telegram.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"net/http"
1010
"strconv"
11+
"time"
1112

1213
"github.com/LXSCA7/gorimpo/internal/core/domain"
1314
"github.com/LXSCA7/gorimpo/internal/core/ports"
@@ -115,12 +116,29 @@ func (t *TelegramAdapter) doRequest(payload map[string]any) error {
115116
}
116117
defer resp.Body.Close()
117118

118-
if resp.StatusCode != http.StatusOK {
119-
bodyBytes, _ := io.ReadAll(resp.Body)
119+
if resp.StatusCode == http.StatusOK {
120+
return nil
121+
}
122+
123+
bodyBytes, _ := io.ReadAll(resp.Body)
124+
125+
if resp.StatusCode == http.StatusTooManyRequests {
126+
var telegramErr struct {
127+
Parameters struct {
128+
RetryAfter int `json:"retry_after"`
129+
} `json:"parameters"`
130+
}
131+
132+
if err := json.Unmarshal(bodyBytes, &telegramErr); err == nil && telegramErr.Parameters.RetryAfter > 0 {
133+
sleepTime := telegramErr.Parameters.RetryAfter
134+
135+
slog.Warn("🚨 Telegram mandou a gente segurar (Erro 429).", "sleeping_seconds", sleepTime)
136+
time.Sleep(time.Duration(sleepTime) * time.Second)
120137

121-
slog.Error("erro na api do telegram", "status", resp.StatusCode, "motivo", string(bodyBytes))
122-
return fmt.Errorf("erro na api do telegram: status %d - %s", resp.StatusCode, string(bodyBytes))
138+
return t.doRequest(payload)
139+
}
123140
}
124141

125-
return nil
142+
slog.Error("Erro na API do Telegram", "status", resp.StatusCode, "motivo", string(bodyBytes))
143+
return fmt.Errorf("erro na api do telegram: status %d - %s", resp.StatusCode, string(bodyBytes))
126144
}

internal/adapters/telemetry/prometheus.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
var _ ports.Metrics = (*PrometheusMetrics)(nil)
1111

1212
type PrometheusMetrics struct {
13-
discarded *prometheus.CounterVec
14-
valid *prometheus.CounterVec
13+
discarded *prometheus.CounterVec
14+
valid *prometheus.CounterVec
15+
scrapedTotal *prometheus.CounterVec
16+
sentTotal *prometheus.CounterVec
1517
}
1618

1719
func NewPrometheusMetrics() *PrometheusMetrics {
@@ -25,6 +27,20 @@ func NewPrometheusMetrics() *PrometheusMetrics {
2527
Name: "gorimpo_valid_total",
2628
Help: "Total de ofertas validas encontradas por termo",
2729
}, []string{"term"}),
30+
31+
scrapedTotal: promauto.NewCounterVec(
32+
prometheus.CounterOpts{
33+
Name: "gorimpo_scraped_total",
34+
Help: "Total bruto de itens raspados da plataforma",
35+
},
36+
[]string{"term"}),
37+
38+
sentTotal: promauto.NewCounterVec(
39+
prometheus.CounterOpts{
40+
Name: "gorimpo_sent_total",
41+
Help: "Total de ofertas enviadas com sucesso pro Telegram",
42+
},
43+
[]string{"term"}),
2844
}
2945
}
3046

@@ -35,3 +51,11 @@ func (p *PrometheusMetrics) RecordDiscarded(term, reason string, count int) {
3551
func (p *PrometheusMetrics) RecordValid(term string, count int) {
3652
p.valid.WithLabelValues(term).Add(float64(count))
3753
}
54+
55+
func (p *PrometheusMetrics) RecordScraped(term string, count int) {
56+
p.scrapedTotal.WithLabelValues(term).Add(float64(count))
57+
}
58+
59+
func (p *PrometheusMetrics) RecordSent(term string, count int) {
60+
p.sentTotal.WithLabelValues(term).Add(float64(count))
61+
}

internal/core/ports/telemetry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ package ports
33
type Metrics interface {
44
RecordDiscarded(term string, reason string, count int)
55
RecordValid(term string, count int)
6+
RecordScraped(term string, count int)
7+
RecordSent(term string, count int)
68
}

internal/core/services/gorimpo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (g *GorimpoService) processSearch(search config.Search) {
9797
slog.Error("Error scraping", "term", search.Term, "error", err)
9898
return
9999
}
100+
g.metrics.RecordScraped(search.Term, len(rawOffers))
100101

101102
var validOffers []domain.Offer
102103
discardedByPrice := 0
@@ -159,6 +160,7 @@ func (g *GorimpoService) processSearch(search config.Search) {
159160
g.metrics.RecordDiscarded(search.Term, "price", discardedByPrice)
160161
g.metrics.RecordDiscarded(search.Term, "filter", discardedByFilter)
161162
g.metrics.RecordValid(search.Term, len(validOffers))
163+
g.metrics.RecordSent(search.Term, newOffersCount)
162164

163165
if newOffersCount > 0 {
164166
slog.Info("💎 Offers sent!", "term", search.Term, "count", newOffersCount)

0 commit comments

Comments
 (0)