2013-02-14 00:04:07 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-06-06 07:55:22 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-05-07 08:55:03 +00:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2013-02-14 00:04:07 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-05-07 08:55:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-02-14 00:04:07 +00:00
|
|
|
)
|
|
|
|
|
2015-06-04 15:03:12 +00:00
|
|
|
var expectedConf = &Config{
|
2015-06-07 15:40:22 +00:00
|
|
|
GlobalConfig: GlobalConfig{
|
2015-05-07 08:55:03 +00:00
|
|
|
ScrapeInterval: Duration(15 * time.Second),
|
|
|
|
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
|
|
|
|
EvaluationInterval: Duration(30 * time.Second),
|
|
|
|
|
|
|
|
Labels: clientmodel.LabelSet{
|
|
|
|
"monitor": "codelab",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
|
|
|
|
RuleFiles: []string{
|
2015-08-05 16:04:34 +00:00
|
|
|
"testdata/first.rules",
|
|
|
|
"/absolute/second.rules",
|
|
|
|
"testdata/my/*.rules",
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ScrapeConfigs: []*ScrapeConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
JobName: "prometheus",
|
|
|
|
|
2015-06-22 20:35:19 +00:00
|
|
|
HonorLabels: true,
|
2015-05-07 08:55:03 +00:00
|
|
|
ScrapeInterval: Duration(15 * time.Second),
|
|
|
|
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
|
|
|
|
|
|
|
|
MetricsPath: DefaultScrapeConfig.MetricsPath,
|
|
|
|
Scheme: DefaultScrapeConfig.Scheme,
|
|
|
|
|
2015-08-05 16:04:34 +00:00
|
|
|
BearerTokenFile: "testdata/valid_token_file",
|
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
TargetGroups: []*TargetGroup{
|
|
|
|
{
|
|
|
|
Targets: []clientmodel.LabelSet{
|
|
|
|
{clientmodel.AddressLabel: "localhost:9090"},
|
|
|
|
{clientmodel.AddressLabel: "localhost:9191"},
|
|
|
|
},
|
|
|
|
Labels: clientmodel.LabelSet{
|
|
|
|
"my": "label",
|
|
|
|
"your": "label",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-05-13 09:28:04 +00:00
|
|
|
FileSDConfigs: []*FileSDConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-05-13 10:03:31 +00:00
|
|
|
Names: []string{"foo/*.slow.json", "foo/*.slow.yml", "single/file.yml"},
|
2015-05-13 09:28:04 +00:00
|
|
|
RefreshInterval: Duration(10 * time.Minute),
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
|
|
|
{
|
2015-05-13 09:28:04 +00:00
|
|
|
Names: []string{"bar/*.yaml"},
|
|
|
|
RefreshInterval: Duration(30 * time.Second),
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-05-13 09:28:04 +00:00
|
|
|
},
|
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
RelabelConfigs: []*RelabelConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"job", "__meta_dns_srv_name"},
|
|
|
|
TargetLabel: "job",
|
|
|
|
Separator: ";",
|
|
|
|
Regex: &Regexp{*regexp.MustCompile("(.*)some-[regex]$")},
|
|
|
|
Replacement: "foo-${1}",
|
|
|
|
Action: RelabelReplace,
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
JobName: "service-x",
|
|
|
|
|
|
|
|
ScrapeInterval: Duration(50 * time.Second),
|
|
|
|
ScrapeTimeout: Duration(5 * time.Second),
|
|
|
|
|
2015-05-15 10:47:50 +00:00
|
|
|
BasicAuth: &BasicAuth{
|
2015-07-06 12:25:20 +00:00
|
|
|
Username: "admin_name",
|
|
|
|
Password: "admin_password",
|
2015-05-15 10:47:50 +00:00
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
MetricsPath: "/my_path",
|
2015-05-15 10:47:50 +00:00
|
|
|
Scheme: "https",
|
2015-05-07 08:55:03 +00:00
|
|
|
|
2015-05-07 14:47:18 +00:00
|
|
|
DNSSDConfigs: []*DNSSDConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
Names: []string{
|
|
|
|
"first.dns.address.domain.com",
|
|
|
|
"second.dns.address.domain.com",
|
|
|
|
},
|
|
|
|
RefreshInterval: Duration(15 * time.Second),
|
2015-07-30 08:56:48 +00:00
|
|
|
Type: "SRV",
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
Names: []string{
|
|
|
|
"first.dns.address.domain.com",
|
|
|
|
},
|
|
|
|
RefreshInterval: Duration(30 * time.Second),
|
2015-07-30 08:56:48 +00:00
|
|
|
Type: "SRV",
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
RelabelConfigs: []*RelabelConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"job"},
|
|
|
|
Regex: &Regexp{*regexp.MustCompile("(.*)some-[regex]$")},
|
|
|
|
Separator: ";",
|
|
|
|
Action: RelabelDrop,
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-06-24 07:07:17 +00:00
|
|
|
{
|
|
|
|
SourceLabels: clientmodel.LabelNames{"__address__"},
|
|
|
|
TargetLabel: "__hash",
|
|
|
|
Modulus: 8,
|
|
|
|
Separator: ";",
|
|
|
|
Action: RelabelHashMod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceLabels: clientmodel.LabelNames{"__hash"},
|
|
|
|
Regex: &Regexp{*regexp.MustCompile("^1$")},
|
|
|
|
Separator: ";",
|
|
|
|
Action: RelabelKeep,
|
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-06-12 21:16:13 +00:00
|
|
|
MetricRelabelConfigs: []*RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: clientmodel.LabelNames{"__name__"},
|
|
|
|
Regex: &Regexp{*regexp.MustCompile("expensive_metric.*$")},
|
|
|
|
Separator: ";",
|
|
|
|
Action: RelabelDrop,
|
|
|
|
},
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2015-06-12 11:39:12 +00:00
|
|
|
{
|
|
|
|
JobName: "service-y",
|
|
|
|
|
|
|
|
ScrapeInterval: Duration(15 * time.Second),
|
|
|
|
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
|
|
|
|
|
|
|
|
MetricsPath: DefaultScrapeConfig.MetricsPath,
|
|
|
|
Scheme: DefaultScrapeConfig.Scheme,
|
|
|
|
|
|
|
|
ConsulSDConfigs: []*ConsulSDConfig{
|
|
|
|
{
|
|
|
|
Server: "localhost:1234",
|
|
|
|
Services: []string{"nginx", "cache", "mysql"},
|
|
|
|
TagSeparator: DefaultConsulSDConfig.TagSeparator,
|
|
|
|
Scheme: DefaultConsulSDConfig.Scheme,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-22 15:48:22 +00:00
|
|
|
{
|
|
|
|
JobName: "service-z",
|
|
|
|
|
|
|
|
ScrapeInterval: Duration(15 * time.Second),
|
|
|
|
ScrapeTimeout: Duration(10 * time.Second),
|
|
|
|
|
|
|
|
MetricsPath: "/metrics",
|
|
|
|
Scheme: "http",
|
|
|
|
|
|
|
|
ClientCert: &ClientCert{
|
2015-08-05 16:04:34 +00:00
|
|
|
Cert: "testdata/valid_cert_file",
|
|
|
|
Key: "testdata/valid_key_file",
|
2015-07-22 15:48:22 +00:00
|
|
|
},
|
|
|
|
BearerToken: "avalidtoken",
|
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
original: "",
|
|
|
|
}
|
2015-05-07 08:55:03 +00:00
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
2015-06-07 15:40:22 +00:00
|
|
|
// Parse a valid file that sets a global scrape timeout. This tests whether parsing
|
|
|
|
// an overwritten default field in the global config permanently changes the default.
|
2015-08-05 16:30:37 +00:00
|
|
|
if _, err := LoadFile("testdata/global_timeout.good.yml"); err != nil {
|
2015-06-07 15:40:22 +00:00
|
|
|
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err)
|
|
|
|
}
|
|
|
|
|
2015-08-05 16:30:37 +00:00
|
|
|
c, err := LoadFile("testdata/conf.good.yml")
|
2015-05-07 08:55:03 +00:00
|
|
|
if err != nil {
|
2015-06-12 11:39:59 +00:00
|
|
|
t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err)
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
bgot, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
2015-06-12 11:39:59 +00:00
|
|
|
t.Fatalf("%s", err)
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
bexp, err := yaml.Marshal(expectedConf)
|
|
|
|
if err != nil {
|
2015-06-12 11:39:59 +00:00
|
|
|
t.Fatalf("%s", err)
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
expectedConf.original = c.original
|
2013-02-14 00:04:07 +00:00
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
if !reflect.DeepEqual(c, expectedConf) {
|
2015-06-12 11:39:59 +00:00
|
|
|
t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", "testdata/conf.good.yml", bgot, bexp)
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
2015-07-06 12:25:20 +00:00
|
|
|
|
|
|
|
// String method must not reveal authentication credentials.
|
|
|
|
s := c.String()
|
|
|
|
if strings.Contains(s, "admin_name") || strings.Contains(s, "admin_password") {
|
|
|
|
t.Fatalf("config's String method reveals authentication credentials.")
|
|
|
|
}
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var expectedErrors = []struct {
|
|
|
|
filename string
|
|
|
|
errMsg string
|
2013-02-14 00:04:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
filename: "jobname.bad.yml",
|
|
|
|
errMsg: `"prom^etheus" is not a valid job name`,
|
2013-02-14 00:04:07 +00:00
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
filename: "jobname_dup.bad.yml",
|
|
|
|
errMsg: `found multiple scrape configs with job name "prometheus"`,
|
2013-06-11 20:59:27 +00:00
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
filename: "labelname.bad.yml",
|
|
|
|
errMsg: `"not$allowed" is not a valid label name`,
|
2015-06-06 07:55:22 +00:00
|
|
|
}, {
|
|
|
|
filename: "labelname2.bad.yml",
|
|
|
|
errMsg: `"not:allowed" is not a valid label name`,
|
2015-04-30 19:15:18 +00:00
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
filename: "regex.bad.yml",
|
|
|
|
errMsg: "error parsing regexp",
|
2015-06-10 21:40:39 +00:00
|
|
|
}, {
|
|
|
|
filename: "regex_missing.bad.yml",
|
|
|
|
errMsg: "relabel configuration requires a regular expression",
|
2015-06-24 07:07:17 +00:00
|
|
|
}, {
|
|
|
|
filename: "modulus_missing.bad.yml",
|
|
|
|
errMsg: "relabel configuration for hashmod requires non-zero modulus",
|
2015-05-28 05:36:21 +00:00
|
|
|
}, {
|
|
|
|
filename: "rules.bad.yml",
|
|
|
|
errMsg: "invalid rule file path",
|
2015-06-12 11:39:59 +00:00
|
|
|
}, {
|
|
|
|
filename: "unknown_attr.bad.yml",
|
|
|
|
errMsg: "unknown fields in scrape_config: consult_sd_configs",
|
2015-07-22 15:48:22 +00:00
|
|
|
}, {
|
|
|
|
filename: "bearertoken.bad.yml",
|
|
|
|
errMsg: "at most one of bearer_token & bearer_token_file must be configured",
|
|
|
|
}, {
|
|
|
|
filename: "bearertoken_basicauth.bad.yml",
|
|
|
|
errMsg: "at most one of basic_auth, bearer_token & bearer_token_file must be configured",
|
2013-12-03 10:59:38 +00:00
|
|
|
},
|
2013-02-14 00:04:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
func TestBadConfigs(t *testing.T) {
|
|
|
|
for _, ee := range expectedErrors {
|
2015-08-05 16:30:37 +00:00
|
|
|
_, err := LoadFile("testdata/" + ee.filename)
|
2015-05-07 08:55:03 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error parsing %s but got none", ee.filename)
|
2015-05-28 05:36:21 +00:00
|
|
|
continue
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), ee.errMsg) {
|
|
|
|
t.Errorf("Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
|
2013-02-14 00:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-06 07:55:22 +00:00
|
|
|
|
|
|
|
func TestBadTargetGroup(t *testing.T) {
|
|
|
|
content, err := ioutil.ReadFile("testdata/tgroup.bad.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var tg TargetGroup
|
|
|
|
err = json.Unmarshal(content, &tg)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected unmarshal error but got none.")
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 14:12:33 +00:00
|
|
|
|
|
|
|
func TestEmptyConfig(t *testing.T) {
|
|
|
|
c, err := Load("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error parsing empty config file: %s", err)
|
|
|
|
}
|
|
|
|
exp := DefaultConfig
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(*c, exp) {
|
|
|
|
t.Fatalf("want %v, got %v", exp, c)
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 17:58:34 +00:00
|
|
|
|
|
|
|
func TestEmptyGlobalBlock(t *testing.T) {
|
|
|
|
c, err := Load("global:\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error parsing empty config file: %s", err)
|
|
|
|
}
|
|
|
|
exp := DefaultConfig
|
|
|
|
exp.original = "global:\n"
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(*c, exp) {
|
|
|
|
t.Fatalf("want %v, got %v", exp, c)
|
|
|
|
}
|
|
|
|
}
|