Skip to content

Commit

Permalink
Merge pull request #21 from lidofinance/fix-sending-networks-alerts
Browse files Browse the repository at this point in the history
feat: Fix sending network alerts though telegram
  • Loading branch information
sergeyWh1te authored Sep 24, 2024
2 parents 0d6941a + 569ed83 commit 045969c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 24.09.2024
1. Fix sending network alerts though telegram

## 23.09.2024
1. Add lru for quorum
2. Tun docker-compose-file
Expand Down
23 changes: 20 additions & 3 deletions internal/app/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
err error
)

// TODO add finding.blockNumber
w.log.Info(fmt.Sprintf("Consumer: %s AlertId %s read %d times", consumer.Name, finding.AlertId, count),
slog.Attr{
Key: `desc`,
Value: slog.StringValue(finding.Description),
},
slog.Attr{
Key: `name`,
Value: slog.StringValue(finding.Name),
},
slog.Attr{
Key: `alertId`,
Value: slog.StringValue(finding.AlertId),
},
slog.Attr{
Key: `severity`,
Value: slog.StringValue(finding.Severity.String()),
},
)

if !w.cache.Contains(key) {
count, err = w.redisClient.Incr(ctx, countKey).Uint64()
if err != nil {
Expand Down Expand Up @@ -263,9 +283,6 @@ func (w *findingWorker) Run(ctx context.Context, g *errgroup.Group) error {
}
}

// TODO add finding.blockNumber
w.log.InfoContext(ctx, fmt.Sprintf("Consumer: %s AlertId %s read %d times", consumer.Name, finding.AlertId, count))

if count >= w.quorum {
status, err := w.GetStatus(ctx, statusKey)
if err != nil {
Expand Down
20 changes: 16 additions & 4 deletions internal/pkg/notifiler/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,30 @@ func NewTelegram(botToken, chatID string, httpClient *http.Client, metricsStore
func (u *telegram) SendFinding(ctx context.Context, alert *proto.Finding) error {
message := fmt.Sprintf("%s\n\n%s\n\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.GetAlertId(), u.source)

return u.send(ctx, message)
if alert.Severity != proto.Finding_UNKNOWN {
return u.send(ctx, message, true)
}

return u.send(ctx, message, false)
}

func (u *telegram) SendAlert(ctx context.Context, alert *models.Alert) error {
message := fmt.Sprintf("%s\n\n%s\n\nAlertId: %s\nSource: %s", alert.Name, alert.Description, alert.AlertID, u.source)

return u.send(ctx, message)
if alert.Severity != models.AlertSeverityUNKNOWN {
return u.send(ctx, message, true)
}

return u.send(ctx, message, false)
}

func (u *telegram) send(ctx context.Context, message string) error {
func (u *telegram) send(ctx context.Context, message string, useMarkdown bool) error {
//nolint
requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=-%s&text=%s&parse_mode=markdown", u.botToken, u.chatID, url.QueryEscape(message))
requestURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=-%s&text=%s", u.botToken, u.chatID, url.QueryEscape(message))
if useMarkdown {
requestURL += `&parse_mode=markdown`
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, http.NoBody)
if err != nil {
return fmt.Errorf("could not create telegram request: %w", err)
Expand Down

0 comments on commit 045969c

Please sign in to comment.