mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-26 15:50:46 +00:00
Implement retrying notifier
Retrying notifier is added to the end of the pipeline where it retries sending out the final notifications until the context times out. Exponential backoff is used.
This commit is contained in:
parent
9bf18ec4a5
commit
9cd90d3482
6
main.go
6
main.go
@ -65,10 +65,14 @@ func main() {
|
||||
routedNotifier := notify.NewRoutedNotifier(func(confs []*config.NotificationConfig) map[string]notify.Notifier {
|
||||
res := notify.Build(confs)
|
||||
for name, n := range res {
|
||||
res[name] = ¬ify.LogNotifier{
|
||||
n = ¬ify.RetryNotifier{
|
||||
Notifier: n,
|
||||
}
|
||||
n = ¬ify.LogNotifier{
|
||||
Log: log.With("notifier", fmt.Sprintf("%T", n)),
|
||||
Notifier: n,
|
||||
}
|
||||
res[name] = n
|
||||
}
|
||||
return res
|
||||
})
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff"
|
||||
"github.com/prometheus/common/log"
|
||||
"github.com/prometheus/common/model"
|
||||
"golang.org/x/net/context"
|
||||
@ -100,6 +101,36 @@ func (ns Notifiers) Notify(ctx context.Context, alerts ...*types.Alert) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type RetryNotifier struct {
|
||||
Notifier Notifier
|
||||
}
|
||||
|
||||
func (n *RetryNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error {
|
||||
var (
|
||||
i = 0
|
||||
b = backoff.NewExponentialBackOff()
|
||||
tick = backoff.NewTicker(b)
|
||||
)
|
||||
defer tick.Stop()
|
||||
|
||||
for {
|
||||
i++
|
||||
|
||||
select {
|
||||
case <-tick.C:
|
||||
if err := n.Notifier.Notify(ctx, alerts...); err != nil {
|
||||
log.Warnf("notify attempt %d failed: %s", i, err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type DedupingNotifier struct {
|
||||
notifies provider.Notifies
|
||||
notifier Notifier
|
||||
|
@ -27,7 +27,7 @@ func (n *recordNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
|
||||
type failNotifier struct{}
|
||||
|
||||
func (n *failNotifier) Notify(ctx context.Context, as ...*types.Alert) error {
|
||||
return fmt.Errorf("")
|
||||
return fmt.Errorf("some error")
|
||||
}
|
||||
|
||||
func TestDedupingNotifier(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user