config: rename AlertingConfig, resolve file paths

pull/2215/head
Fabian Reinartz 8 years ago
parent d4deb8bbf2
commit 3fb4d1191b

@ -268,7 +268,7 @@ func reloadConfig(filename string, rls ...Reloadable) (err error) {
if err != nil { if err != nil {
return err return err
} }
acfg := &config.AlertmanagersConfig{ acfg := &config.AlertmanagerConfig{
Scheme: u.Scheme, Scheme: u.Scheme,
PathPrefix: u.Path, PathPrefix: u.Path,
Timeout: cfg.notifierTimeout, Timeout: cfg.notifierTimeout,
@ -284,7 +284,7 @@ func reloadConfig(filename string, rls ...Reloadable) (err error) {
}, },
}, },
} }
conf.AlertingConfig.AlertmanagersConfigs = append(conf.AlertingConfig.AlertmanagersConfigs, acfg) conf.AlertingConfig.AlertmanagerConfigs = append(conf.AlertingConfig.AlertmanagerConfigs, acfg)
} }
failed := false failed := false

@ -88,7 +88,7 @@ var (
} }
// DefaultAlertmanagersConfig is the default alertmanager configuration. // DefaultAlertmanagersConfig is the default alertmanager configuration.
DefaultAlertmanagersConfig = AlertmanagersConfig{ DefaultAlertmanagersConfig = AlertmanagerConfig{
Scheme: "http", Scheme: "http",
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
} }
@ -220,27 +220,33 @@ func resolveFilepaths(baseDir string, cfg *Config) {
cfg.RuleFiles[i] = join(rf) cfg.RuleFiles[i] = join(rf)
} }
for _, cfg := range cfg.ScrapeConfigs { clientPaths := func(scfg *HTTPClientConfig) {
scfg := &cfg.HTTPClientConfig
scfg.BearerTokenFile = join(scfg.BearerTokenFile) scfg.BearerTokenFile = join(scfg.BearerTokenFile)
scfg.TLSConfig.CAFile = join(scfg.TLSConfig.CAFile) scfg.TLSConfig.CAFile = join(scfg.TLSConfig.CAFile)
scfg.TLSConfig.CertFile = join(scfg.TLSConfig.CertFile) scfg.TLSConfig.CertFile = join(scfg.TLSConfig.CertFile)
scfg.TLSConfig.KeyFile = join(scfg.TLSConfig.KeyFile) scfg.TLSConfig.KeyFile = join(scfg.TLSConfig.KeyFile)
}
for _, kcfg := range cfg.ServiceDiscoveryConfig.KubernetesSDConfigs { sdPaths := func(cfg *ServiceDiscoveryConfig) {
for _, kcfg := range cfg.KubernetesSDConfigs {
kcfg.BearerTokenFile = join(kcfg.BearerTokenFile) kcfg.BearerTokenFile = join(kcfg.BearerTokenFile)
kcfg.TLSConfig.CAFile = join(kcfg.TLSConfig.CAFile) kcfg.TLSConfig.CAFile = join(kcfg.TLSConfig.CAFile)
kcfg.TLSConfig.CertFile = join(kcfg.TLSConfig.CertFile) kcfg.TLSConfig.CertFile = join(kcfg.TLSConfig.CertFile)
kcfg.TLSConfig.KeyFile = join(kcfg.TLSConfig.KeyFile) kcfg.TLSConfig.KeyFile = join(kcfg.TLSConfig.KeyFile)
} }
for _, mcfg := range cfg.MarathonSDConfigs {
for _, mcfg := range cfg.ServiceDiscoveryConfig.MarathonSDConfigs {
mcfg.TLSConfig.CAFile = join(mcfg.TLSConfig.CAFile) mcfg.TLSConfig.CAFile = join(mcfg.TLSConfig.CAFile)
mcfg.TLSConfig.CertFile = join(mcfg.TLSConfig.CertFile) mcfg.TLSConfig.CertFile = join(mcfg.TLSConfig.CertFile)
mcfg.TLSConfig.KeyFile = join(mcfg.TLSConfig.KeyFile) mcfg.TLSConfig.KeyFile = join(mcfg.TLSConfig.KeyFile)
} }
}
for _, cfg := range cfg.ScrapeConfigs {
clientPaths(&cfg.HTTPClientConfig)
sdPaths(&cfg.ServiceDiscoveryConfig)
}
for _, cfg := range cfg.AlertingConfig.AlertmanagerConfigs {
clientPaths(&cfg.HTTPClientConfig)
sdPaths(&cfg.ServiceDiscoveryConfig)
} }
} }
@ -442,7 +448,7 @@ func (c *ServiceDiscoveryConfig) UnmarshalYAML(unmarshal func(interface{}) error
if err := unmarshal((*plain)(c)); err != nil { if err := unmarshal((*plain)(c)); err != nil {
return err return err
} }
if err := checkOverflow(c.XXX, "TLS config"); err != nil { if err := checkOverflow(c.XXX, "service discovery config"); err != nil {
return err return err
} }
return nil return nil
@ -465,6 +471,16 @@ type HTTPClientConfig struct {
XXX map[string]interface{} `yaml:",inline"` XXX map[string]interface{} `yaml:",inline"`
} }
func (c *HTTPClientConfig) validate() error {
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
}
if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured")
}
return nil
}
// ScrapeConfig configures a scraping unit for Prometheus. // ScrapeConfig configures a scraping unit for Prometheus.
type ScrapeConfig struct { type ScrapeConfig struct {
// The job name to which the job label is set by default. // The job name to which the job label is set by default.
@ -511,14 +527,12 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if len(c.JobName) == 0 { if len(c.JobName) == 0 {
return fmt.Errorf("job_name is empty") return fmt.Errorf("job_name is empty")
} }
// The UnmarshalYAML method of HTTPClientConfig is not being called because it's not a pointer. // The UnmarshalYAML method of HTTPClientConfig is not being called because it's not a pointer.
// We cannot make it a pointer as the parser panics for inlined pointer structs. // We cannot make it a pointer as the parser panics for inlined pointer structs.
// Thus we just do its validation here. // Thus we just do its validation here.
if len(c.HTTPClientConfig.BearerToken) > 0 && len(c.HTTPClientConfig.BearerTokenFile) > 0 { if err := c.HTTPClientConfig.validate(); err != nil {
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured") return err
}
if c.HTTPClientConfig.BasicAuth != nil && (len(c.HTTPClientConfig.BearerToken) > 0 || len(c.HTTPClientConfig.BearerTokenFile) > 0) {
return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured")
} }
// Check for users putting URLs in target groups. // Check for users putting URLs in target groups.
@ -536,8 +550,8 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// AlertingConfig configures alerting and alertmanager related configs // AlertingConfig configures alerting and alertmanager related configs
type AlertingConfig struct { type AlertingConfig struct {
AlertRelabelConfigs []*RelabelConfig `yaml:"alert_relabel_configs,omitempty"` AlertRelabelConfigs []*RelabelConfig `yaml:"alert_relabel_configs,omitempty"`
AlertmanagersConfigs []*AlertmanagersConfig `yaml:"alertmanagers,omitempty"` AlertmanagerConfigs []*AlertmanagerConfig `yaml:"alertmanagers,omitempty"`
// Catches all undefined fields and must be empty after parsing. // Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"` XXX map[string]interface{} `yaml:",inline"`
@ -559,7 +573,7 @@ func (c *AlertingConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
} }
// AlertmanagersConfig configures how Alertmanagers can be discovered and communicated with. // AlertmanagersConfig configures how Alertmanagers can be discovered and communicated with.
type AlertmanagersConfig struct { type AlertmanagerConfig struct {
// We cannot do proper Go type embedding below as the parser will then parse // We cannot do proper Go type embedding below as the parser will then parse
// values arbitrarily into the overflow maps of further-down types. // values arbitrarily into the overflow maps of further-down types.
@ -581,23 +595,21 @@ type AlertmanagersConfig struct {
} }
// UnmarshalYAML implements the yaml.Unmarshaler interface. // UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *AlertmanagersConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { func (c *AlertmanagerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultAlertmanagersConfig *c = DefaultAlertmanagersConfig
type plain AlertmanagersConfig type plain AlertmanagerConfig
if err := unmarshal((*plain)(c)); err != nil { if err := unmarshal((*plain)(c)); err != nil {
return err return err
} }
if err := checkOverflow(c.XXX, "alertmanager config"); err != nil { if err := checkOverflow(c.XXX, "alertmanager config"); err != nil {
return err return err
} }
// The UnmarshalYAML method of HTTPClientConfig is not being called because it's not a pointer. // The UnmarshalYAML method of HTTPClientConfig is not being called because it's not a pointer.
// We cannot make it a pointer as the parser panics for inlined pointer structs. // We cannot make it a pointer as the parser panics for inlined pointer structs.
// Thus we just do its validation here. // Thus we just do its validation here.
if len(c.HTTPClientConfig.BearerToken) > 0 && len(c.HTTPClientConfig.BearerTokenFile) > 0 { if err := c.HTTPClientConfig.validate(); err != nil {
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured") return err
}
if c.HTTPClientConfig.BasicAuth != nil && (len(c.HTTPClientConfig.BearerToken) > 0 || len(c.HTTPClientConfig.BearerTokenFile) > 0) {
return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured")
} }
// Check for users putting URLs in target groups. // Check for users putting URLs in target groups.

@ -403,7 +403,7 @@ var expectedConf = &Config{
}, },
}, },
AlertingConfig: AlertingConfig{ AlertingConfig: AlertingConfig{
AlertmanagersConfigs: []*AlertmanagersConfig{ AlertmanagerConfigs: []*AlertmanagerConfig{
{ {
Scheme: "https", Scheme: "https",
Timeout: 10 * time.Second, Timeout: 10 * time.Second,

@ -149,7 +149,7 @@ func (n *Notifier) ApplyConfig(conf *config.Config) error {
amSets := []*alertmanagerSet{} amSets := []*alertmanagerSet{}
ctx, cancel := context.WithCancel(n.ctx) ctx, cancel := context.WithCancel(n.ctx)
for _, cfg := range conf.AlertingConfig.AlertmanagersConfigs { for _, cfg := range conf.AlertingConfig.AlertmanagerConfigs {
ams, err := newAlertmanagerSet(cfg) ams, err := newAlertmanagerSet(cfg)
if err != nil { if err != nil {
return err return err
@ -300,8 +300,8 @@ func (n *Notifier) Alertmanagers() []string {
return res return res
} }
// sendAll sends the alerts to all configured Alertmanagers at concurrently. // sendAll sends the alerts to all configured Alertmanagers concurrently.
// It returns the number of sends that have failed and true if all failed. // It returns true if the alerts could be sent successfully to at least on Alertmanager.
func (n *Notifier) sendAll(alerts ...*model.Alert) bool { func (n *Notifier) sendAll(alerts ...*model.Alert) bool {
begin := time.Now() begin := time.Now()
@ -380,8 +380,7 @@ func (n *Notifier) Collect(ch chan<- prometheus.Metric) {
ch <- n.queueCapacity ch <- n.queueCapacity
} }
// alertmanager holds all necessary information to send alerts // alertmanager holds Alertmanager endpoint information.
// to an Alertmanager endpoint.
type alertmanager struct { type alertmanager struct {
plainURL string // test injection hook plainURL string // test injection hook
labels model.LabelSet labels model.LabelSet
@ -419,14 +418,14 @@ func (a alertmanager) send(ctx context.Context, c *http.Client, b []byte) error
// discovery definitions that have a common configuration on how alerts should be sent. // discovery definitions that have a common configuration on how alerts should be sent.
type alertmanagerSet struct { type alertmanagerSet struct {
ts *discovery.TargetSet ts *discovery.TargetSet
cfg *config.AlertmanagersConfig cfg *config.AlertmanagerConfig
client *http.Client client *http.Client
mtx sync.RWMutex mtx sync.RWMutex
ams []alertmanager ams []alertmanager
} }
func newAlertmanagerSet(cfg *config.AlertmanagersConfig) (*alertmanagerSet, error) { func newAlertmanagerSet(cfg *config.AlertmanagerConfig) (*alertmanagerSet, error) {
client, err := retrieval.NewHTTPClient(cfg.HTTPClientConfig) client, err := retrieval.NewHTTPClient(cfg.HTTPClientConfig)
if err != nil { if err != nil {
return nil, err return nil, err
@ -477,7 +476,7 @@ func postPath(pre string) string {
// alertmanagersFromGroup extracts a list of alertmanagers from a target group and an associcated // alertmanagersFromGroup extracts a list of alertmanagers from a target group and an associcated
// AlertmanagerConfig. // AlertmanagerConfig.
func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagersConfig) ([]alertmanager, error) { func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagerConfig) ([]alertmanager, error) {
var res []alertmanager var res []alertmanager
for _, lset := range tg.Targets { for _, lset := range tg.Targets {

@ -153,7 +153,7 @@ func TestHandlerSendAll(t *testing.T) {
{plainURL: server1.URL}, {plainURL: server1.URL},
{plainURL: server2.URL}, {plainURL: server2.URL},
}, },
cfg: &config.AlertmanagersConfig{ cfg: &config.AlertmanagerConfig{
Timeout: time.Second, Timeout: time.Second,
}, },
}) })
@ -314,7 +314,7 @@ func TestHandlerQueueing(t *testing.T) {
ams: []alertmanager{ ams: []alertmanager{
{plainURL: server.URL}, {plainURL: server.URL},
}, },
cfg: &config.AlertmanagersConfig{ cfg: &config.AlertmanagerConfig{
Timeout: time.Second, Timeout: time.Second,
}, },
}) })

Loading…
Cancel
Save