From 9c5468786dade87e29d1c54a111fce14df2a3621 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sun, 11 Oct 2015 16:54:31 +0200 Subject: [PATCH] Simplify notifier constructors --- notify/impl.go | 4 +-- notify/notify.go | 73 +++++++++++++++++++++---------------------- notify/notify_test.go | 16 +++------- 3 files changed, 43 insertions(+), 50 deletions(-) diff --git a/notify/impl.go b/notify/impl.go index 66b7334d..dcfa07c1 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -33,10 +33,10 @@ func Build(confs []*config.NotificationConfig, tmpl *template.Template) map[stri ) for i, c := range nc.WebhookConfigs { - add(i, notify.NewWebhook(c)) + add(i, NewWebhook(c)) } for i, c := range nc.EmailConfigs { - add(i, notify.NewEmail(c, tmpl)) + add(i, NewEmail(c, tmpl)) } res[nc.Name] = fo diff --git a/notify/notify.go b/notify/notify.go index b8c90388..125890d6 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -10,7 +10,6 @@ import ( "github.com/prometheus/common/model" "golang.org/x/net/context" - "github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/provider" "github.com/prometheus/alertmanager/types" ) @@ -82,7 +81,7 @@ type Notifier interface { // at once. type Fanout map[string]Notifier -func (ns Notifiers) Notify(ctx context.Context, alerts ...*types.Alert) error { +func (ns Fanout) Notify(ctx context.Context, alerts ...*types.Alert) error { var ( wg sync.WaitGroup me types.MultiError @@ -94,9 +93,9 @@ func (ns Notifiers) Notify(ctx context.Context, alerts ...*types.Alert) error { return fmt.Errorf("destination missing") } - for prefix, n := range ns { + for suffix, n := range ns { // Suffix the destination with the unique key for the fanout. - foCtx := WithDestination(ctx, fmt.Sprintf("%s/%s", dest, prefix)) + foCtx := WithDestination(ctx, fmt.Sprintf("%s/%s", dest, suffix)) go func(n Notifier) { if err := n.Notify(foCtx, alerts...); err != nil { @@ -109,11 +108,18 @@ func (ns Notifiers) Notify(ctx context.Context, alerts ...*types.Alert) error { wg.Wait() - return me + if len(me) > 0 { + return me + } + return nil } type RetryNotifier struct { - Notifier Notifier + notifier Notifier +} + +func Retry(n Notifier) *RetryNotifier { + return &RetryNotifier{notifier: n} } func (n *RetryNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { @@ -129,7 +135,7 @@ func (n *RetryNotifier) Notify(ctx context.Context, alerts ...*types.Alert) erro select { case <-tick.C: - if err := n.Notifier.Notify(ctx, alerts...); err != nil { + if err := n.notifier.Notify(ctx, alerts...); err != nil { log.Warnf("Notify attempt %d failed: %s", i, err) } else { return nil @@ -147,11 +153,8 @@ type DedupingNotifier struct { notifier Notifier } -func NewDedupingNotifier(notifies provider.Notifies, n Notifier) *DedupingNotifier { - return &DedupingNotifier{ - notifies: notifies, - notifier: n, - } +func Dedup(notifies provider.Notifies, n Notifier) *DedupingNotifier { + return &DedupingNotifier{notifies: notifies, notifier: n} } func (n *DedupingNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { @@ -263,39 +266,31 @@ func (n *DedupingNotifier) Notify(ctx context.Context, alerts ...*types.Alert) e // RoutedNotifier dispatches the alerts to one of a set of // named notifiers based on the name value provided in the context. -type RoutedNotifier struct { - sync.RWMutex - Notifiers map[string]Notifier -} +type Router map[string]Notifier -func (n *RoutedNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { - name, ok := Destination(ctx) +func (rs Router) Notify(ctx context.Context, alerts ...*types.Alert) error { + dest, ok := Destination(ctx) if !ok { return fmt.Errorf("notifier name missing") } - n.RLock() - defer n.RUnlock() - - notifier, ok := n.Notifiers[name] + notifier, ok := rs[dest] if !ok { - return fmt.Errorf("notifier %q does not exist", name) + return fmt.Errorf("notifier %q does not exist", dest) } return notifier.Notify(ctx, alerts...) } -type NotifyFunc func(ctx context.Context, alerts ...*types.Alert) error - -func (f NotifyFunc) Notify(ctx context.Context, alerts ...*types.Alert) error { - return f(ctx, alerts...) -} - // MutingNotifier wraps a notifier and applies a Silencer // before sending out an alert. type MutingNotifier struct { types.Muter - Notifier Notifier + notifier Notifier +} + +func Mute(m types.Muter, n Notifier) *MutingNotifier { + return &MutingNotifier{Muter: m, notifier: n} } func (n *MutingNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { @@ -309,19 +304,23 @@ func (n *MutingNotifier) Notify(ctx context.Context, alerts ...*types.Alert) err } } - return n.Notifier.Notify(ctx, filtered...) + return n.notifier.Notify(ctx, filtered...) } type LogNotifier struct { - Log log.Logger - Notifier Notifier + log log.Logger + notifier Notifier } -func (ln *LogNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { - ln.Log.Debugf("notify %v", alerts) +func Log(n Notifier, log log.Logger) *LogNotifier { + return &LogNotifier{log: log, notifier: n} +} - if ln.Notifier != nil { - return ln.Notifier.Notify(ctx, alerts...) +func (n *LogNotifier) Notify(ctx context.Context, alerts ...*types.Alert) error { + n.log.Debugf("notify %v", alerts) + + if n.notifier != nil { + return n.notifier.Notify(ctx, alerts...) } return nil } diff --git a/notify/notify_test.go b/notify/notify_test.go index a2f5f6ec..0839e46a 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -34,7 +34,7 @@ func TestDedupingNotifier(t *testing.T) { var ( record = &recordNotifier{} notifies = provider.NewMemNotifies(provider.NewMemData()) - deduper = NewDedupingNotifier(notifies, record) + deduper = Dedup(notifies, record) ctx = context.Background() ) now := time.Now() @@ -226,14 +226,11 @@ func TestDedupingNotifier(t *testing.T) { } func TestRoutedNotifier(t *testing.T) { - notifiers := map[string]Notifier{ + router := Router{ "1": &recordNotifier{}, "2": &recordNotifier{}, "3": &recordNotifier{}, } - routed := &RoutedNotifier{ - Notifiers: notifiers, - } for _, route := range []string{"3", "2", "1"} { var ( @@ -244,12 +241,12 @@ func TestRoutedNotifier(t *testing.T) { }, } ) - err := routed.Notify(ctx, alert) + err := router.Notify(ctx, alert) if err != nil { t.Fatal(err) } - rn := routed.Notifiers[route].(*recordNotifier) + rn := router[route].(*recordNotifier) if len(rn.alerts) != 1 && alert != rn.alerts[0] { t.Fatalf("Expeceted alert %v, got %v", alert, rn.alerts) } @@ -264,10 +261,7 @@ func TestMutingNotifier(t *testing.T) { }) record := &recordNotifier{} - muteNotifer := MutingNotifier{ - Notifier: record, - Muter: muter, - } + muteNotifer := Mute(muter, record) in := []model.LabelSet{ {},