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"
"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"`

View File

@ -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)
}

10
main.go
View File

@ -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()

View File

@ -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))
}

View File

@ -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{}{}