Skip to content

Commit

Permalink
Merge pull request #1966 from lavanet/fix-telegram-options
Browse files Browse the repository at this point in the history
fix: telegram alerting tuning
  • Loading branch information
Yaroms authored Feb 19, 2025
2 parents 6088bbb + db4aac9 commit ef7624a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
11 changes: 6 additions & 5 deletions protocol/monitoring/alerting.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ type Alerting struct {

func NewAlerting(options AlertingOptions) *Alerting {
al := &Alerting{
activeAlerts: map[AlertEntry]AlertCount{},
healthy: map[LavaEntity]struct{}{},
unhealthy: map[LavaEntity]struct{}{},
currentAlerts: map[AlertEntry]struct{}{},
payload: map[string]interface{}{},
activeAlerts: map[AlertEntry]AlertCount{},
healthy: map[LavaEntity]struct{}{},
unhealthy: map[LavaEntity]struct{}{},
currentAlerts: map[AlertEntry]struct{}{},
payload: map[string]interface{}{},
TelegramAlerting: options.TelegramAlertingOptions,
}
if options.Url != "" {
al.url = options.Url
Expand Down
58 changes: 39 additions & 19 deletions protocol/monitoring/telegram_alerting.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,50 @@ func (al *Alerting) SendTelegramAlert(alert string, attrs []utils.Attribute) err
return fmt.Errorf("telegram configuration missing")
}

message := fmt.Sprintf("%s\n", alert)
for _, attr := range attrs {
message += fmt.Sprintf("%s: %v\n", attr.Key, attr.Value)
}
send := func(message string) error {
payload := map[string]string{
"chat_id": al.TelegramAlerting.TelegramChannelID,
"text": message,
}

payload := map[string]string{
"chat_id": al.TelegramAlerting.TelegramChannelID,
"text": message,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload: %v", err)
}

jsonData, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload: %v", err)
}
url := fmt.Sprintf("%s/bot%s/sendMessage", TELEGRAM_URL, al.TelegramAlerting.TelegramBotToken)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to send telegram alert: %v", err)
}
defer resp.Body.Close()

url := fmt.Sprintf("%s/bot%s/sendMessage", TELEGRAM_URL, al.TelegramAlerting.TelegramBotToken)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to send telegram alert: %v", err)
if resp.StatusCode != http.StatusOK {
var respBody struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code,omitempty"`
Description string `json:"description,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
return fmt.Errorf("telegram API returned non-200 status: %d, failed to decode response: %v", resp.StatusCode, err)
}
return fmt.Errorf("telegram API returned non-200 status: %d, error code: %d, description: %s", resp.StatusCode, respBody.ErrorCode, respBody.Description)
}
return nil
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("telegram API returned non-200 status: %d", resp.StatusCode)
const maxMessageLength = 4096
message := fmt.Sprintf("%s\n", alert)
for _, attr := range attrs {
currentLine := fmt.Sprintf("%s: %v\n", attr.Key, attr.Value)
if len(message)+len(currentLine) > maxMessageLength {
err := send(message)
if err != nil {
fmt.Println("Error sending telegram alert:", err)
}
message = ""
}
message += currentLine
}

return nil
Expand Down

0 comments on commit ef7624a

Please sign in to comment.