From 0b59196a85f7552880059d40055b81130884f525 Mon Sep 17 00:00:00 2001 From: Thomas Menard Date: Mon, 7 Mar 2016 11:13:51 +0100 Subject: [PATCH] Postgresql_extensible tests are now working --- .../postgresql_extensible.go | 32 +++++---- .../postgresql_extensible_test.go | 70 +++---------------- 2 files changed, 28 insertions(+), 74 deletions(-) diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index bc30329cd..a72746891 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -18,14 +18,21 @@ type Postgresql struct { OrderedColumns []string AllColumns []string AdditionalTags []string - Query []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 diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go index 3a26d587f..f4e6d9915 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go @@ -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) {