2015-09-25 16:14:46 +00:00
|
|
|
package main
|
2015-07-02 16:38:05 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-26 09:12:47 +00:00
|
|
|
"fmt"
|
2015-07-02 16:38:05 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-09-28 10:12:27 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-07-02 16:38:05 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-24 22:15:27 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-09-25 11:12:51 +00:00
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
"github.com/prometheus/alertmanager/config"
|
2015-09-29 13:12:31 +00:00
|
|
|
"github.com/prometheus/alertmanager/notify"
|
2015-09-25 11:12:51 +00:00
|
|
|
"github.com/prometheus/alertmanager/provider"
|
2015-09-25 12:38:57 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2015-07-02 16:38:05 +00:00
|
|
|
)
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
const ResolveTimeout = 30 * time.Second
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// Dispatcher sorts incoming alerts into aggregation groups and
|
|
|
|
// assigns the correct notifiers to each.
|
2015-07-02 16:38:05 +00:00
|
|
|
type Dispatcher struct {
|
2015-09-27 11:09:02 +00:00
|
|
|
routes Routes
|
|
|
|
alerts provider.Alerts
|
2015-09-29 13:12:31 +00:00
|
|
|
notifier notify.Notifier
|
2015-07-02 16:38:05 +00:00
|
|
|
|
|
|
|
aggrGroups map[model.Fingerprint]*aggrGroup
|
2015-07-04 12:41:10 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
done chan struct{}
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
2015-09-29 09:58:30 +00:00
|
|
|
|
|
|
|
log log.Logger
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// NewDispatcher returns a new Dispatcher.
|
2015-09-29 13:12:31 +00:00
|
|
|
func NewDispatcher(ap provider.Alerts, n notify.Notifier) *Dispatcher {
|
2015-09-27 11:09:02 +00:00
|
|
|
return &Dispatcher{
|
|
|
|
alerts: ap,
|
|
|
|
notifier: n,
|
2015-09-29 09:58:30 +00:00
|
|
|
log: log.With("component", "dispatcher"),
|
2015-09-27 11:09:02 +00:00
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// ApplyConfig updates the dispatcher to match the new configuration.
|
2015-09-29 10:22:13 +00:00
|
|
|
func (d *Dispatcher) ApplyConfig(conf *config.Config) bool {
|
2015-09-24 22:15:27 +00:00
|
|
|
d.mtx.Lock()
|
|
|
|
defer d.mtx.Unlock()
|
2015-07-09 13:01:38 +00:00
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
// If a cancelation function is set, the dispatcher is running.
|
|
|
|
if d.cancel != nil {
|
|
|
|
d.Stop()
|
|
|
|
defer func() { go d.Run() }()
|
|
|
|
}
|
2015-09-25 11:44:00 +00:00
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
d.routes = NewRoutes(conf.Routes)
|
2015-09-29 10:22:13 +00:00
|
|
|
|
|
|
|
return true
|
2015-07-09 13:01:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// Run starts dispatching alerts incoming via the updates channel.
|
2015-09-25 11:44:00 +00:00
|
|
|
func (d *Dispatcher) Run() {
|
2015-09-24 22:15:27 +00:00
|
|
|
d.done = make(chan struct{})
|
2015-09-25 11:44:00 +00:00
|
|
|
d.aggrGroups = map[model.Fingerprint]*aggrGroup{}
|
|
|
|
|
|
|
|
d.ctx, d.cancel = context.WithCancel(context.Background())
|
|
|
|
|
2015-09-29 08:00:02 +00:00
|
|
|
d.run(d.alerts.Subscribe())
|
2015-09-25 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 08:00:02 +00:00
|
|
|
func (d *Dispatcher) run(it provider.AlertIterator) {
|
2015-09-26 12:12:55 +00:00
|
|
|
cleanup := time.NewTicker(15 * time.Second)
|
|
|
|
defer cleanup.Stop()
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-09-29 08:00:02 +00:00
|
|
|
defer it.Close()
|
|
|
|
|
2015-07-04 12:59:52 +00:00
|
|
|
for {
|
|
|
|
select {
|
2015-09-29 08:00:02 +00:00
|
|
|
case alert := <-it.Next():
|
2015-09-29 09:58:30 +00:00
|
|
|
d.log.With("alert", alert).Debug("Received alert")
|
|
|
|
|
2015-09-29 08:00:02 +00:00
|
|
|
// Log errors but keep trying
|
|
|
|
if err := it.Err(); err != nil {
|
|
|
|
log.Errorf("Error on alert update: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-09-24 22:15:27 +00:00
|
|
|
d.mtx.RLock()
|
|
|
|
routes := d.routes.Match(alert.Labels)
|
|
|
|
d.mtx.RUnlock()
|
|
|
|
|
|
|
|
for _, r := range routes {
|
|
|
|
d.processAlert(alert, r)
|
|
|
|
}
|
|
|
|
|
2015-09-26 12:12:55 +00:00
|
|
|
case <-cleanup.C:
|
2015-07-04 12:59:52 +00:00
|
|
|
for _, ag := range d.aggrGroups {
|
|
|
|
if ag.empty() {
|
|
|
|
ag.stop()
|
|
|
|
delete(d.aggrGroups, ag.fingerprint())
|
|
|
|
}
|
|
|
|
}
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
case <-d.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// Stop the dispatcher.
|
|
|
|
func (d *Dispatcher) Stop() {
|
|
|
|
d.cancel()
|
2015-09-25 16:14:46 +00:00
|
|
|
d.cancel = nil
|
2015-09-26 09:12:47 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
<-d.done
|
|
|
|
}
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// notifyFunc is a function that performs notifcation for the alert
|
|
|
|
// with the given fingerprint. It aborts on context cancelation.
|
2015-09-26 16:12:56 +00:00
|
|
|
// Returns false iff notifying failed.
|
2015-09-26 12:12:55 +00:00
|
|
|
type notifyFunc func(context.Context, ...*types.Alert) bool
|
2015-09-24 22:15:27 +00:00
|
|
|
|
|
|
|
// processAlert determins in which aggregation group the alert falls
|
|
|
|
// and insert it.
|
2015-09-25 12:38:57 +00:00
|
|
|
func (d *Dispatcher) processAlert(alert *types.Alert, opts *RouteOpts) {
|
2015-07-02 16:38:05 +00:00
|
|
|
group := model.LabelSet{}
|
|
|
|
|
|
|
|
for ln, lv := range alert.Labels {
|
|
|
|
if _, ok := opts.GroupBy[ln]; ok {
|
|
|
|
group[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := group.Fingerprint()
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// If the group does not exist, create it.
|
2015-07-02 16:38:05 +00:00
|
|
|
ag, ok := d.aggrGroups[fp]
|
|
|
|
if !ok {
|
2015-09-25 16:14:46 +00:00
|
|
|
ag = newAggrGroup(d.ctx, group, opts)
|
2015-07-02 16:38:05 +00:00
|
|
|
d.aggrGroups[fp] = ag
|
2015-09-27 11:09:02 +00:00
|
|
|
|
|
|
|
go ag.run(func(ctx context.Context, alerts ...*types.Alert) bool {
|
|
|
|
if err := d.notifier.Notify(ctx, alerts...); err != nil {
|
|
|
|
log.Errorf("Notify for %d alerts failed: %s", len(alerts), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ag.insert(alert)
|
2015-07-04 10:52:53 +00:00
|
|
|
}
|
2015-07-02 18:48:21 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// aggrGroup aggregates alert fingerprints into groups to which a
|
|
|
|
// common set of routing options applies.
|
|
|
|
// It emits notifications in the specified intervals.
|
|
|
|
type aggrGroup struct {
|
2015-07-04 10:52:53 +00:00
|
|
|
labels model.LabelSet
|
|
|
|
opts *RouteOpts
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
done chan struct{}
|
2015-09-25 16:14:46 +00:00
|
|
|
next *time.Timer
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-07-04 10:52:53 +00:00
|
|
|
mtx sync.RWMutex
|
2015-09-25 16:14:46 +00:00
|
|
|
alerts map[model.Fingerprint]*types.Alert
|
2015-07-04 10:52:53 +00:00
|
|
|
hasSent bool
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// newAggrGroup returns a new aggregation group.
|
|
|
|
func newAggrGroup(ctx context.Context, labels model.LabelSet, opts *RouteOpts) *aggrGroup {
|
2015-07-02 18:48:21 +00:00
|
|
|
ag := &aggrGroup{
|
2015-07-04 10:52:53 +00:00
|
|
|
labels: labels,
|
|
|
|
opts: opts,
|
2015-09-25 16:14:46 +00:00
|
|
|
alerts: map[model.Fingerprint]*types.Alert{},
|
2015-07-02 18:48:21 +00:00
|
|
|
}
|
2015-09-24 22:15:27 +00:00
|
|
|
ag.ctx, ag.cancel = context.WithCancel(ctx)
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-07-02 18:48:21 +00:00
|
|
|
return ag
|
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-09-26 15:54:49 +00:00
|
|
|
func (ag *aggrGroup) String() string {
|
|
|
|
return fmt.Sprintf("%x", ag.fingerprint())
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:12:31 +00:00
|
|
|
func (ag *aggrGroup) run(nf notifyFunc) {
|
2015-09-24 22:15:27 +00:00
|
|
|
ag.done = make(chan struct{})
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
// Set an initial one-time wait before flushing
|
|
|
|
// the first batch of notifications.
|
2015-09-25 16:14:46 +00:00
|
|
|
ag.next = time.NewTimer(ag.opts.GroupWait)
|
2015-09-24 22:15:27 +00:00
|
|
|
|
|
|
|
defer close(ag.done)
|
2015-07-04 10:52:53 +00:00
|
|
|
defer ag.next.Stop()
|
|
|
|
|
2015-07-02 16:38:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
2015-07-04 10:52:53 +00:00
|
|
|
case <-ag.next.C:
|
2015-09-26 16:03:54 +00:00
|
|
|
// Give the notifcations time until the next flush to
|
|
|
|
// finish before terminating them.
|
|
|
|
ctx, _ := context.WithTimeout(ag.ctx, ag.opts.GroupInterval)
|
2015-09-24 22:15:27 +00:00
|
|
|
|
2015-09-27 11:09:02 +00:00
|
|
|
// Populate context with the destination name.
|
2015-09-29 13:12:31 +00:00
|
|
|
ctx = context.WithValue(ctx, notify.NotifyName, ag.opts.SendTo)
|
2015-09-27 11:09:02 +00:00
|
|
|
|
2015-07-04 10:52:53 +00:00
|
|
|
// Wait the configured interval before calling flush again.
|
2015-09-26 16:03:54 +00:00
|
|
|
ag.next.Reset(ag.opts.GroupInterval)
|
2015-07-04 10:52:53 +00:00
|
|
|
|
2015-09-26 12:12:55 +00:00
|
|
|
ag.flush(func(alerts ...*types.Alert) bool {
|
2015-09-29 13:12:31 +00:00
|
|
|
return nf(ctx, alerts...)
|
2015-09-24 22:15:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
case <-ag.ctx.Done():
|
2015-07-02 16:38:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ag *aggrGroup) stop() {
|
2015-09-24 22:15:27 +00:00
|
|
|
// Calling cancel will terminate all in-process notifications
|
|
|
|
// and the run() loop.
|
|
|
|
ag.cancel()
|
|
|
|
<-ag.done
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ag *aggrGroup) fingerprint() model.Fingerprint {
|
|
|
|
return ag.labels.Fingerprint()
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:48:21 +00:00
|
|
|
// insert the alert into the aggregation group. If the aggregation group
|
|
|
|
// is empty afterwards, true is returned.
|
2015-09-25 16:14:46 +00:00
|
|
|
func (ag *aggrGroup) insert(alert *types.Alert) {
|
2015-07-04 10:52:53 +00:00
|
|
|
ag.mtx.Lock()
|
2015-09-24 22:15:27 +00:00
|
|
|
defer ag.mtx.Unlock()
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
ag.alerts[alert.Fingerprint()] = alert
|
2015-07-02 18:48:21 +00:00
|
|
|
|
|
|
|
// Immediately trigger a flush if the wait duration for this
|
|
|
|
// alert is already over.
|
2015-09-29 15:26:44 +00:00
|
|
|
if !ag.hasSent && alert.UpdatedAt.Add(ag.opts.GroupWait).Before(time.Now()) {
|
2015-07-04 10:52:53 +00:00
|
|
|
ag.next.Reset(0)
|
2015-07-02 16:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:48:21 +00:00
|
|
|
func (ag *aggrGroup) empty() bool {
|
|
|
|
ag.mtx.RLock()
|
|
|
|
defer ag.mtx.RUnlock()
|
|
|
|
|
2015-07-04 10:52:53 +00:00
|
|
|
return len(ag.alerts) == 0
|
2015-07-02 18:48:21 +00:00
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-07-02 18:48:21 +00:00
|
|
|
// flush sends notifications for all new alerts.
|
2015-09-26 12:12:55 +00:00
|
|
|
func (ag *aggrGroup) flush(notify func(...*types.Alert) bool) {
|
2015-09-27 17:50:41 +00:00
|
|
|
if ag.empty() {
|
|
|
|
return
|
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
ag.mtx.Lock()
|
|
|
|
|
2015-09-26 12:12:55 +00:00
|
|
|
var (
|
|
|
|
alerts = make(map[model.Fingerprint]*types.Alert, len(ag.alerts))
|
|
|
|
alertsSlice = make([]*types.Alert, 0, len(ag.alerts))
|
|
|
|
)
|
2015-09-25 16:14:46 +00:00
|
|
|
for fp, alert := range ag.alerts {
|
|
|
|
alerts[fp] = alert
|
2015-09-26 12:12:55 +00:00
|
|
|
alertsSlice = append(alertsSlice, alert)
|
2015-07-04 10:52:53 +00:00
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
ag.mtx.Unlock()
|
|
|
|
|
2015-09-26 12:12:55 +00:00
|
|
|
if notify(alertsSlice...) {
|
|
|
|
ag.mtx.Lock()
|
|
|
|
for fp, a := range alerts {
|
|
|
|
// Only delete if the fingerprint has not been inserted
|
|
|
|
// again since we notified about it.
|
|
|
|
if a.Resolved() && ag.alerts[fp] == a {
|
2015-09-26 15:54:49 +00:00
|
|
|
delete(ag.alerts, fp)
|
2015-09-24 22:15:27 +00:00
|
|
|
}
|
2015-09-26 12:12:55 +00:00
|
|
|
}
|
|
|
|
ag.mtx.Unlock()
|
2015-07-10 17:25:56 +00:00
|
|
|
}
|
2015-07-02 16:38:05 +00:00
|
|
|
|
2015-09-24 22:15:27 +00:00
|
|
|
ag.hasSent = true
|
|
|
|
}
|