Alerting: Fix so that sending an alert with the Alertmanager notifier doesn't fail when one of multiple configured URL's are down (#31079)

Fixes behaviour of Notify that returns error when one of the dispatch 
event return error, to maintain high availability, we should return error 
when all dispatched events return error instead.

Fixes #30509
This commit is contained in:
Ricky Putra 2021-02-11 22:13:53 +08:00 committed by GitHub
parent 9d36f3cc2e
commit e21f6a29b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package notifiers
import (
"context"
"fmt"
"regexp"
"strings"
"time"
@ -170,6 +171,7 @@ func (am *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error
bodyJSON := simplejson.NewFromAny(alerts)
body, _ := bodyJSON.MarshalJSON()
errCnt := 0
for _, url := range am.URL {
cmd := &models.SendWebhookSync{
@ -182,10 +184,15 @@ func (am *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
am.log.Error("Failed to send alertmanager", "error", err, "alertmanager", am.Name, "url", url)
return err
errCnt++
}
}
// This happens when every dispatch return error
if errCnt == len(am.URL) {
return fmt.Errorf("failed to send alert to alertmanager")
}
return nil
}