From ce7d852d2234f3979e68ce69c7a4600d960c6ccb Mon Sep 17 00:00:00 2001
From: Mika Eloranta <mel@ohmu.fi>
Date: Mon, 25 Apr 2016 15:10:13 +0300
Subject: [PATCH] postgresql_extensible: configurable measurement name

The output measurement name can be configured per query.
---
 .../postgresql_extensible.go                  | 40 ++++++++++++-------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go
index 34181187..cd6fa8cc 100644
--- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go
+++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go
@@ -21,18 +21,20 @@ type Postgresql struct {
 	AdditionalTags   []string
 	sanitizedAddress string
 	Query            []struct {
-		Sqlquery   string
-		Version    int
-		Withdbname bool
-		Tagvalue   string
+		Sqlquery    string
+		Version     int
+		Withdbname  bool
+		Tagvalue    string
+		Measurement string
 	}
 }
 
 type query []struct {
-	Sqlquery   string
-	Version    int
-	Withdbname bool
-	Tagvalue   string
+	Sqlquery    string
+	Version     int
+	Withdbname  bool
+	Tagvalue    string
+	Measurement string
 }
 
 var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
@@ -65,24 +67,28 @@ var sampleConfig = `
   ## because the databases variable was set to ['postgres', 'pgbench' ] and the
   ## withdbname was true. Be careful that if the withdbname is set to false you
   ## don't have to define the where clause (aka with the dbname) the tagvalue
-  ## field is used to define custom tags (separated by comas)
+  ## field is used to define custom tags (separated by commas)
+  ## The optional "measurement" value can be used to override the default
+  ## output measurement name ("postgresql").
   #
   ## Structure :
   ## [[inputs.postgresql_extensible.query]]
   ##   sqlquery string
   ##   version string
   ##   withdbname boolean
-  ##   tagvalue string (coma separated)
+  ##   tagvalue string (comma separated)
+  ##   measurement string
   [[inputs.postgresql_extensible.query]]
     sqlquery="SELECT * FROM pg_stat_database"
     version=901
     withdbname=false
     tagvalue=""
+    measurement=""
   [[inputs.postgresql_extensible.query]]
     sqlquery="SELECT * FROM pg_stat_bgwriter"
     version=901
     withdbname=false
-    tagvalue=""
+    tagvalue="postgresql.stats"
 `
 
 func (p *Postgresql) SampleConfig() string {
@@ -106,6 +112,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
 	var db_version int
 	var query string
 	var tag_value string
+	var meas_name string
 
 	if p.Address == "" || p.Address == "localhost" {
 		p.Address = localhost
@@ -131,6 +138,11 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
 	for i := range p.Query {
 		sql_query = p.Query[i].Sqlquery
 		tag_value = p.Query[i].Tagvalue
+		if p.Query[i].Measurement != "" {
+			meas_name = p.Query[i].Measurement
+		} else {
+			meas_name = "postgresql"
+		}
 
 		if p.Query[i].Withdbname {
 			if len(p.Databases) != 0 {
@@ -170,7 +182,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
 			}
 
 			for rows.Next() {
-				err = p.accRow(rows, acc)
+				err = p.accRow(meas_name, rows, acc)
 				if err != nil {
 					return err
 				}
@@ -201,7 +213,7 @@ func (p *Postgresql) SanitizedAddress() (_ string, err error) {
 	return p.sanitizedAddress, err
 }
 
-func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
+func (p *Postgresql) accRow(meas_name string, row scanner, acc telegraf.Accumulator) error {
 	var columnVars []interface{}
 	var dbname bytes.Buffer
 
@@ -267,7 +279,7 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
 			}
 		}
 	}
-	acc.AddFields("postgresql", fields, tags)
+	acc.AddFields(meas_name, fields, tags)
 	return nil
 }
 
-- 
GitLab