Browse Source

Merge pull request #1057 from prometheus/config-target

Allow all instance labels in target groups
pull/1204/head
Brian Brazil 9 years ago
parent
commit
452220ec90
  1. 22
      config/config.go
  2. 3
      config/config_test.go
  3. 5
      config/testdata/url_in_targetgroup.bad.yml
  4. 3
      retrieval/targetmanager.go
  5. 22
      retrieval/targetmanager_test.go

22
config/config.go

@ -398,9 +398,28 @@ 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 for users putting URLs in target groups.
if len(c.RelabelConfigs) == 0 {
for _, tg := range c.TargetGroups {
for _, t := range tg.Targets {
if err = CheckTargetAddress(t[model.AddressLabel]); err != nil {
return err
}
}
}
}
return checkOverflow(c.XXX, "scrape_config")
}
// 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.
if strings.Contains(string(address), "/") {
return fmt.Errorf("%q is not a valid hostname", address)
}
return nil
}
// BasicAuth contains basic HTTP authentication credentials.
type BasicAuth struct {
Username string `yaml:"username"`
@ -457,9 +476,6 @@ func (tg *TargetGroup) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
tg.Targets = make([]model.LabelSet, 0, len(g.Targets))
for _, t := range g.Targets {
if strings.Contains(t, "/") {
return fmt.Errorf("%q is not a valid hostname", t)
}
tg.Targets = append(tg.Targets, model.LabelSet{
model.AddressLabel: model.LabelValue(t),
})

3
config/config_test.go

@ -335,6 +335,9 @@ var expectedErrors = []struct {
}, {
filename: "marathon_no_servers.bad.yml",
errMsg: "Marathon SD config must contain at least one Marathon server",
}, {
filename: "url_in_targetgroup.bad.yml",
errMsg: "\"http://bad\" is not a valid hostname",
},
}

5
config/testdata/url_in_targetgroup.bad.yml vendored

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

3
retrieval/targetmanager.go

@ -496,6 +496,9 @@ func (tm *TargetManager) targetsFromGroup(tg *config.TargetGroup, cfg *config.Sc
if labels == nil {
continue
}
if err = config.CheckTargetAddress(labels[model.AddressLabel]); err != nil {
return nil, err
}
for ln := range labels {
// Meta labels are deleted after relabelling. Other internal labels propagate to

22
retrieval/targetmanager_test.go

@ -278,6 +278,25 @@ func TestTargetManagerConfigUpdate(t *testing.T) {
},
},
}
// Test that targets without host:port addresses are dropped.
testJob3 := &config.ScrapeConfig{
JobName: "test_job1",
ScrapeInterval: config.Duration(1 * time.Minute),
TargetGroups: []*config.TargetGroup{{
Targets: []model.LabelSet{
{model.AddressLabel: "example.net:80"},
},
}},
RelabelConfigs: []*config.RelabelConfig{
{
SourceLabels: model.LabelNames{model.AddressLabel},
Regex: config.MustNewRegexp("(.*)"),
TargetLabel: "__address__",
Replacement: "http://$1",
Action: config.RelabelReplace,
},
},
}
sequence := []struct {
scrapeConfigs []*config.ScrapeConfig
@ -348,6 +367,9 @@ func TestTargetManagerConfigUpdate(t *testing.T) {
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "foo.com:1235"},
},
},
}, {
scrapeConfigs: []*config.ScrapeConfig{testJob3},
expected: map[string][]model.LabelSet{},
},
}
conf := &config.Config{}

Loading…
Cancel
Save