From 0514b9ba432588852e47d61ad2e7defc6727163e Mon Sep 17 00:00:00 2001 From: Ali Firoozi Zamani Date: Sat, 28 Sep 2024 10:53:05 +0330 Subject: [PATCH 1/4] feat(alerts): telegram drop rules --- configs/config.go | 1 + configs/config.yml.example | 2 ++ internal/platform/repositories/alert/telegram.go | 13 +++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/configs/config.go b/configs/config.go index 8eaead6..87f180b 100644 --- a/configs/config.go +++ b/configs/config.go @@ -41,6 +41,7 @@ type Config struct { IsEnabled bool `yaml:"enable"` Host string `yaml:"host"` Token string `yaml:"token"` + Drop []string `yaml:"drop"` Teams map[string][]TelegramTeam `yaml:"teams"` } `yaml:"telegram"` Slack struct { diff --git a/configs/config.yml.example b/configs/config.yml.example index 5ad3e8d..7989479 100644 --- a/configs/config.yml.example +++ b/configs/config.yml.example @@ -33,6 +33,8 @@ notifier: enable: true host: "https://api.telegram.org/bot" token: "1234:ABCD" + drop_rules: + - new_api_v2_alert teams: team1: - chat: "-123456789" diff --git a/internal/platform/repositories/alert/telegram.go b/internal/platform/repositories/alert/telegram.go index fdb465a..7347e83 100644 --- a/internal/platform/repositories/alert/telegram.go +++ b/internal/platform/repositories/alert/telegram.go @@ -28,8 +28,17 @@ func (r *Repository) CreateTelegramMessage(alert models.Alert) error { params := url.Values{} params.Add("chat_id", t.Chat) params.Add("parse_mode", "markdownv2") - if t.Topic != "" { - params.Add("message_thread_id", t.Topic) + // Don't send message if the the topic is matched to any dropped rules. + if len(r.config.Notifier.Telegram.Drop) > 0 { + for _, rule := range r.config.Notifier.Telegram.Drop { + if rule != t.Topic && t.Topic != "" { + params.Add("message_thread_id", t.Topic) + } + } + } else { + if t.Topic != "" { + params.Add("message_thread_id", t.Topic) + } } url := r.config.Notifier.Telegram.Host + r.config.Notifier.Telegram.Token + "/sendMessage?" + params.Encode() urls = append(urls, url) From e36147ff09eed2d32fbeedbf11a1768c3746d2ec Mon Sep 17 00:00:00 2001 From: Ali Firoozi Zamani Date: Sat, 5 Oct 2024 09:53:31 +0330 Subject: [PATCH 2/4] feat(alerts): telegram drop rules update --- configs/config.go | 6 +++--- configs/config.yml.example | 7 +++++-- internal/platform/repositories/alert/telegram.go | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/configs/config.go b/configs/config.go index 87f180b..bf3c20b 100644 --- a/configs/config.go +++ b/configs/config.go @@ -41,7 +41,6 @@ type Config struct { IsEnabled bool `yaml:"enable"` Host string `yaml:"host"` Token string `yaml:"token"` - Drop []string `yaml:"drop"` Teams map[string][]TelegramTeam `yaml:"teams"` } `yaml:"telegram"` Slack struct { @@ -59,8 +58,9 @@ type Config struct { // TelegramTeam is a struct for Telegram team configuration type TelegramTeam struct { - Chat string `yaml:"chat"` - Topic string `yaml:"topic"` + Chat string `yaml:"chat"` + Topic string `yaml:"topic"` + DropRules []string `yaml:"drop_rules"` } // Load will loads the configuration from the given path diff --git a/configs/config.yml.example b/configs/config.yml.example index 7989479..4fd06e6 100644 --- a/configs/config.yml.example +++ b/configs/config.yml.example @@ -33,16 +33,19 @@ notifier: enable: true host: "https://api.telegram.org/bot" token: "1234:ABCD" - drop_rules: - - new_api_v2_alert teams: team1: - chat: "-123456789" + drop_rules: + - new_api_v2_alert team2: - chat: "-124564" topic: "56789" + drop_rules: + - new_api_v2_alert - chat: "-356457" topic: "982456" + drop_rules: [] slack: enable: true teams: diff --git a/internal/platform/repositories/alert/telegram.go b/internal/platform/repositories/alert/telegram.go index 7347e83..985a0ed 100644 --- a/internal/platform/repositories/alert/telegram.go +++ b/internal/platform/repositories/alert/telegram.go @@ -29,8 +29,8 @@ func (r *Repository) CreateTelegramMessage(alert models.Alert) error { params.Add("chat_id", t.Chat) params.Add("parse_mode", "markdownv2") // Don't send message if the the topic is matched to any dropped rules. - if len(r.config.Notifier.Telegram.Drop) > 0 { - for _, rule := range r.config.Notifier.Telegram.Drop { + if len(t.DropRules) > 0 { + for _, rule := range t.DropRules { if rule != t.Topic && t.Topic != "" { params.Add("message_thread_id", t.Topic) } From 366d89bf2283b4e9cac71c69c2a5110c79a6c196 Mon Sep 17 00:00:00 2001 From: Ali Firoozi Zamani Date: Fri, 14 Mar 2025 17:30:05 +0330 Subject: [PATCH 3/4] fix: Fix the drop rules logic --- configs/config.go | 4 +- configs/config.yml.example | 1 - .../platform/repositories/alert/telegram.go | 44 ++++++++++++------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/configs/config.go b/configs/config.go index bf3c20b..f7c0051 100644 --- a/configs/config.go +++ b/configs/config.go @@ -60,10 +60,10 @@ type Config struct { type TelegramTeam struct { Chat string `yaml:"chat"` Topic string `yaml:"topic"` - DropRules []string `yaml:"drop_rules"` + DropRules []string `yaml:"drop_rules,omitempty"` } -// Load will loads the configuration from the given path +// Load will load the configuration from the given path func Load(configPath string) (*Config, error) { var cfg Config absPath, err := filepath.Abs(configPath) diff --git a/configs/config.yml.example b/configs/config.yml.example index 4fd06e6..5e9b24e 100644 --- a/configs/config.yml.example +++ b/configs/config.yml.example @@ -45,7 +45,6 @@ notifier: - new_api_v2_alert - chat: "-356457" topic: "982456" - drop_rules: [] slack: enable: true teams: diff --git a/internal/platform/repositories/alert/telegram.go b/internal/platform/repositories/alert/telegram.go index 985a0ed..6eb8b14 100644 --- a/internal/platform/repositories/alert/telegram.go +++ b/internal/platform/repositories/alert/telegram.go @@ -10,6 +10,16 @@ import ( log "github.com/sirupsen/logrus" ) +// shouldDropAlert checks if the alert matches any of the drop rules for the team. +func shouldDropAlert(alert models.Alert, dropRules []string) bool { + for _, rule := range dropRules { + if alert.Data.Alert.Output == rule || alert.Data.Alert.ShortOutput == rule || alert.Event == rule { + return true + } + } + return false +} + // CreateTelegramMessage sends a telegram message func (r *Repository) CreateTelegramMessage(alert models.Alert) error { var urls []string @@ -22,26 +32,24 @@ func (r *Repository) CreateTelegramMessage(alert models.Alert) error { continue } - // Create a list of URLs for every targets in the team. + // Create a list of URLs for every target in the team. // For example, a team needs to send a message to 2 chats, so we create 2 URLs. for _, t := range team { + + // Check if the alert matches any drop rules for this team. + if shouldDropAlert(alert, t.DropRules) { + log.Infof("[Telegram] Alert dropped for team %s due to drop rules: %v", tag, t.DropRules) + continue + } + params := url.Values{} params.Add("chat_id", t.Chat) params.Add("parse_mode", "markdownv2") - // Don't send message if the the topic is matched to any dropped rules. - if len(t.DropRules) > 0 { - for _, rule := range t.DropRules { - if rule != t.Topic && t.Topic != "" { - params.Add("message_thread_id", t.Topic) - } - } - } else { - if t.Topic != "" { - params.Add("message_thread_id", t.Topic) - } + if t.Topic != "" { + params.Add("message_thread_id", t.Topic) } - url := r.config.Notifier.Telegram.Host + r.config.Notifier.Telegram.Token + "/sendMessage?" + params.Encode() - urls = append(urls, url) + u := r.config.Notifier.Telegram.Host + r.config.Notifier.Telegram.Token + "/sendMessage?" + params.Encode() + urls = append(urls, u) } } @@ -52,8 +60,12 @@ func (r *Repository) CreateTelegramMessage(alert models.Alert) error { return err } - for _, url := range urls { - r.CallTelegram(url, body) + for _, u := range urls { + err = r.CallTelegram(u, body) + if err != nil { + log.Errorf("[Telegram] Failed to send Telegram message: %s", err) + r.monitoring.Record([]monitoring.Event{monitoring.NewEvent(monitoring.IncTelegramSendFailure)}) + } } return nil From f1830d893f0a05d40cb312a316e8775305f735bf Mon Sep 17 00:00:00 2001 From: Ali Firoozi Zamani Date: Fri, 14 Mar 2025 19:29:30 +0330 Subject: [PATCH 4/4] chore: check the name rule --- internal/platform/repositories/alert/telegram.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/platform/repositories/alert/telegram.go b/internal/platform/repositories/alert/telegram.go index 6eb8b14..4ff7136 100644 --- a/internal/platform/repositories/alert/telegram.go +++ b/internal/platform/repositories/alert/telegram.go @@ -13,7 +13,7 @@ import ( // shouldDropAlert checks if the alert matches any of the drop rules for the team. func shouldDropAlert(alert models.Alert, dropRules []string) bool { for _, rule := range dropRules { - if alert.Data.Alert.Output == rule || alert.Data.Alert.ShortOutput == rule || alert.Event == rule { + if alert.Data.Service.Name == rule || alert.Data.Service.ShortName == rule { return true } }