Implement global routing options

This commit is contained in:
Fabian Reinartz 2015-10-19 16:17:15 +02:00
parent a515b22325
commit ef0ee97781
5 changed files with 34 additions and 31 deletions

View File

@ -19,6 +19,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -155,10 +156,20 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
} }
var DefaultGlobalConfig = GlobalConfig{ 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", PagerdutyURL: "https://events.pagerduty.com/generic/2010-04-15/create_event.json",
} }
type GlobalConfig struct { 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"` Smarthost string `yaml:"smarthost"`
SlackURL string `yaml:"slack_url"` SlackURL string `yaml:"slack_url"`
PagerdutyURL string `yaml:"pagerduty_url"` PagerdutyURL string `yaml:"pagerduty_url"`

View File

@ -9,7 +9,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify" "github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/provider" "github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/types" "github.com/prometheus/alertmanager/types"
@ -20,7 +19,7 @@ const ResolveTimeout = 5 * time.Minute
// Dispatcher sorts incoming alerts into aggregation groups and // Dispatcher sorts incoming alerts into aggregation groups and
// assigns the correct notifiers to each. // assigns the correct notifiers to each.
type Dispatcher struct { type Dispatcher struct {
routes Routes route *Route
alerts provider.Alerts alerts provider.Alerts
notifier notify.Notifier notifier notify.Notifier
@ -34,11 +33,11 @@ type Dispatcher struct {
} }
// NewDispatcher returns a new Dispatcher. // 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{ disp := &Dispatcher{
alerts: ap, alerts: ap,
notifier: n, notifier: n,
routes: NewRoutes(r, nil), route: r,
log: log.With("component", "dispatcher"), log: log.With("component", "dispatcher"),
} }
return disp return disp
@ -72,7 +71,7 @@ func (d *Dispatcher) run(it provider.AlertIterator) {
continue continue
} }
for _, r := range d.routes.Match(alert.Labels) { for _, r := range d.route.Match(alert.Labels) {
d.processAlert(alert, r) d.processAlert(alert, r)
} }

10
main.go
View File

@ -21,6 +21,7 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"syscall" "syscall"
"time"
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
"github.com/prometheus/common/route" "github.com/prometheus/common/route"
@ -63,6 +64,7 @@ func main() {
inhibitor *Inhibitor inhibitor *Inhibitor
tmpl *template.Template tmpl *template.Template
disp *Dispatcher disp *Dispatcher
routes *Route
) )
defer disp.Stop() defer disp.Stop()
@ -114,7 +116,13 @@ func main() {
disp.Stop() disp.Stop()
inhibitor = NewInhibitor(alerts, conf.InhibitRules) 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() go disp.Run()

View File

@ -23,23 +23,6 @@ import (
"github.com/prometheus/alertmanager/types" "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. // A Route is a node that contains definitions of how to handle alerts.
type Route struct { type Route struct {
// The configuration parameters for matches of this route. // The configuration parameters for matches of this route.
@ -53,7 +36,7 @@ type Route struct {
Continue bool Continue bool
// Children routes of this route. // Children routes of this route.
Routes Routes Routes []*Route
} }
func NewRoute(cr *config.Route, parent *RouteOpts) *Route { func NewRoute(cr *config.Route, parent *RouteOpts) *Route {
@ -107,11 +90,8 @@ func NewRoute(cr *config.Route, parent *RouteOpts) *Route {
return route return route
} }
func NewRoutes(croutes []*config.Route, parent *RouteOpts) Routes { func NewRoutes(croutes []*config.Route, parent *RouteOpts) []*Route {
if parent == nil { res := []*Route{}
parent = &DefaultRouteOpts
}
res := Routes{}
for _, cr := range croutes { for _, cr := range croutes {
res = append(res, NewRoute(cr, parent)) res = append(res, NewRoute(cr, parent))
} }

View File

@ -72,8 +72,13 @@ routes:
t.Fatal(err) t.Fatal(err)
} }
var ( var (
def = DefaultRouteOpts def = &RouteOpts{
tree = NewRoute(&ctree, &def) 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{} { lset := func(labels ...string) map[model.LabelName]struct{} {
s := map[model.LabelName]struct{}{} s := map[model.LabelName]struct{}{}