diff --git a/configs/config.go b/configs/config.go index 8eaead6..f7c0051 100644 --- a/configs/config.go +++ b/configs/config.go @@ -58,11 +58,12 @@ 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,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 5ad3e8d..5e9b24e 100644 --- a/configs/config.yml.example +++ b/configs/config.yml.example @@ -36,9 +36,13 @@ notifier: 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" slack: diff --git a/internal/platform/repositories/alert/telegram.go b/internal/platform/repositories/alert/telegram.go index fdb465a..4ff7136 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.Service.Name == rule || alert.Data.Service.ShortName == rule { + return true + } + } + return false +} + // CreateTelegramMessage sends a telegram message func (r *Repository) CreateTelegramMessage(alert models.Alert) error { var urls []string @@ -22,17 +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") 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) } } @@ -43,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