From ef0ee97781d47d2ef7cc3d4b784f0a35166ead88 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Mon, 19 Oct 2015 16:17:15 +0200 Subject: [PATCH] Implement global routing options --- config/config.go | 11 +++++++++++ dispatch.go | 9 ++++----- main.go | 10 +++++++++- route.go | 26 +++----------------------- route_test.go | 9 +++++++-- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/config/config.go b/config/config.go index ea7a34e2..965676af 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "github.com/prometheus/common/model" "gopkg.in/yaml.v2" @@ -155,10 +156,20 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { } var DefaultGlobalConfig = GlobalConfig{ + GroupWait: model.Duration(30 * time.Second), + GroupInterval: model.Duration(5 * time.Minute), + RepeatInterval: model.Duration(1 * time.Hour), + SendResolved: true, + PagerdutyURL: "https://events.pagerduty.com/generic/2010-04-15/create_event.json", } type GlobalConfig struct { + GroupWait model.Duration `yaml:"group_wait"` + GroupInterval model.Duration `yaml:"group_interval"` + RepeatInterval model.Duration `yaml:"repeat_interval"` + SendResolved bool `yaml:"send_resolved"` + Smarthost string `yaml:"smarthost"` SlackURL string `yaml:"slack_url"` PagerdutyURL string `yaml:"pagerduty_url"` diff --git a/dispatch.go b/dispatch.go index ddb0de22..22156775 100644 --- a/dispatch.go +++ b/dispatch.go @@ -9,7 +9,6 @@ import ( "github.com/prometheus/common/model" "golang.org/x/net/context" - "github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/provider" "github.com/prometheus/alertmanager/types" @@ -20,7 +19,7 @@ const ResolveTimeout = 5 * time.Minute // Dispatcher sorts incoming alerts into aggregation groups and // assigns the correct notifiers to each. type Dispatcher struct { - routes Routes + route *Route alerts provider.Alerts notifier notify.Notifier @@ -34,11 +33,11 @@ type Dispatcher struct { } // NewDispatcher returns a new Dispatcher. -func NewDispatcher(ap provider.Alerts, r []*config.Route, n notify.Notifier) *Dispatcher { +func NewDispatcher(ap provider.Alerts, r *Route, n notify.Notifier) *Dispatcher { disp := &Dispatcher{ alerts: ap, notifier: n, - routes: NewRoutes(r, nil), + route: r, log: log.With("component", "dispatcher"), } return disp @@ -72,7 +71,7 @@ func (d *Dispatcher) run(it provider.AlertIterator) { continue } - for _, r := range d.routes.Match(alert.Labels) { + for _, r := range d.route.Match(alert.Labels) { d.processAlert(alert, r) } diff --git a/main.go b/main.go index 581ab6b0..58fe9207 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "os/signal" "path/filepath" "syscall" + "time" "github.com/prometheus/common/log" "github.com/prometheus/common/route" @@ -63,6 +64,7 @@ func main() { inhibitor *Inhibitor tmpl *template.Template disp *Dispatcher + routes *Route ) defer disp.Stop() @@ -114,7 +116,13 @@ func main() { disp.Stop() inhibitor = NewInhibitor(alerts, conf.InhibitRules) - disp = NewDispatcher(alerts, conf.Routes, build(conf.NotificationConfigs)) + routes = NewRoute(&config.Route{Routes: conf.Routes}, &RouteOpts{ + GroupWait: time.Duration(conf.Global.GroupWait), + GroupInterval: time.Duration(conf.Global.GroupInterval), + RepeatInterval: time.Duration(conf.Global.RepeatInterval), + SendResolved: conf.Global.SendResolved, + }) + disp = NewDispatcher(alerts, routes, build(conf.NotificationConfigs)) go disp.Run() diff --git a/route.go b/route.go index a573130d..8e18934a 100644 --- a/route.go +++ b/route.go @@ -23,23 +23,6 @@ import ( "github.com/prometheus/alertmanager/types" ) -var DefaultRouteOpts = RouteOpts{ - GroupWait: 20 * time.Second, - GroupInterval: 5 * time.Minute, - RepeatInterval: 1 * time.Hour, - SendResolved: true, -} - -type Routes []*Route - -func (rs Routes) Match(lset model.LabelSet) []*RouteOpts { - fakeParent := &Route{ - Routes: rs, - RouteOpts: DefaultRouteOpts, - } - return fakeParent.Match(lset) -} - // A Route is a node that contains definitions of how to handle alerts. type Route struct { // The configuration parameters for matches of this route. @@ -53,7 +36,7 @@ type Route struct { Continue bool // Children routes of this route. - Routes Routes + Routes []*Route } func NewRoute(cr *config.Route, parent *RouteOpts) *Route { @@ -107,11 +90,8 @@ func NewRoute(cr *config.Route, parent *RouteOpts) *Route { return route } -func NewRoutes(croutes []*config.Route, parent *RouteOpts) Routes { - if parent == nil { - parent = &DefaultRouteOpts - } - res := Routes{} +func NewRoutes(croutes []*config.Route, parent *RouteOpts) []*Route { + res := []*Route{} for _, cr := range croutes { res = append(res, NewRoute(cr, parent)) } diff --git a/route_test.go b/route_test.go index 09ec125c..524a2e67 100644 --- a/route_test.go +++ b/route_test.go @@ -72,8 +72,13 @@ routes: t.Fatal(err) } var ( - def = DefaultRouteOpts - tree = NewRoute(&ctree, &def) + def = &RouteOpts{ + GroupWait: 20 * time.Second, + GroupInterval: 5 * time.Minute, + RepeatInterval: 1 * time.Hour, + SendResolved: true, + } + tree = NewRoute(&ctree, def) ) lset := func(labels ...string) map[model.LabelName]struct{} { s := map[model.LabelName]struct{}{}