From b5a8f7b8fabe5a2b5213347c3006612fc52265b1 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 30 Apr 2015 21:15:18 +0200 Subject: [PATCH] Cleanup, test, and document config. --- config/config.go | 10 ++- config/config_test.go | 5 +- config/fixtures/full.conf.input | 89 +++++++++++++++++++++++++++ config/fixtures/sd_targets.conf.input | 2 + config/load.go | 10 +-- config/load_test.go | 2 +- retrieval/targetmanager.go | 6 +- retrieval/targetmanager_test.go | 4 +- rules/manager/manager.go | 4 +- 9 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 config/fixtures/full.conf.input diff --git a/config/config.go b/config/config.go index 94f45fb0f7..e8eafa67a6 100644 --- a/config/config.go +++ b/config/config.go @@ -29,8 +29,10 @@ import ( pb "github.com/prometheus/prometheus/config/generated" ) -var jobNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_-]*$") -var labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") +var ( + jobNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_-]*$") + labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") +) // Config encapsulates the configuration of a Prometheus instance. It wraps the // raw configuration protocol buffer to be able to add custom methods to it. @@ -232,15 +234,17 @@ func (c *DNSConfig) Validate() error { return nil } -// SDRefreshInterval gets the the SD refresh interval for the scrape config. +// RefreshInterval gets the the refresh interval for DNS service discovery. func (c *DNSConfig) RefreshInterval() time.Duration { return stringToDuration(c.GetRefreshInterval()) } +// RelabelConfig encapsulates the protobuf configuration object for relabeling. type RelabelConfig struct { pb.RelabelConfig } +// Validate checks the RelabelConfig for the validity of its fields. func (c *RelabelConfig) Validate() error { if len(c.GetSourceLabel()) == 0 { return errors.New("at least one source label is required") diff --git a/config/config_test.go b/config/config_test.go index 9955dc99fa..80dfb30280 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -34,8 +34,9 @@ var configTests = []struct { inputFile: "empty.conf.input", }, { inputFile: "sd_targets.conf.input", - }, - { + }, { + inputFile: "full.conf.input", + }, { inputFile: "invalid_proto_format.conf.input", shouldFail: true, errContains: "unknown field name", diff --git a/config/fixtures/full.conf.input b/config/fixtures/full.conf.input new file mode 100644 index 0000000000..5e5b340923 --- /dev/null +++ b/config/fixtures/full.conf.input @@ -0,0 +1,89 @@ +global < + scrape_interval: "30s" + evaluation_interval: "30s" + labels: < + label: < + name: "monitor" + value: "test" + > + label: < + name: "more" + value: "test" + > + > + rule_file: "prometheus.rules" + rule_file: "prometheus.more.rules" +> + +scrape_config: < + job_name: "prometheus" + scrape_interval: "15s" + metrics_path: "/metrics" + scheme: "http" + + target_group: < + target: "localhost:9090" + > +> + +scrape_config: < + job_name: "myjob" + scrape_interval: "15s" + metrics_path: "/metrics" + scheme: "http" + + dns_config: < + name: "first.srv.name" + name: "second.srv.name" + refresh_interval: "1h" + > + + dns_config: < + name: "first2.srv.name" + name: "second2.srv.name" + refresh_interval: "1m" + > + + relabel_config: < + source_label: "l1" + source_label: "l2" + regex: "^foobar.*$" + target_label: "l3" + replacement: "static" + > + relabel_config: < + source_label: "l4" + regex: "^foobar.*$" + action: DROP + > + relabel_config: < + source_label: "l4" + regex: "^foobar.*$" + action: KEEP + > + + target_group: < + target: "localhost:9090" + target: "localhost:9091" + labels: < + label: < + name: "tg1" + value: "tg1" + > + > + > + target_group: < + target: "my.domain:9090" + target: "my.domain:9091" + labels: < + label: < + name: "tg2" + value: "tg2" + > + label: < + name: "tg2_1" + value: "tg2_1" + > + > + > +> diff --git a/config/fixtures/sd_targets.conf.input b/config/fixtures/sd_targets.conf.input index 7b694db217..2f9acd68ef 100644 --- a/config/fixtures/sd_targets.conf.input +++ b/config/fixtures/sd_targets.conf.input @@ -2,5 +2,7 @@ scrape_config: < job_name: "testjob" dns_config: < name: "sd_name" + name: "sd_name2" + refresh_interval: "15s" > > diff --git a/config/load.go b/config/load.go index 75b6b03f80..3ee7f86951 100644 --- a/config/load.go +++ b/config/load.go @@ -22,10 +22,10 @@ import ( ) // LoadFromString returns a config parsed from the provided string. -func LoadFromString(configStr string) (Config, error) { +func LoadFromString(configStr string) (*Config, error) { configProto := pb.PrometheusConfig{} if err := proto.UnmarshalText(configStr, &configProto); err != nil { - return Config{}, err + return nil, err } if configProto.Global == nil { configProto.Global = &pb.GlobalConfig{} @@ -36,17 +36,17 @@ func LoadFromString(configStr string) (Config, error) { } } - config := Config{configProto} + config := &Config{configProto} err := config.Validate() return config, err } // LoadFromFile returns a config parsed from the file of the provided name. -func LoadFromFile(fileName string) (Config, error) { +func LoadFromFile(fileName string) (*Config, error) { configStr, err := ioutil.ReadFile(fileName) if err != nil { - return Config{}, err + return nil, err } return LoadFromString(string(configStr)) diff --git a/config/load_test.go b/config/load_test.go index 0a086f3004..5e1aa423f4 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -20,6 +20,6 @@ import ( func TestLoadFromFile(t *testing.T) { _, err := LoadFromFile("file-does-not-exist.conf") if err == nil { - t.Error(err) + t.Error("Error expected on non-existing config file path but got none") } } diff --git a/retrieval/targetmanager.go b/retrieval/targetmanager.go index 66d29bd235..6e9f29f49c 100644 --- a/retrieval/targetmanager.go +++ b/retrieval/targetmanager.go @@ -63,7 +63,7 @@ type TargetManager struct { } // NewTargetManager creates a new TargetManager based on the given config. -func NewTargetManager(cfg config.Config, sampleAppender storage.SampleAppender) (*TargetManager, error) { +func NewTargetManager(cfg *config.Config, sampleAppender storage.SampleAppender) (*TargetManager, error) { tm := &TargetManager{ sampleAppender: sampleAppender, targets: make(map[string][]Target), @@ -273,7 +273,7 @@ func (tm *TargetManager) Pools() map[string][]Target { // ApplyConfig resets the manager's target providers and job configurations as defined // by the new cfg. The state of targets that are valid in the new configuration remains unchanged. -func (tm *TargetManager) ApplyConfig(cfg config.Config) error { +func (tm *TargetManager) ApplyConfig(cfg *config.Config) error { tm.stop(false) // Even if updating the config failed, we want to continue rather than stop scraping anything. defer tm.Run() @@ -285,7 +285,7 @@ func (tm *TargetManager) ApplyConfig(cfg config.Config) error { return nil } -func (tm *TargetManager) applyConfig(cfg config.Config) error { +func (tm *TargetManager) applyConfig(cfg *config.Config) error { // Only apply changes if everything was successful. providers := map[*config.ScrapeConfig][]TargetProvider{} diff --git a/retrieval/targetmanager_test.go b/retrieval/targetmanager_test.go index 6b539d1a74..7aa0903140 100644 --- a/retrieval/targetmanager_test.go +++ b/retrieval/targetmanager_test.go @@ -262,7 +262,7 @@ func TestTargetManagerConfigUpdate(t *testing.T) { }, } - targetManager, err := NewTargetManager(config.Config{}, nopAppender{}) + targetManager, err := NewTargetManager(&config.Config{}, nopAppender{}) if err != nil { t.Fatal(err) } @@ -273,7 +273,7 @@ func TestTargetManagerConfigUpdate(t *testing.T) { cfg := pb.PrometheusConfig{ ScrapeConfig: step.scrapeConfigs, } - err := targetManager.ApplyConfig(config.Config{cfg}) + err := targetManager.ApplyConfig(&config.Config{cfg}) if err != nil { t.Fatal(err) } diff --git a/rules/manager/manager.go b/rules/manager/manager.go index 6865e7df5e..1e7e070f45 100644 --- a/rules/manager/manager.go +++ b/rules/manager/manager.go @@ -74,7 +74,7 @@ func init() { // NewRuleManager. type RuleManager interface { // Load and add rules from rule files specified in the configuration. - AddRulesFromConfig(config config.Config) error + AddRulesFromConfig(config *config.Config) error // Start the rule manager's periodic rule evaluation. Run() // Stop the rule manager's rule evaluation cycles. @@ -267,7 +267,7 @@ func (m *ruleManager) runIteration() { wg.Wait() } -func (m *ruleManager) AddRulesFromConfig(config config.Config) error { +func (m *ruleManager) AddRulesFromConfig(config *config.Config) error { for _, ruleFile := range config.Global.RuleFile { newRules, err := rules.LoadRulesFromFile(ruleFile) if err != nil {