Move time intervals to own section, add config validation
Signed-off-by: Ben Ridley <benridley29@gmail.com>
This commit is contained in:
parent
ea5b925147
commit
fe4b8399c3
|
@ -25,6 +25,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/benridley/gotime"
|
||||
"github.com/pkg/errors"
|
||||
commoncfg "github.com/prometheus/common/config"
|
||||
"github.com/prometheus/common/model"
|
||||
|
@ -222,16 +223,7 @@ func resolveFilepaths(baseDir string, cfg *Config) {
|
|||
// A MuteTimeInterval represents a named set of time intervals for which a route should be muted.
|
||||
type MuteTimeInterval struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
TimeIntervals []TimeInterval `yaml:"time_intervals"`
|
||||
}
|
||||
|
||||
// A TimeInterval describes intervals of time.
|
||||
type TimeInterval struct {
|
||||
Times []string `yaml:"times"`
|
||||
Weekdays []string `yaml:"weekdays"`
|
||||
DaysOfMonth []string `yaml:"days_of_month"`
|
||||
Months []string `yaml:"months"`
|
||||
Years []string `yaml:"years"`
|
||||
TimeIntervals []gotime.TimeInterval `yaml:"time_intervals"`
|
||||
}
|
||||
|
||||
// Config is the top-level configuration for Alertmanager's config files.
|
||||
|
@ -241,6 +233,7 @@ type Config struct {
|
|||
InhibitRules []*InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"`
|
||||
Receivers []*Receiver `yaml:"receivers,omitempty" json:"receivers,omitempty"`
|
||||
Templates []string `yaml:"templates" json:"templates"`
|
||||
MuteTimeIntervals []MuteTimeInterval `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"`
|
||||
|
||||
// original is the input from which the config was parsed.
|
||||
original string
|
||||
|
@ -431,7 +424,15 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||
}
|
||||
|
||||
// Validate that all receivers used in the routing tree are defined.
|
||||
return checkReceiver(c.Route, names)
|
||||
if err := checkReceiver(c.Route, names); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tiNames := make(map[string]struct{})
|
||||
for _, mt := range c.MuteTimeIntervals {
|
||||
tiNames[mt.Name] = struct{}{}
|
||||
}
|
||||
return checkTimeInterval(c.Route, tiNames)
|
||||
}
|
||||
|
||||
// checkReceiver returns an error if a node in the routing tree
|
||||
|
@ -451,12 +452,28 @@ func checkReceiver(r *Route, receivers map[string]struct{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func checkTimeInterval(r *Route, timeIntervals map[string]struct{}) error {
|
||||
for _, sr := range r.Routes {
|
||||
if err := checkTimeInterval(sr, timeIntervals); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(r.MuteTimes) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, mt := range r.MuteTimes {
|
||||
if _, ok := timeIntervals[mt]; !ok {
|
||||
return fmt.Errorf("undefined time interval %s used in route", mt)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultGlobalConfig returns GlobalConfig with default values.
|
||||
func DefaultGlobalConfig() GlobalConfig {
|
||||
return GlobalConfig{
|
||||
ResolveTimeout: model.Duration(5 * time.Minute),
|
||||
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
||||
MuteTimeIntervals: []MuteTimeInterval{},
|
||||
SMTPHello: "localhost",
|
||||
SMTPRequireTLS: true,
|
||||
PagerdutyURL: mustParseURL("https://events.pagerduty.com/v2/enqueue"),
|
||||
|
@ -573,7 +590,6 @@ type GlobalConfig struct {
|
|||
SMTPAuthIdentity string `yaml:"smtp_auth_identity,omitempty" json:"smtp_auth_identity,omitempty"`
|
||||
SMTPRequireTLS bool `yaml:"smtp_require_tls" json:"smtp_require_tls,omitempty"`
|
||||
SlackAPIURL *SecretURL `yaml:"slack_api_url,omitempty" json:"slack_api_url,omitempty"`
|
||||
MuteTimeIntervals []MuteTimeInterval `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"`
|
||||
PagerdutyURL *URL `yaml:"pagerduty_url,omitempty" json:"pagerduty_url,omitempty"`
|
||||
OpsGenieAPIURL *URL `yaml:"opsgenie_api_url,omitempty" json:"opsgenie_api_url,omitempty"`
|
||||
OpsGenieAPIKey Secret `yaml:"opsgenie_api_key,omitempty" json:"opsgenie_api_key,omitempty"`
|
||||
|
|
Loading…
Reference in New Issue