Browse Source

Load rule files from entire directories

pull/762/head
Fabian Reinartz 10 years ago
parent
commit
c44ac7bc26
  1. 6
      config/config.go
  2. 5
      config/config_test.go
  3. 1
      config/testdata/conf.good.yml
  4. 3
      config/testdata/rules.bad.yml
  5. 12
      rules/manager.go

6
config/config.go

@ -18,6 +18,7 @@ import (
var (
patJobName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
patFileSDName = regexp.MustCompile(`^[^*]*(\*[^/]*)?\.(json|yml|yaml|JSON|YML|YAML)$`)
patRulePath = regexp.MustCompile(`^[^*]*(\*[^/]*)?$`)
)
// Load parses the YAML input s into a Config.
@ -112,6 +113,11 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&c.DefaultedConfig); err != nil {
return err
}
for _, rf := range c.RuleFiles {
if !patRulePath.MatchString(rf) {
return fmt.Errorf("invalid rule file path %q", rf)
}
}
// Do global overrides and validate unique names.
jobNames := map[string]struct{}{}
for _, scfg := range c.ScrapeConfigs {

5
config/config_test.go

@ -27,6 +27,7 @@ var expectedConf = &Config{DefaultedConfig{
RuleFiles: []string{
"first.rules",
"second.rules",
"my/*.rules",
},
ScrapeConfigs: []*ScrapeConfig{
@ -151,6 +152,9 @@ var expectedErrors = []struct {
}, {
filename: "regex.bad.yml",
errMsg: "error parsing regexp",
}, {
filename: "rules.bad.yml",
errMsg: "invalid rule file path",
},
}
@ -159,6 +163,7 @@ func TestBadConfigs(t *testing.T) {
_, err := LoadFromFile("testdata/" + ee.filename)
if err == nil {
t.Errorf("Expected error parsing %s but got none", ee.filename)
continue
}
if !strings.Contains(err.Error(), ee.errMsg) {
t.Errorf("Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)

1
config/testdata/conf.good.yml vendored

@ -11,6 +11,7 @@ global:
rule_files:
- "first.rules"
- "second.rules"
- "my/*.rules"
scrape_configs:
- job_name: prometheus

3
config/testdata/rules.bad.yml vendored

@ -0,0 +1,3 @@
rule_files:
- 'my_rule' # fine
- 'my/*/rule' # bad

12
rules/manager.go

@ -16,6 +16,7 @@ package rules
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"time"
@ -281,7 +282,16 @@ func (m *Manager) ApplyConfig(conf *config.Config) {
copy(rulesSnapshot, m.rules)
m.rules = m.rules[:0]
if err := m.loadRuleFiles(conf.RuleFiles...); err != nil {
var files []string
for _, pat := range conf.RuleFiles {
fs, err := filepath.Glob(pat)
if err != nil {
// The only error can be a bad pattern.
log.Errorf("Error retrieving rule files for %s: %s", pat, err)
}
files = append(files, fs...)
}
if err := m.loadRuleFiles(files...); err != nil {
// If loading the new rules failed, restore the old rule set.
m.rules = rulesSnapshot
log.Errorf("Error loading rules, previous rule set restored: %s", err)

Loading…
Cancel
Save