Skip to content

feat(alerts): telegram drop rules #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions configs/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 26 additions & 5 deletions internal/platform/repositories/alert/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable url collides with imported package name, that's why I changed it to u

urls = append(urls, url)
u := r.config.Notifier.Telegram.Host + r.config.Notifier.Telegram.Token + "/sendMessage?" + params.Encode()
urls = append(urls, u)
}
}

Expand All @@ -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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable url collides with imported package name, that's why I changed it to u

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
Expand Down