Postgresql_extensible tests are now working

This commit is contained in:
Thomas Menard 2016-03-07 11:13:51 +01:00
parent 975a4c6096
commit 0b59196a85
2 changed files with 28 additions and 74 deletions

View File

@ -21,11 +21,18 @@ type Postgresql struct {
Query []struct {
Sqlquery string
Version int
Withdbname string
Withdbname bool
Tagvalue string
}
}
type query []struct {
Sqlquery string
Version int
Withdbname bool
Tagvalue string
}
var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
var sampleConfig = `
@ -51,15 +58,21 @@ var sampleConfig = `
# succeed.
# the tagvalue field is used to define custom tags (separated by comas)
#
# Structure :
# [[inputs.postgresql_extensible.query]]
# sqlquery string
# version string
# withdbname boolean
# tagvalue string (coma separated)
[[inputs.postgresql_extensible.query]]
sqlquery="SELECT * FROM pg_stat_database where datname"
version=901
withdbname="true"
withdbname=true
tagvalue=""
[[inputs.postgresql_extensible.query]]
sqlquery="SELECT * FROM pg_stat_bgwriter"
version=901
withdbname="false"
withdbname=false
tagvalue=""
`
@ -80,8 +93,6 @@ var localhost = "host=localhost sslmode=disable"
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
var sql_query string
var query_version int
var query_with_dbname string
var query_addon string
var db_version int
var query string
@ -105,17 +116,14 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
if err != nil {
return err
}
// We loop in order to process each query
// Query is not run if Database version does not match the query version.
for i := range p.Query {
sql_query = p.Query[i].Sqlquery
query_version = p.Query[i].Version
query_with_dbname = p.Query[i].Withdbname
tag_value = p.Query[i].Tagvalue
if query_with_dbname == "true" {
if p.Query[i].Withdbname {
if len(p.Databases) != 0 {
query_addon = fmt.Sprintf(` IN ('%s')`,
strings.Join(p.Databases, "','"))
@ -127,7 +135,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
}
sql_query += query_addon
if query_version <= db_version {
if p.Query[i].Version <= db_version {
rows, err := db.Query(sql_query)
if err != nil {
return err

View File

@ -18,12 +18,17 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
Databases: []string{"postgres"},
Query: query{
{Sqlquery: "select * from pg_stat_database",
Version: 901,
Withdbname: false,
Tagvalue: ""},
},
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
fmt.Printf(p.Databases)
availableColumns := make(map[string]bool)
for _, col := range p.AllColumns {
availableColumns[col] = true
@ -43,14 +48,6 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
"temp_bytes",
"deadlocks",
"numbackends",
"buffers_alloc",
"buffers_backend",
"buffers_backend_fsync",
"buffers_checkpoint",
"buffers_clean",
"checkpoints_req",
"checkpoints_timed",
"maxwritten_clean",
}
floatMetrics := []string{
@ -77,58 +74,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
}
assert.True(t, metricsCounted > 0)
//assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
}
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
Databases: []string{"postgres"},
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
point, ok := acc.Get("postgresql")
require.True(t, ok)
assert.Equal(t, "postgres", point.Tags["db"])
}
func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
}
var acc testutil.Accumulator
err := p.Gather(&acc)
require.NoError(t, err)
var found bool
for _, pnt := range acc.Metrics {
if pnt.Measurement == "postgresql" {
if pnt.Tags["db"] == "postgres" {
found = true
break
}
}
}
assert.True(t, found)
assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
}
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {