diff --git a/config/config.go b/config/config.go index 86ce13db6c..c3cec1aefd 100644 --- a/config/config.go +++ b/config/config.go @@ -89,7 +89,8 @@ var ( // DefaultAlertmanagersConfig is the default alertmanager configuration. DefaultAlertmanagersConfig = AlertmanagersConfig{ - Scheme: "http", + Scheme: "http", + Timeout: 10 * time.Second, } // DefaultRelabelConfig is the default Relabel configuration. @@ -535,7 +536,8 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { // AlertingConfig configures alerting and alertmanager related configs type AlertingConfig struct { - AlertRelabelConfigs []*RelabelConfig `yaml:"alert_relabel_configs,omitempty"` + AlertRelabelConfigs []*RelabelConfig `yaml:"alert_relabel_configs,omitempty"` + AlertmanagersConfigs []*AlertmanagersConfig `yaml:"alertmanagers,omitempty"` // Catches all undefined fields and must be empty after parsing. XXX map[string]interface{} `yaml:",inline"` @@ -556,6 +558,61 @@ func (c *AlertingConfig) UnmarshalYAML(unmarshal func(interface{}) error) error return nil } +// AlertmanagersConfig configures how Alertmanagers can be discovered and communicated with. +type AlertmanagersConfig struct { + // We cannot do proper Go type embedding below as the parser will then parse + // values arbitrarily into the overflow maps of further-down types. + + ServiceDiscoveryConfig ServiceDiscoveryConfig `yaml:",inline"` + HTTPClientConfig HTTPClientConfig `yaml:",inline"` + + // The URL scheme to use when talking to Alertmanagers. + Scheme string `yaml:"scheme,omitempty"` + // Path prefix to add in front of the push endpoint path. + PathPrefix string `yaml:"path_prefix,omitempty"` + // The timeout used when sending alerts. + Timeout time.Duration `yaml:"timeout,omitempty"` + + // List of Alertmanager relabel configurations. + RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"` + + // Catches all undefined fields and must be empty after parsing. + XXX map[string]interface{} `yaml:",inline"` +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (c *AlertmanagersConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + *c = DefaultAlertmanagersConfig + type plain AlertmanagersConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + if err := checkOverflow(c.XXX, "alertmanager config"); err != nil { + return err + } + // 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. + // Thus we just do its validation here. + if len(c.HTTPClientConfig.BearerToken) > 0 && len(c.HTTPClientConfig.BearerTokenFile) > 0 { + return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured") + } + 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. + if len(c.RelabelConfigs) == 0 { + for _, tg := range c.ServiceDiscoveryConfig.StaticConfigs { + for _, t := range tg.Targets { + if err := CheckTargetAddress(t[model.AddressLabel]); err != nil { + return err + } + } + } + } + return nil +} + // CheckTargetAddress checks if target address is valid. func CheckTargetAddress(address model.LabelValue) error { // For now check for a URL, we may want to expand this later. diff --git a/config/config_test.go b/config/config_test.go index 0a2ba5c0bb..9730954108 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -402,6 +402,25 @@ var expectedConf = &Config{ }, }, }, + AlertingConfig: AlertingConfig{ + AlertmanagersConfigs: []*AlertmanagersConfig{ + { + Scheme: "https", + Timeout: 10 * time.Second, + ServiceDiscoveryConfig: ServiceDiscoveryConfig{ + StaticConfigs: []*TargetGroup{ + { + Targets: []model.LabelSet{ + {model.AddressLabel: "1.2.3.4:9093"}, + {model.AddressLabel: "1.2.3.5:9093"}, + {model.AddressLabel: "1.2.3.6:9093"}, + }, + }, + }, + }, + }, + }, + }, original: "", } diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index bad5e1e6f7..65c2086d33 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -174,3 +174,12 @@ scrape_configs: static_configs: - targets: - localhost:9090 + +alerting: + alertmanagers: + - scheme: https + static_configs: + - targets: + - "1.2.3.4:9093" + - "1.2.3.5:9093" + - "1.2.3.6:9093"