Merge pull request #662 from prometheus/fabxc/sd_cfg

Cleanup, test, and document config.
This commit is contained in:
Julius Volz 2015-04-30 23:58:10 +02:00
commit d108366cd6
9 changed files with 114 additions and 18 deletions

View file

@ -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")

View file

@ -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",

View file

@ -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"
>
>
>
>

View file

@ -2,5 +2,7 @@ scrape_config: <
job_name: "testjob"
dns_config: <
name: "sd_name"
name: "sd_name2"
refresh_interval: "15s"
>
>

View file

@ -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))

View file

@ -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")
}
}

View file

@ -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{}

View file

@ -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)
}

View file

@ -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 {