postgresql_extensible: configurable measurement name

The output measurement name can be configured per query.
This commit is contained in:
Mika Eloranta 2016-04-25 15:10:13 +03:00
parent e7daf5094a
commit 6a371a15ee
1 changed files with 26 additions and 14 deletions

View File

@ -25,6 +25,7 @@ type Postgresql struct {
Version int
Withdbname bool
Tagvalue string
Measurement string
}
}
@ -33,6 +34,7 @@ type query []struct {
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
}