From 86087120ddc8b75a2fbbf54549cd41fc682af68c Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 7 May 2015 16:47:18 +0200 Subject: [PATCH] Replace example config with new YAML format. --- config/config.go | 69 ++++++++++++++++---------- config/config_test.go | 6 +-- config/testdata/conf.good.yml | 4 +- config/testdata/labelname.bad.yml | 2 +- documentation/examples/prometheus.conf | 30 ----------- documentation/examples/prometheus.yml | 30 +++++++++++ retrieval/targetmanager.go | 2 +- 7 files changed, 79 insertions(+), 64 deletions(-) delete mode 100644 documentation/examples/prometheus.conf create mode 100644 documentation/examples/prometheus.yml diff --git a/config/config.go b/config/config.go index 1c501b66f..87fe01595 100644 --- a/config/config.go +++ b/config/config.go @@ -66,7 +66,7 @@ var ( } // The default DNS SD configuration. - DefaultDNSConfig = DefaultedDNSConfig{ + DefaultDNSSDConfig = DefaultedDNSSDConfig{ RefreshInterval: Duration(30 * time.Second), } ) @@ -80,7 +80,7 @@ type Config struct { original string } -func (c *Config) String() string { +func (c Config) String() string { if c.original != "" { return c.original } @@ -117,12 +117,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { // DefaultedConfig is a proxy type for Config. type DefaultedConfig struct { - GlobalConfig *GlobalConfig `yaml:"global_config"` + GlobalConfig *GlobalConfig `yaml:"global"` RuleFiles []string `yaml:"rule_files,omitempty"` ScrapeConfigs []*ScrapeConfig `yaml:"scrape_configs,omitempty"` } -// GlobalConfig configures values that used across other configuration +// GlobalConfig configures values that are used across other configuration // objects. type GlobalConfig struct { // DefaultedGlobalConfig contains the actual fields for GlobalConfig. @@ -141,11 +141,11 @@ func (c *GlobalConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { // DefaultedGlobalConfig is a proxy type for GlobalConfig. type DefaultedGlobalConfig struct { // How frequently to scrape targets by default. - ScrapeInterval Duration `yaml:"scrape_interval"` + ScrapeInterval Duration `yaml:"scrape_interval,omitempty"` // The default timeout when scraping targets. - ScrapeTimeout Duration `yaml:"scrape_timeout"` + ScrapeTimeout Duration `yaml:"scrape_timeout,omitempty"` // How frequently to evaluate rules by default. - EvaluationInterval Duration `yaml:"evaluation_interval"` + EvaluationInterval Duration `yaml:"evaluation_interval,omitempty"` // The labels to add to any timeseries that this Prometheus instance scrapes. Labels clientmodel.LabelSet `yaml:"labels,omitempty"` @@ -175,18 +175,18 @@ type DefaultedScrapeConfig struct { // The job name to which the job label is set by default. JobName string `yaml:"job_name"` // How frequently to scrape the targets of this scrape config. - ScrapeInterval Duration `yaml:"scrape_interval"` + ScrapeInterval Duration `yaml:"scrape_interval,omitempty"` // The timeout for scraping targets of this config. - ScrapeTimeout Duration `yaml:"scrape_timeout"` + ScrapeTimeout Duration `yaml:"scrape_timeout,omitempty"` // The HTTP resource path on which to fetch metrics from targets. - MetricsPath string `yaml:"metrics_path"` + MetricsPath string `yaml:"metrics_path,omitempty"` // The URL scheme with which to fetch metrics from targets. - Scheme string `yaml:"scheme"` + Scheme string `yaml:"scheme,omitempty"` // List of labeled target groups for this job. TargetGroups []*TargetGroup `yaml:"target_groups,omitempty"` // List of DNS service discovery configurations. - DNSConfigs []*DNSConfig `yaml:"dns_configs,omitempty"` + DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"` // List of relabel configurations. RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"` } @@ -203,7 +203,7 @@ type TargetGroup struct { Source string `yaml:"-", json:"-"` } -func (tg *TargetGroup) String() string { +func (tg TargetGroup) String() string { return tg.Source } @@ -229,29 +229,44 @@ func (tg *TargetGroup) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -// DNSConfig is the configuration for DNS based service discovery. -type DNSConfig struct { - // DefaultedDNSConfig contains the actual fields for DNSConfig. - DefaultedDNSConfig `yaml:",inline"` +// MarshalYAML implements the yaml.Marshaller interface. +func (tg TargetGroup) MarshalYAML() (interface{}, error) { + g := &struct { + Targets []string `yaml:"targets"` + Labels clientmodel.LabelSet `yaml:"labels,omitempty"` + }{ + Targets: make([]string, 0, len(tg.Targets)), + Labels: tg.Labels, + } + for _, t := range tg.Targets { + g.Targets = append(g.Targets, string(t[clientmodel.AddressLabel])) + } + return g, nil +} + +// DNSSDConfig is the configuration for DNS based service discovery. +type DNSSDConfig struct { + // DefaultedDNSSDConfig contains the actual fields for DNSSDConfig. + DefaultedDNSSDConfig `yaml:",inline"` } // UnmarshalYAML implements the yaml.Unmarshaller interface. -func (c *DNSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { - c.DefaultedDNSConfig = DefaultDNSConfig - err := unmarshal(&c.DefaultedDNSConfig) +func (c *DNSSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + c.DefaultedDNSSDConfig = DefaultDNSSDConfig + err := unmarshal(&c.DefaultedDNSSDConfig) if err != nil { return err } if len(c.Names) == 0 { - return fmt.Errorf("DNS config must contain at least one SRV server name") + return fmt.Errorf("DNS config must contain at least one SRV record name") } return nil } -// DefaultedDNSConfig is a proxy type for DNSConfig. -type DefaultedDNSConfig struct { +// DefaultedDNSSDConfig is a proxy type for DNSSDConfig. +type DefaultedDNSSDConfig struct { Names []string `yaml:"names"` - RefreshInterval Duration `yaml:"refresh_interval"` + RefreshInterval Duration `yaml:"refresh_interval,omitempty"` } // RelabelAction is the action to be performed on relabeling. @@ -298,7 +313,7 @@ type DefaultedRelabelConfig struct { // with the configured separator in order. SourceLabels clientmodel.LabelNames `yaml:"source_labels,flow"` // Separator is the string between concatenated values from the source labels. - Separator string `yaml:"separator"` + Separator string `yaml:"separator,omitempty"` // Regex against which the concatenation is matched. Regex *Regexp `yaml:"regex"` // The label to which the resulting string is written in a replacement. @@ -306,7 +321,7 @@ type DefaultedRelabelConfig struct { // Replacement is the regex replacement pattern to be used. Replacement string `yaml:"replacement,omitempty"` // Action is the action to be performed for the relabeling. - Action RelabelAction `yaml:"action"` + Action RelabelAction `yaml:"action,omitempty"` } // Regexp encapsulates a regexp.Regexp and makes it YAML marshallable. @@ -329,7 +344,7 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error { } // MarshalYAML implements the yaml.Marshaller interface. -func (re *Regexp) MarshalYAML() (interface{}, error) { +func (re Regexp) MarshalYAML() (interface{}, error) { return re.String(), nil } diff --git a/config/config_test.go b/config/config_test.go index f4c1fa721..792ad83c1 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -72,15 +72,15 @@ var expectedConf = &Config{DefaultedConfig{ MetricsPath: "/my_path", Scheme: "http", - DNSConfigs: []*DNSConfig{ - {DefaultedDNSConfig{ + DNSSDConfigs: []*DNSSDConfig{ + {DefaultedDNSSDConfig{ Names: []string{ "first.dns.address.domain.com", "second.dns.address.domain.com", }, RefreshInterval: Duration(15 * time.Second), }}, - {DefaultedDNSConfig{ + {DefaultedDNSSDConfig{ Names: []string{ "first.dns.address.domain.com", }, diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index 290d2b5cc..a1ff4c192 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -1,5 +1,5 @@ # my global config -global_config: +global: scrape_interval: 15s evaluation_interval: 30s # scrape_timeout is set to the global default (10s). @@ -46,7 +46,7 @@ scrape_configs: metrics_path: /my_path # scheme defaults to 'http'. - dns_configs: + dns_sd_configs: - refresh_interval: 15s names: - first.dns.address.domain.com diff --git a/config/testdata/labelname.bad.yml b/config/testdata/labelname.bad.yml index e940d58e7..66ea324cf 100644 --- a/config/testdata/labelname.bad.yml +++ b/config/testdata/labelname.bad.yml @@ -1,3 +1,3 @@ -global_config: +global: labels: not$allowed: value diff --git a/documentation/examples/prometheus.conf b/documentation/examples/prometheus.conf deleted file mode 100644 index 3bbaad72c..000000000 --- a/documentation/examples/prometheus.conf +++ /dev/null @@ -1,30 +0,0 @@ -# Global default settings. -global { - scrape_interval: "15s" # By default, scrape targets every 15 seconds. - evaluation_interval: "15s" # By default, evaluate rules every 15 seconds. - - # Attach these extra labels to all timeseries collected by this Prometheus instance. - labels: { - label: { - name: "monitor" - value: "codelab-monitor" - } - } - - # Load and evaluate rules in this file every 'evaluation_interval' seconds. This field may be repeated. - #rule_file: "prometheus.rules" -} - -# A job definition containing exactly one endpoint to scrape: Here it's prometheus itself. -job: { - # The job name is added as a label `job={job-name}` to any timeseries scraped from this job. - name: "prometheus" - # Override the global default and scrape targets from this job every 5 seconds. - scrape_interval: "5s" - - # Let's define a group of targets to scrape for this job. In this case, only one. - target_group: { - # These endpoints are scraped via HTTP. - target: "http://localhost:9090/metrics" - } -} diff --git a/documentation/examples/prometheus.yml b/documentation/examples/prometheus.yml new file mode 100644 index 000000000..075645395 --- /dev/null +++ b/documentation/examples/prometheus.yml @@ -0,0 +1,30 @@ +# my global config +global_config: + scrape_interval: 15s # By default, scrape targets every 15 seconds. + evaluation_interval: 15s # By default, scrape targets every 15 seconds. + # scrape_timeout is set to the global default (10s). + + # Attach these extra labels to all timeseries collected by this Prometheus instance. + labels: + monitor: codelab-monitor + +# Load and evaluate rules in this file every 'evaluation_interval' seconds. +rule_files: + # - "first.rules" + # - "second.rules" + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: prometheus + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + scrape_timeout: 10s + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + target_groups: + - targets: ['localhost:9090'] \ No newline at end of file diff --git a/retrieval/targetmanager.go b/retrieval/targetmanager.go index 4cf253c3e..beebe7bd6 100644 --- a/retrieval/targetmanager.go +++ b/retrieval/targetmanager.go @@ -360,7 +360,7 @@ func (tm *TargetManager) targetsFromGroup(tg *config.TargetGroup, cfg *config.Sc func ProvidersFromConfig(cfg *config.ScrapeConfig) ([]TargetProvider, error) { var providers []TargetProvider - for _, dnscfg := range cfg.DNSConfigs { + for _, dnscfg := range cfg.DNSSDConfigs { dnsSD := discovery.NewDNSDiscovery(dnscfg.Names, time.Duration(dnscfg.RefreshInterval)) providers = append(providers, dnsSD) }