Skip to content

Commit

Permalink
add custom labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigory Ignatyev committed Dec 17, 2018
1 parent 8cc1bd6 commit 6c2db27
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
26 changes: 21 additions & 5 deletions cmd/bash-exporter/bash-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ func main() {
addr := flag.String("web.listen-address", ":9300", "Address on which to expose metrics")
interval := flag.Int("interval", 300, "Interval for metrics collection in seconds")
path := flag.String("path", "/scripts", "path to directory with bash scripts")
labels := flag.String("labels", "hostname,env", "additioanal labels")
prefix := flag.String("prefix", "bash", "Prefix for metrics")
debug := flag.Bool("debug", false, "Debug log level")
flag.Parse()

var labelsArr []string

labelsArr = strings.Split(*labels, ",")
labelsArr = append(labelsArr, "verb", "job")

verbMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: fmt.Sprintf("%s", *prefix),
Help: "bash exporter metrics",
},
[]string{"verb", "job"},
// []string{"verb", "job"},
labelsArr,
)
prometheus.MustRegister(verbMetrics)

Expand All @@ -49,11 +56,11 @@ func main() {
}

http.Handle("/metrics", prometheus.Handler())
go Run(int(*interval), *path, names, *debug)
go Run(int(*interval), *path, names, labelsArr, *debug)
log.Fatal(http.ListenAndServe(*addr, nil))
}

func Run(interval int, path string, names []string, debug bool) {
func Run(interval int, path string, names []string, labelsArr []string, debug bool) {
for {
var wg sync.WaitGroup
oArr := []*run.Output{}
Expand All @@ -76,8 +83,17 @@ func Run(interval int, path string, names []string, debug bool) {
// }
verbMetrics.Reset()
for _, o := range oArr {
for metric, value := range o.Result {
verbMetrics.With(prometheus.Labels{"verb": metric, "job": o.Job}).Set(float64(value))

for metric, value := range o.Schema.Results {
for _, label := range labelsArr {
if _, ok := o.Schema.Labels[label]; !ok {
o.Schema.Labels[label] = ""
}
}
o.Schema.Labels["verb"] = metric
o.Schema.Labels["job"] = o.Job
fmt.Println(o.Schema.Labels)
verbMetrics.With(prometheus.Labels(o.Schema.Labels)).Set(float64(value))
}
}
time.Sleep(time.Duration(interval) * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion examples/job-1.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

echo "{\"items\": 21}"
echo '{"labels": {"env": "dev"}, "results": {"items": 21} }'

exit 0
2 changes: 1 addition & 1 deletion examples/job-2.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

echo "{\"get\": 0.003, \"put\": 0.13, \"time\": 0.5}"
echo '{"labels": {"hostname": "node-1"}, "results": {"get": 0.003, "put": 0.13, "time": 0.5} }'

exit 0
19 changes: 19 additions & 0 deletions examples/job-3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

hnm=`hostname`

if [ true ]
then
result=1
else
result=0
fi

if [ $result=1 ]
then
echo "{\"labels\": {\"host\": \"$hnm\"}, \"results\": {\"my_system_health\": 1} }"
else
echo "{\"labels\": {\"host\": \"$hnm\"}, \"results\": {\"my_system_health\": 0} }"
fi

exit 0
11 changes: 8 additions & 3 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
)

type Output struct {
Result map[string]float64 `json:""`
Job string `json:""`
Schema Schema `json:""`
Job string `json:""`
}

type Schema struct {
Results map[string]float64 `json:"results"`
Labels map[string]string `json:"labels"`
}

func (o *Output) RunJob(p *Params) {
Expand All @@ -25,7 +30,7 @@ func (o *Output) RunExec(path *string) {
log.Fatal(err)
}

err = json.Unmarshal(out, &o.Result)
err = json.Unmarshal(out, &o.Schema)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 6c2db27

Please sign in to comment.