Skip to content

Commit

Permalink
Fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Jun 20, 2017
1 parent f1bd48a commit ca8a04d
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 17 deletions.
9 changes: 9 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ global:
scrape_interval: 10s
```

Nested values can be specified by separating them with `-`. For example, if environment variables `GLOBAL_EXTERNAL_LABELS-CLUSTER=swarm` and `GLOBAL_EXTERNAL_LABELS-TYPE=production` are defined, the resulting Prometheus configuration would be as follows.

```
global:
external_labels:
cluster: swarm
type: production
```

Please consult [Prometheus Configuration](https://prometheus.io/docs/operating/configuration/) for more information about the available options.
34 changes: 31 additions & 3 deletions prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import (
"text/template"
"bytes"
"github.com/spf13/afero"
"strings"
)

func GetGlobalConfig() string {
data := getGlobalConfigData()
config := `
global:`
for _, e := range os.Environ() {
if key, value := getArgFromEnv(e, "GLOBAL"); len(key) > 0 {
config = config + "\n " + key + ": " + value
for key, values := range data {
if len(values[""]) > 0 {
config += "\n " + key + ": " + values[""]
} else {
config += "\n " + key + ":"
for subKey, value := range values {
config += "\n " + subKey + ": " + value
}
}
}
return config
Expand Down Expand Up @@ -50,3 +57,24 @@ func WriteConfig(scrapes map[string]Scrape, alerts map[string]Alert) {
LogPrintf("Writing to prometheus.yml")
afero.WriteFile(FS, "/etc/prometheus/prometheus.yml", []byte(config), 0644)
}

func getGlobalConfigData() map[string]map[string]string {
data := map[string]map[string]string{}
for _, e := range os.Environ() {
if key, value := getArgFromEnv(e, "GLOBAL"); len(key) > 0 {
realKey := key
subKey := ""
if strings.Contains(key, "-") {
keys := strings.Split(key, "-")
realKey = keys[0]
subKey = keys[1]
}
if _, ok := data[realKey]; !ok {
data[realKey] = map[string]string{}
}
subData := data[realKey]
subData[subKey] = value
}
}
return data
}
21 changes: 21 additions & 0 deletions prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ global:
s.Equal(expected, actual)
}

func (s *ConfigTestSuite) Test_GlobalConfig_AllowsNestedEntries() {
scrapeIntervalOrig := os.Getenv("GLOBAL_SCRAPE_INTERVAL")
defer func() {
os.Setenv("GLOBAL_SCRAPE_INTERVAL", scrapeIntervalOrig)
os.Unsetenv("GLOBAL_EXTERNAL_LABELS-CLUSTER")
os.Unsetenv("GLOBAL_EXTERNAL_LABELS-TYPE")
}()
os.Setenv("GLOBAL_SCRAPE_INTERVAL", "123s")
os.Setenv("GLOBAL_EXTERNAL_LABELS-CLUSTER", "swarm")
os.Setenv("GLOBAL_EXTERNAL_LABELS-TYPE", "production")
expected := `
global:
scrape_interval: 123s
external_labels:
cluster: swarm
type: production`

actual := GetGlobalConfig()
s.Equal(expected, actual)
}

// GetScrapeConfig

func (s *ConfigTestSuite) Test_GetScrapeConfig_ReturnsConfigWithData() {
Expand Down
4 changes: 3 additions & 1 deletion scripts/dm-swarm-07.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ docker stack deploy \
exporter

docker stack deploy \
-c stacks/go-demo-alert.yml \
-c stacks/go-demo-scale.yml \
go-demo

docker stack deploy -c stacks/jenkins.yml jenkins
9 changes: 8 additions & 1 deletion stacks/alert-manager-slack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ version: "3"
services:

alert-manager:
image: vfarcic/alert-manager:slack
image: prom/alertmanager
ports:
- 9093:9093
networks:
- monitor
secrets:
- alert_manager_config
command: -config.file=/run/secrets/alert_manager_config -storage.path=/alertmanager

networks:
monitor:
external: true

secrets:
alert_manager_config:
external: true
2 changes: 1 addition & 1 deletion stacks/docker-flow-monitor-slack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.1"
services:

monitor:
image: vfarcic/docker-flow-monitor:${TAG:-latest}
image: vfarcic/docker-flow-monitor
environment:
- LISTENER_ADDRESS=swarm-listener
- GLOBAL_SCRAPE_INTERVAL=10s
Expand Down
2 changes: 1 addition & 1 deletion stacks/go-demo-alert-info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

main:
image: vfarcic/go-demo${TAG}
image: vfarcic/go-demo
environment:
- DB=db
networks:
Expand Down
2 changes: 1 addition & 1 deletion stacks/go-demo-alert-long.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

main:
image: vfarcic/go-demo${TAG}
image: vfarcic/go-demo
environment:
- DB=db
networks:
Expand Down
2 changes: 1 addition & 1 deletion stacks/go-demo-alert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

main:
image: vfarcic/go-demo${TAG}
image: vfarcic/go-demo
environment:
- DB=db
networks:
Expand Down
2 changes: 1 addition & 1 deletion stacks/go-demo-mem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

main:
image: vfarcic/go-demo${TAG}
image: vfarcic/go-demo
environment:
- DB=db
networks:
Expand Down
45 changes: 45 additions & 0 deletions stacks/go-demo-scale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3'

services:

main:
image: vfarcic/go-demo
environment:
- DB=db
networks:
- proxy
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
labels:
- com.df.notify=true
- com.df.distribute=true
- com.df.servicePath=/demo
- com.df.port=8080
- com.df.alertName=memlimit
- com.df.alertIf=@service_mem_limit:0.8
- com.df.alertFor=5s
- com.df.scaleMin=2
- com.df.scaleMax=4
resources:
reservations:
memory: 5M
limits:
memory: 10M

db:
image: mongo
networks:
- proxy
deploy:
resources:
reservations:
memory: 40M
limits:
memory: 80M

networks:
proxy:
external: true
2 changes: 1 addition & 1 deletion stacks/go-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

main:
image: vfarcic/go-demo${TAG}
image: vfarcic/go-demo
environment:
- DB=db
networks:
Expand Down
32 changes: 26 additions & 6 deletions stacks/jenkins.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
version: '3'
version: '3.1'

services:

main:
image: jenkinsci/jenkins:${TAG:-lts-alpine}
master:
image: vfarcic/jenkins
ports:
- "50000:50000"
- 50000:50000
environment:
- JAVA_OPTS="-Djenkins.install.runSetupWizard=false"
- JENKINS_OPTS="--prefix=/jenkins"
networks:
- proxy
- default
deploy:
labels:
- com.df.notify=true
Expand All @@ -20,12 +21,31 @@ services:
- jenkins-user
- jenkins-pass

agent:
image: vfarcic/jenkins-swarm-agent
environment:
- USER_NAME_SECRET=/run/secrets/${JENKINS_USER_SECRET:-jenkins-user}
- PASSWORD_SECRET=/run/secrets/${JENKINS_PASS_SECRET:-jenkins-pass}
- COMMAND_OPTIONS=-master http://master:8080/jenkins -labels 'prod' -executors 4
networks:
- default
volumes:
- /var/run/docker.sock:/var/run/docker.sock
secrets:
- jenkins-user
- jenkins-pass
deploy:
placement:
constraints: [node.role == manager]

networks:
proxy:
external: true
default:
external: false

secrets:
jenkins-user:
external: true
jenkins-pass:
external: true
external: true

0 comments on commit ca8a04d

Please sign in to comment.