config: deprecate `target_groups` for `static_configs`

This change deprecates the `target_groups` option in favor
of `static_configs`. The old configuration is still accepted
but prints a warning.
Configuration loading errors if both options are set.
This commit is contained in:
Fabian Reinartz 2016-06-08 15:54:04 +02:00
parent ad18ac51b8
commit 0f21bd31ca
6 changed files with 20 additions and 9 deletions

View File

@ -23,6 +23,7 @@ import (
"strings"
"time"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v2"
)
@ -399,7 +400,9 @@ type ScrapeConfig struct {
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
// List of labeled target groups for this job.
TargetGroups []*TargetGroup `yaml:"target_groups,omitempty"`
// XXX(fabxc): `target_groups` is deprecated.
TargetGroups []*TargetGroup `yaml:"target_groups,omitempty"`
StaticConfigs []*TargetGroup `yaml:"static_configs,omitempty"`
// List of DNS service discovery configurations.
DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"`
// List of file service discovery configurations.
@ -445,9 +448,17 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
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")
}
// Check `target_groups` deprecation.
if c.TargetGroups != nil && c.StaticConfigs != nil {
return fmt.Errorf("'target_groups' is deprecated, configure static targets via 'static_configs' only")
}
if c.TargetGroups != nil {
log.Warnf("The 'target_groups' option for scrape configurations is deprecated, use 'static_configs' instead")
c.StaticConfigs = c.TargetGroups
}
// Check for users putting URLs in target groups.
if len(c.RelabelConfigs) == 0 {
for _, tg := range c.TargetGroups {
for _, tg := range c.StaticConfigs {
for _, t := range tg.Targets {
if err = CheckTargetAddress(t[model.AddressLabel]); err != nil {
return err

View File

@ -57,7 +57,7 @@ var expectedConf = &Config{
BearerTokenFile: "testdata/valid_token_file",
TargetGroups: []*TargetGroup{
StaticConfigs: []*TargetGroup{
{
Targets: []model.LabelSet{
{model.AddressLabel: "localhost:9090"},
@ -409,8 +409,8 @@ func TestBadConfigs(t *testing.T) {
}
}
func TestBadTargetGroup(t *testing.T) {
content, err := ioutil.ReadFile("testdata/tgroup.bad.json")
func TestBadStaticConfigs(t *testing.T) {
content, err := ioutil.ReadFile("testdata/static_config.bad.json")
if err != nil {
t.Fatal(err)
}

View File

@ -32,7 +32,7 @@ scrape_configs:
- files:
- bar/*.yaml
target_groups:
static_configs:
- targets: ['localhost:9090', 'localhost:9191']
labels:
my: label

View File

@ -1,5 +1,5 @@
scrape_configs:
- job_name: prometheus
target_groups:
static_configs:
- targets:
- http://bad

View File

@ -402,8 +402,8 @@ func providersFromConfig(cfg *config.ScrapeConfig) map[string]TargetProvider {
for i, c := range cfg.AzureSDConfigs {
app("azure", i, discovery.NewAzureDiscovery(c))
}
if len(cfg.TargetGroups) > 0 {
app("static", 0, NewStaticProvider(cfg.TargetGroups))
if len(cfg.StaticConfigs) > 0 {
app("static", 0, NewStaticProvider(cfg.StaticConfigs))
}
return providers