Skip to content

Commit 734317b

Browse files
noushiTomas Haber
authored andcommitted
Add support for custom queries from yaml file
1 parent a780a46 commit 734317b

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

postgres_exporter.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"os"
1010
"time"
11+
"io/ioutil"
12+
"gopkg.in/yaml.v2"
1113

1214
"strconv"
1315

@@ -216,6 +218,95 @@ var queryOverrides = map[string]string{
216218
ON tmp.state = tmp2.state AND pg_database.datname = tmp2.datname`,
217219
}
218220

221+
// Add queries to the metricMaps and queryOverrides maps
222+
func add_queries(queriesPath string) (err error) {
223+
var extra map[string]interface{}
224+
225+
content, err := ioutil.ReadFile(queriesPath)
226+
if err != nil {
227+
return err
228+
}
229+
230+
err = yaml.Unmarshal(content, &extra)
231+
if err != nil {
232+
return err
233+
}
234+
235+
236+
for metric, specs := range extra {
237+
for key, value := range specs.(map[interface{}]interface{}) {
238+
switch key.(string) {
239+
case "query":
240+
query := value.(string)
241+
queryOverrides[metric] = query
242+
243+
case "metrics":
244+
for _, c := range value.([]interface{}) {
245+
column := c.(map[interface{}]interface{})
246+
247+
for n, a := range column {
248+
var cmap ColumnMapping
249+
var metric_map map[string]ColumnMapping
250+
251+
metric_map = make(map[string]ColumnMapping)
252+
253+
name := n.(string)
254+
255+
for attr_key, attr_val := range a.(map[interface{}]interface{}) {
256+
switch(attr_key.(string)) {
257+
case "usage":
258+
usage, err := _string_to_columnusage(attr_val.(string))
259+
if err != nil {
260+
return err
261+
}
262+
cmap.usage = usage
263+
case "description":
264+
cmap.description = attr_val.(string)
265+
}
266+
}
267+
268+
cmap.mapping = nil
269+
270+
metric_map[name] = cmap
271+
272+
metricMaps[metric] = metric_map
273+
}
274+
}
275+
}
276+
}
277+
}
278+
279+
return
280+
}
281+
282+
// convert a string to the corresponding ColumnUsage
283+
func _string_to_columnusage(s string) (u ColumnUsage, err error) {
284+
switch(s) {
285+
case "DISCARD":
286+
u = DISCARD
287+
288+
case "LABEL":
289+
u = LABEL
290+
291+
case "COUNTER":
292+
u = COUNTER
293+
294+
case "GAUGE":
295+
u = GAUGE
296+
297+
case "MAPPEDMETRIC":
298+
u = MAPPEDMETRIC
299+
300+
case "DURATION":
301+
u = DURATION
302+
default:
303+
err = fmt.Errorf("wrong ColumnUsage given : %s", s)
304+
}
305+
306+
return
307+
}
308+
309+
219310
// Turn the MetricMap column mapping into a prometheus descriptor mapping.
220311
func makeDescMap(metricMaps map[string]map[string]ColumnMapping) map[string]MetricMapNamespace {
221312
var metricMap = make(map[string]MetricMapNamespace)
@@ -588,6 +679,13 @@ func main() {
588679
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
589680
}
590681

682+
if *queriesPath != "" {
683+
err := add_queries(*queriesPath)
684+
if err != nil {
685+
log.Warnln("Unparseable queries file - discarding merge: ", *queriesPath, err)
686+
}
687+
}
688+
591689
exporter := NewExporter(dsn)
592690
prometheus.MustRegister(exporter)
593691

0 commit comments

Comments
 (0)