diff --git a/config/config.go b/config/config.go index 17137bb674..81ab4fb06f 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,10 @@ import ( "github.com/prometheus/prometheus/utility" ) -var jobNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_-]*$") +var ( + patJobName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) + patFileSDName = regexp.MustCompile(`^[^*]*(\*[^/]*)?\.(json|yml|yaml)$`) +) // Load parses the YAML input s into a Config. func Load(s string) (*Config, error) { @@ -69,6 +72,11 @@ var ( DefaultDNSSDConfig = DefaultedDNSSDConfig{ RefreshInterval: Duration(30 * time.Second), } + + // The default file SD configuration. + DefaultFileSDConfig = DefaultedFileSDConfig{ + RefreshInterval: Duration(30 * time.Second), + } ) // Config is the top-level configuration for Prometheus's config files. @@ -164,7 +172,7 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if err != nil { return err } - if !jobNameRE.MatchString(c.JobName) { + if !patJobName.MatchString(c.JobName) { return fmt.Errorf("%q is not a valid job name", c.JobName) } return nil @@ -189,6 +197,8 @@ type DefaultedScrapeConfig struct { TargetGroups []*TargetGroup `yaml:"target_groups,omitempty"` // List of DNS service discovery configurations. DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"` + // List of file service discovery configurations. + FileSDConfigs []*FileSDConfig `yaml:"file_sd_configs,omitempty"` // List of relabel configurations. RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"` } @@ -266,7 +276,7 @@ func (c *DNSSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } if len(c.Names) == 0 { - return fmt.Errorf("DNS config must contain at least one SRV record name") + return fmt.Errorf("DNS-SD config must contain at least one SRV record name") } return nil } @@ -277,6 +287,36 @@ type DefaultedDNSSDConfig struct { RefreshInterval Duration `yaml:"refresh_interval,omitempty"` } +// FileSDConfig is the configuration for file based discovery. +type FileSDConfig struct { + // DefaultedFileSDConfig contains the actual fields for FileSDConfig. + DefaultedFileSDConfig `yaml:",inline"` +} + +// UnmarshalYAML implements the yaml.Unmarshaller interface. +func (c *FileSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + c.DefaultedFileSDConfig = DefaultFileSDConfig + err := unmarshal(&c.DefaultedFileSDConfig) + if err != nil { + return err + } + if len(c.Names) == 0 { + return fmt.Errorf("file discovery config must contain at least on path name") + } + for _, name := range c.Names { + if !patFileSDName.MatchString(name) { + return fmt.Errorf("path name %q is not valid for file discovery", name) + } + } + return nil +} + +// DefaultedFileSDConfig is a proxy type for FileSDConfig. +type DefaultedFileSDConfig struct { + Names []string `yaml:"names"` + RefreshInterval Duration `yaml:"refresh_interval,omitempty"` +} + // RelabelAction is the action to be performed on relabeling. type RelabelAction string diff --git a/config/config_test.go b/config/config_test.go index 0b7aabd43a..40ea0fa7e2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -52,6 +52,17 @@ var expectedConf = &Config{DefaultedConfig{ }, }, + FileSDConfigs: []*FileSDConfig{ + {DefaultedFileSDConfig{ + Names: []string{"foo/*.slow.json", "foo/*.slow.yml"}, + RefreshInterval: Duration(10 * time.Minute), + }}, + {DefaultedFileSDConfig{ + Names: []string{"bar/*.yaml"}, + RefreshInterval: Duration(30 * time.Second), + }}, + }, + RelabelConfigs: []*RelabelConfig{ {DefaultedRelabelConfig{ SourceLabels: clientmodel.LabelNames{"job", "__meta_dns_srv_name"}, diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index 943901edff..cde9eeed68 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -24,6 +24,14 @@ scrape_configs: labels: foo: baz + file_sd_configs: + - names: + - foo/*.slow.json + - foo/*.slow.yml + refresh_interval: 10m + - names: + - bar/*.yaml + target_groups: - targets: ['localhost:9090', 'localhost:9191'] labels: