Skip to content

Commit fc3a14f

Browse files
committed
fix bug with multiple columns in same metric (only the last one was being used because the map was being overwritten each time); add -dumpmaps option for debugging
1 parent 895166b commit fc3a14f

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

postgres_exporter.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
//"regexp"
1111
//"strconv"
1212
//"strings"
13+
"gopkg.in/yaml.v2"
14+
"io/ioutil"
1315
"math"
1416
"time"
15-
"io/ioutil"
16-
"gopkg.in/yaml.v2"
1717

1818
_ "github.com/lib/pq"
1919
"github.com/prometheus/client_golang/prometheus"
@@ -36,6 +36,10 @@ var (
3636
"extend.query-path", "",
3737
"Path to custom queries to run.",
3838
)
39+
onlyDumpMaps = flag.Bool(
40+
"dumpmaps", false,
41+
"Do not run, simply dump the maps.",
42+
)
3943
)
4044

4145
// Metric name parts.
@@ -111,6 +115,21 @@ var variableMaps = map[string]map[string]ColumnMapping{
111115
},
112116
}
113117

118+
func dumpMaps() {
119+
for name, cmap := range metricMaps {
120+
query, ok := queryOverrides[name]
121+
if ok {
122+
fmt.Printf("%s: %s\n", name, query)
123+
} else {
124+
fmt.Println(name)
125+
}
126+
for column, details := range cmap {
127+
fmt.Printf(" %-40s %v\n", column, details)
128+
}
129+
fmt.Println()
130+
}
131+
}
132+
114133
var metricMaps = map[string]map[string]ColumnMapping{
115134
"pg_stat_bgwriter": map[string]ColumnMapping{
116135
"checkpoints_timed": {COUNTER, "Number of scheduled checkpoints that have been performed", nil},
@@ -235,7 +254,6 @@ func addQueries(queriesPath string) (err error) {
235254
return err
236255
}
237256

238-
239257
for metric, specs := range extra {
240258
for key, value := range specs.(map[interface{}]interface{}) {
241259
switch key.(string) {
@@ -249,14 +267,16 @@ func addQueries(queriesPath string) (err error) {
249267

250268
for n, a := range column {
251269
var cmap ColumnMapping
252-
var metric_map map[string]ColumnMapping
253270

254-
metric_map = make(map[string]ColumnMapping)
271+
metric_map, ok := metricMaps[metric]
272+
if !ok {
273+
metric_map = make(map[string]ColumnMapping)
274+
}
255275

256276
name := n.(string)
257277

258278
for attr_key, attr_val := range a.(map[interface{}]interface{}) {
259-
switch(attr_key.(string)) {
279+
switch attr_key.(string) {
260280
case "usage":
261281
usage, err := stringToColumnUsage(attr_val.(string))
262282
if err != nil {
@@ -374,7 +394,7 @@ func makeDescMap(metricMaps map[string]map[string]ColumnMapping) map[string]Metr
374394

375395
// convert a string to the corresponding ColumnUsage
376396
func stringToColumnUsage(s string) (u ColumnUsage, err error) {
377-
switch(s) {
397+
switch s {
378398
case "DISCARD":
379399
u = DISCARD
380400

@@ -666,18 +686,23 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
666686
func main() {
667687
flag.Parse()
668688

669-
dsn := os.Getenv("DATA_SOURCE_NAME")
670-
if len(dsn) == 0 {
671-
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
672-
}
673-
674689
if *queriesPath != "" {
675690
err := addQueries(*queriesPath)
676691
if err != nil {
677692
log.Warnln("Unparseable queries file - discarding merge: ", *queriesPath, err)
678693
}
679694
}
680695

696+
if *onlyDumpMaps {
697+
dumpMaps()
698+
return
699+
}
700+
701+
dsn := os.Getenv("DATA_SOURCE_NAME")
702+
if len(dsn) == 0 {
703+
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
704+
}
705+
681706
exporter := NewExporter(dsn)
682707
prometheus.MustRegister(exporter)
683708

0 commit comments

Comments
 (0)