From d5ff099d056a9d58d542469ff391385656512930 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sat, 4 Jul 2015 14:41:10 +0200 Subject: [PATCH] cleanup dispatcher notifications, fix default route opts --- main.go | 5 ++++- manager/api.go | 8 ++++++-- manager/dispatch.go | 37 ++++++++++++++++++++++++++----------- manager/notify.go | 20 +++++++++++++++----- manager/route.go | 4 ++-- 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 62ff96dc..d3662b6b 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,10 @@ func main() { log.Fatal(err) } - disp := manager.NewDispatcher(state) + disp := manager.NewDispatcher(state, []manager.Notifier{ + manager.NewLogNotifier("default"), + }) + router := route.New() go disp.Run() diff --git a/manager/api.go b/manager/api.go index a347ef69..870b52a9 100644 --- a/manager/api.go +++ b/manager/api.go @@ -73,9 +73,13 @@ func (api *API) addAlerts(w http.ResponseWriter, r *http.Request) { return } for _, alert := range alerts { + now := time.Now() + if alert.Timestamp.IsZero() { - alert.Timestamp = time.Now() - alert.CreatedAt = alert.Timestamp + alert.Timestamp = now + } + if alert.CreatedAt.IsZero() { + alert.CreatedAt = now } } diff --git a/manager/dispatch.go b/manager/dispatch.go index 662404e9..ae626b85 100644 --- a/manager/dispatch.go +++ b/manager/dispatch.go @@ -18,25 +18,39 @@ type Dispatcher struct { state State aggrGroups map[model.Fingerprint]*aggrGroup + notifiers map[string]Notifier + + mtx sync.RWMutex } -func NewDispatcher(state State) *Dispatcher { - return &Dispatcher{ +func NewDispatcher(state State, notifiers []Notifier) *Dispatcher { + disp := &Dispatcher{ state: state, aggrGroups: map[model.Fingerprint]*aggrGroup{}, + notifiers: map[string]Notifier{}, } + + for _, n := range notifiers { + disp.notifiers[n.Name()] = n + } + + return disp } func (d *Dispatcher) notify(name string, alerts ...*Alert) error { if len(alerts) == 0 { - return + return nil } - n := &LogNotifier{} - i := []interface{}{name, "::"} - for _, a := range alerts { - i = append(i, a) + + d.mtx.RLock() + notifier, ok := d.notifiers[name] + d.mtx.RUnlock() + + if !ok { + return fmt.Errorf("notifier %q does not exist", name) } - n.Send(i...) + + return notifier.Send(alerts...) } func (d *Dispatcher) Run() { @@ -171,6 +185,10 @@ func newAggrGroup(d *Dispatcher, labels model.LabelSet, opts *RouteOpts) *aggrGr alerts: map[model.Fingerprint]struct{}{}, done: make(chan struct{}), + + // Set an initial one-time wait before flushing + // the first batch of notifications. + next: time.NewTimer(opts.GroupWait), } go ag.run() @@ -179,9 +197,6 @@ func newAggrGroup(d *Dispatcher, labels model.LabelSet, opts *RouteOpts) *aggrGr } func (ag *aggrGroup) run() { - // Set an initial one-time wait before flushing - // the first batch of notifications. - ag.next = time.NewTimer(ag.opts.GroupWait) defer ag.next.Stop() diff --git a/manager/notify.go b/manager/notify.go index 730168f5..117ece9c 100644 --- a/manager/notify.go +++ b/manager/notify.go @@ -6,16 +6,26 @@ import ( type Notifier interface { Name() string - Send(...interface{}) + Send(...*Alert) error } type LogNotifier struct { + name string } -func (*LogNotifier) Name() string { - return "default" +func NewLogNotifier(name string) Notifier { + return &LogNotifier{name} } -func (*LogNotifier) Send(v ...interface{}) { - log.Infoln(v...) +func (ln *LogNotifier) Name() string { + return ln.name +} + +func (ln *LogNotifier) Send(alerts ...*Alert) error { + log.Infof("notify %q", ln.name) + + for _, a := range alerts { + log.Infof(" - %v", a) + } + return nil } diff --git a/manager/route.go b/manager/route.go index 00b101cd..0b78ece0 100644 --- a/manager/route.go +++ b/manager/route.go @@ -167,10 +167,10 @@ func (ro *RouteOpts) populateDefault(parent *RouteOpts) { if ro.SendTo == "" { ro.SendTo = parent.SendTo } - if ro.hasWait { + if !ro.hasWait { ro.GroupWait = parent.GroupWait } - if ro.hasInterval { + if !ro.hasInterval { ro.GroupInterval = parent.GroupInterval } }