Change notifier interface, add silencing notifier

This commit is contained in:
Fabian Reinartz 2015-09-25 00:14:41 +02:00
parent 3cc6044989
commit 4de9878f37
1 changed files with 71 additions and 11 deletions

View File

@ -2,26 +2,18 @@ package manager
import ( import (
"github.com/prometheus/log" "github.com/prometheus/log"
"golang.org/x/net/context"
) )
type Notifier interface { type Notifier interface {
Name() string Notify(context.Context, *Alert) error
Send(...*Alert) error
} }
type LogNotifier struct { type LogNotifier struct {
name string name string
} }
func NewLogNotifier(name string) Notifier { func (ln *LogNotifier) Notify(ctx context.Context, a *Alert) error {
return &LogNotifier{name}
}
func (ln *LogNotifier) Name() string {
return ln.name
}
func (ln *LogNotifier) Send(alerts ...*Alert) error {
log.Infof("notify %q", ln.name) log.Infof("notify %q", ln.name)
for _, a := range alerts { for _, a := range alerts {
@ -29,3 +21,71 @@ func (ln *LogNotifier) Send(alerts ...*Alert) error {
} }
return nil return nil
} }
// routedNotifier forwards alerts to notifiers matching the alert in
// a routing tree.
type routedNotifier struct {
notifiers map[string]Notifier
}
func (n *routedNotifier) Notify(alert *Alert) error {
}
// A Silencer determines whether a given label set is muted.
type Silencer interface {
Mutes(model.LabelSet) bool
}
// A Silence determines whether a given label set is muted
// at the current time.
type Silence struct {
ID model.Fingerprint
// A set of matchers determining if an alert is
Matchers Matchers
// Name/email of the silence creator.
CreatedBy string
// When the silence was first created (Unix timestamp).
CreatedAt, EndsAt time.Time
// Additional comment about the silence.
Comment string
// timeFunc provides the time against which to evaluate
// the silence.
timeFunc func() time.Time
}
func (sil *Silence) Mutes(lset model.LabelSet) bool {
t := sil.timeFunc()
if t.Before(sil.CreatedAt) || t.After(sil.EndsAt) {
return false
}
return sil.Matchers.Match(lset)
}
// silencedNotifier wraps a notifier and applies a Silencer
// before sending out an alert.
type silencedNotifier struct {
Notifier
silencer Silencer
}
func (n *silencedNotifier) Notify(alert *Alert) error {
// TODO(fabxc): increment total alerts counter.
// Do not send the alert if the silencer mutes it.
if n.silencer.Mutes(alert.Labels) {
// TODO(fabxc): increment muted alerts counter.
return nil
}
return n.Notifier.Send(alert)
}
type Inhibitor interface {
Inhibits(model.LabelSet) bool
}