@@ -12,6 +12,8 @@ import (
1212 //"strings"
1313 "math"
1414 "time"
15+ "io/ioutil"
16+ "gopkg.in/yaml.v2"
1517
1618 _ "github.com/lib/pq"
1719 "github.com/prometheus/client_golang/prometheus"
@@ -219,6 +221,95 @@ var queryOverrides = map[string]string{
219221 ON tmp.state = tmp2.state AND pg_database.datname = tmp2.datname` ,
220222}
221223
224+ // Add queries to the metricMaps and queryOverrides maps
225+ func add_queries (queriesPath string ) (err error ) {
226+ var extra map [string ]interface {}
227+
228+ content , err := ioutil .ReadFile (queriesPath )
229+ if err != nil {
230+ return err
231+ }
232+
233+ err = yaml .Unmarshal (content , & extra )
234+ if err != nil {
235+ return err
236+ }
237+
238+
239+ for metric , specs := range extra {
240+ for key , value := range specs .(map [interface {}]interface {}) {
241+ switch key .(string ) {
242+ case "query" :
243+ query := value .(string )
244+ queryOverrides [metric ] = query
245+
246+ case "metrics" :
247+ for _ , c := range value .([]interface {}) {
248+ column := c .(map [interface {}]interface {})
249+
250+ for n , a := range column {
251+ var cmap ColumnMapping
252+ var metric_map map [string ]ColumnMapping
253+
254+ metric_map = make (map [string ]ColumnMapping )
255+
256+ name := n .(string )
257+
258+ for attr_key , attr_val := range a .(map [interface {}]interface {}) {
259+ switch (attr_key .(string )) {
260+ case "usage" :
261+ usage , err := _string_to_columnusage (attr_val .(string ))
262+ if err != nil {
263+ return err
264+ }
265+ cmap .usage = usage
266+ case "description" :
267+ cmap .description = attr_val .(string )
268+ }
269+ }
270+
271+ cmap .mapping = nil
272+
273+ metric_map [name ] = cmap
274+
275+ metricMaps [metric ] = metric_map
276+ }
277+ }
278+ }
279+ }
280+ }
281+
282+ return
283+ }
284+
285+ // convert a string to the corresponding ColumnUsage
286+ func _string_to_columnusage (s string ) (u ColumnUsage , err error ) {
287+ switch (s ) {
288+ case "DISCARD" :
289+ u = DISCARD
290+
291+ case "LABEL" :
292+ u = LABEL
293+
294+ case "COUNTER" :
295+ u = COUNTER
296+
297+ case "GAUGE" :
298+ u = GAUGE
299+
300+ case "MAPPEDMETRIC" :
301+ u = MAPPEDMETRIC
302+
303+ case "DURATION" :
304+ u = DURATION
305+ default :
306+ err = fmt .Errorf ("wrong ColumnUsage given : %s" , s )
307+ }
308+
309+ return
310+ }
311+
312+
222313// Turn the MetricMap column mapping into a prometheus descriptor mapping.
223314func makeDescMap (metricMaps map [string ]map [string ]ColumnMapping ) map [string ]MetricMapNamespace {
224315 var metricMap = make (map [string ]MetricMapNamespace )
@@ -580,6 +671,13 @@ func main() {
580671 log .Fatal ("couldn't find environment variable DATA_SOURCE_NAME" )
581672 }
582673
674+ if * queriesPath != "" {
675+ err := add_queries (* queriesPath )
676+ if err != nil {
677+ log .Warnln ("Unparseable queries file - discarding merge: " , * queriesPath , err )
678+ }
679+ }
680+
583681 exporter := NewExporter (dsn )
584682 prometheus .MustRegister (exporter )
585683
0 commit comments