diff --git a/Dockerfile b/Dockerfile index 10e87c8..c40d65e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.7 AS build +FROM golang:1.8 AS build ADD . /src WORKDIR /src RUN go get -t github.com/stretchr/testify/suite @@ -14,7 +14,8 @@ ENV GLOBAL_SCRAPE_INTERVAL=10s \ ARG_CONFIG_FILE=/etc/prometheus/prometheus.yml \ ARG_STORAGE_LOCAL_PATH=/prometheus \ ARG_WEB_CONSOLE_LIBRARIES=/usr/share/prometheus/console_libraries \ - ARG_WEB_CONSOLE_TEMPLATES=/usr/share/prometheus/consoles + ARG_WEB_CONSOLE_TEMPLATES=/usr/share/prometheus/consoles \ + CONFIGS_DIR="/run/secrets" EXPOSE 8080 diff --git a/docs/config.md b/docs/config.md index 9d6aad4..e01ea4b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -71,4 +71,6 @@ Please consult [Prometheus Configuration](https://prometheus.io/docs/operating/c ## Scrapes -Content of Docker secrets prefixed with the name `scrape_` is automatically added to the `scrape_configs` section of the configuration. \ No newline at end of file +Additional scrapes can be added through files prefixed with `scrape_`. By default, all such files located in `/run/secrets` are automatically added to the `scrape_configs` section of the configuration. The directory can be changed by setting a different value to the environment variable `CONFIGS_DIR`. + +The simplest way to add scrape configs is to use Docker [secrets](https://docs.docker.com/engine/swarm/secrets/) or [configs](https://docs.docker.com/engine/swarm/configs/). \ No newline at end of file diff --git a/prometheus/config.go b/prometheus/config.go index ed49d1e..de5f546 100644 --- a/prometheus/config.go +++ b/prometheus/config.go @@ -26,7 +26,7 @@ global:` } func GetScrapeConfig(scrapes map[string]Scrape) string { - config := getScrapeConfigFromMap(scrapes) + getScrapeConfigFromSecrets() + config := getScrapeConfigFromMap(scrapes) + getScrapeConfigFromDir() if len(config) > 0 { if !strings.HasPrefix(config, "\n") { config = "\n" + config @@ -73,14 +73,21 @@ func getGlobalConfigData() map[string]map[string]string { return data } -func getScrapeConfigFromSecrets() string { +func getScrapeConfigFromDir() string { config := "" - if files, err := afero.ReadDir(FS, "/run/secrets/"); err == nil { + dir := "/run/secrets/" + if len(os.Getenv("CONFIGS_DIR")) > 0 { + dir = os.Getenv("CONFIGS_DIR") + } + if !strings.HasSuffix(dir, "/") { + dir += "/" + } + if files, err := afero.ReadDir(FS, dir); err == nil { for _, file := range files { if !strings.HasPrefix(file.Name(), "scrape_") { continue } - if content, err := afero.ReadFile(FS, "/run/secrets/" + file.Name()); err == nil { + if content, err := afero.ReadFile(FS, dir + file.Name()); err == nil { config += string(content) if !strings.HasSuffix(config, "\n") { config += "\n" diff --git a/prometheus/config_test.go b/prometheus/config_test.go index b568893..e1e7914 100644 --- a/prometheus/config_test.go +++ b/prometheus/config_test.go @@ -157,6 +157,31 @@ scrape_configs: s.Equal(expected, actual) } +func (s *ConfigTestSuite) Test_GetScrapeConfig_ReturnsConfigsFromCustomDirectory() { + fsOrig := FS + defer func() { FS = fsOrig }() + FS = afero.NewMemMapFs() + defer func() { os.Unsetenv("CONFIGS_DIR") }() + os.Setenv("CONFIGS_DIR", "/tmp") + job := ` - job_name: "my-service" + dns_sd_configs: + - names: ["tasks.my-service"] + type: A + port: 5678` + expected := fmt.Sprintf(` +scrape_configs: +%s +`, + job, + ) + scrapes := map[string]Scrape {} + afero.WriteFile(FS, "/tmp/scrape_job", []byte(job), 0644) + + actual := GetScrapeConfig(scrapes) + + s.Equal(expected, actual) +} + func (s *ConfigTestSuite) Test_GetScrapeConfig_ReturnsEmptyString_WhenNoData() { actual := GetScrapeConfig(map[string]Scrape{})