Skip to content

Commit

Permalink
Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Aug 8, 2017
1 parent 5c51ebc commit e60cbbe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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/).
15 changes: 11 additions & 4 deletions prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down

0 comments on commit e60cbbe

Please sign in to comment.