Skip to content

Commit

Permalink
ENH: Adds configs_dir (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjpfan authored May 31, 2018
1 parent 6551d19 commit cb12acf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func WriteConfig(configPath string, scrapes map[string]Scrape,
FS.MkdirAll(fileSDDir, 0755)
c.InsertScrapes(scrapes)

configsDir := os.Getenv("CONFIGS_DIR")
if len(configDir) != 0 {
c.InsertScrapesFromDir(configsDir)
}

if len(alerts) > 0 {
logPrintf("Writing to alert.rules")
afero.WriteFile(FS, alertRulesPath, []byte(GetAlertConfig(alerts)), 0644)
Expand Down
11 changes: 11 additions & 0 deletions prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,20 +551,29 @@ func (s *ConfigTestSuite) Test_Writeconfig_WritesConfig() {
FS = fsOrig
os.Unsetenv("REMOTE_WRITE_URL")
os.Unsetenv("REMOTE_READ_URL")
os.Unsetenv("CONFIGS_DIR")
}()
os.Setenv("REMOTE_WRITE_URL", "http://acme.com/write")
os.Setenv("REMOTE_READ_URL", "http://acme.com/read")
os.Setenv("CONFIGS_DIR", "/tmp")
FS = afero.NewMemMapFs()
scrapes := map[string]Scrape{
"service-1": {ServiceName: "service-1", ScrapePort: 1234},
"service-2": {ServiceName: "service-2", ScrapePort: 5678},
}
alerts := map[string]Alert{}
job := `- job_name: "my-service"
dns_sd_configs:
- names: ["tasks.my-service"]
type: A
port: 5678`
afero.WriteFile(FS, "/tmp/scrape_job", []byte(job), 0644)

c := Config{}
c.InsertEnv("REMOTE_WRITE_URL", "http://acme.com/write")
c.InsertEnv("REMOTE_READ_URL", "http://acme.com/read")
c.InsertScrapes(scrapes)
c.InsertScrapesFromDir("/tmp")

nodeLabels := map[string]map[string]string{}
WriteConfig("/etc/prometheus/prometheus.yml", scrapes, alerts, nodeLabels)
Expand All @@ -578,8 +587,10 @@ func (s *ConfigTestSuite) Test_Writeconfig_WritesConfig() {
s.Equal(c.RemoteWriteConfigs[0].URL, actualConfig.RemoteWriteConfigs[0].URL)

// Order of scrapes can be different
s.Require().Len(actualConfig.ScrapeConfigs, 3)
s.Contains(actualConfig.ScrapeConfigs, c.ScrapeConfigs[0])
s.Contains(actualConfig.ScrapeConfigs, c.ScrapeConfigs[1])
s.Contains(actualConfig.ScrapeConfigs, c.ScrapeConfigs[2])
}

func (s *ConfigTestSuite) Test_Writeconfig_WithNodeInfoAndNodes_WritesConfig() {
Expand Down

0 comments on commit cb12acf

Please sign in to comment.