From 28410754f8cddb08cbc338bdcaa30ae8e4b53c52 Mon Sep 17 00:00:00 2001 From: Guillermo Gonzalez Date: Mon, 28 Mar 2016 18:33:06 -0300 Subject: [PATCH 1/2] add AddressTag config to postgresql input to toggle inclusion of server address tag --- plugins/inputs/postgresql/postgresql.go | 21 ++++++---- plugins/inputs/postgresql/postgresql_test.go | 43 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/postgresql/postgresql.go b/plugins/inputs/postgresql/postgresql.go index d8d0d19786516..2c6487488ad4f 100644 --- a/plugins/inputs/postgresql/postgresql.go +++ b/plugins/inputs/postgresql/postgresql.go @@ -20,6 +20,7 @@ type Postgresql struct { OrderedColumns []string AllColumns []string sanitizedAddress string + AddressTag bool } var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true} @@ -42,6 +43,9 @@ var sampleConfig = ` ## A list of databases to pull metrics about. If not specified, metrics for all ## databases are gathered. # databases = ["app_production", "testing"] + ## Toggle adding a server:canonicalizedAddress tag to the measurment. If not + ## specified, it's enabled by default. + # addressTag = true|false ` func (p *Postgresql) SampleConfig() string { @@ -184,13 +188,16 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error { dbname.WriteString("postgres") } - var tagAddress string - tagAddress, err = p.SanitizedAddress() - if err != nil { - return err - } + tags := map[string]string{"db": dbname.String()} - tags := map[string]string{"server": tagAddress, "db": dbname.String()} + if p.AddressTag { + var tagAddress string + tagAddress, err = p.SanitizedAddress() + if err != nil { + return err + } + tags["server"] = tagAddress + } fields := make(map[string]interface{}) for col, val := range columnMap { @@ -206,6 +213,6 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error { func init() { inputs.Add("postgresql", func() telegraf.Input { - return &Postgresql{} + return &Postgresql{AddressTag: true} }) } diff --git a/plugins/inputs/postgresql/postgresql_test.go b/plugins/inputs/postgresql/postgresql_test.go index 552b18cdb4b2a..e631c3d1075de 100644 --- a/plugins/inputs/postgresql/postgresql_test.go +++ b/plugins/inputs/postgresql/postgresql_test.go @@ -102,6 +102,49 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { assert.Equal(t, "postgres", point.Tags["db"]) } +func TestPostgresqlTagsMetricsWithServerTag(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + address := fmt.Sprintf("host=%s user=postgres sslmode=disable", + testutil.GetLocalHost()) + p := &Postgresql{ + Address: address, + AddressTag: true, + } + + var acc testutil.Accumulator + + err := p.Gather(&acc) + require.NoError(t, err) + + point, ok := acc.Get("postgresql") + require.True(t, ok) + fmt.Println(point.Tags) + assert.Equal(t, address, point.Tags["server"]) +} + +func TestPostgresqlTagsMetricsWithoutServerTag(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()), + AddressTag: false, + } + + var acc testutil.Accumulator + + err := p.Gather(&acc) + require.NoError(t, err) + + point, ok := acc.Get("postgresql") + require.True(t, ok) + assert.Equal(t, "", point.Tags["server"]) +} + func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode") From 4f27667112c9fa0a24b0575266f929d91dfdac70 Mon Sep 17 00:00:00 2001 From: Guillermo Gonzalez Date: Wed, 30 Mar 2016 10:17:40 -0300 Subject: [PATCH 2/2] use address_tag in config --- plugins/inputs/postgresql/postgresql.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/postgresql/postgresql.go b/plugins/inputs/postgresql/postgresql.go index 2c6487488ad4f..aae46081c4f27 100644 --- a/plugins/inputs/postgresql/postgresql.go +++ b/plugins/inputs/postgresql/postgresql.go @@ -20,7 +20,7 @@ type Postgresql struct { OrderedColumns []string AllColumns []string sanitizedAddress string - AddressTag bool + AddressTag bool `toml:"address_tag"` } var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true} @@ -45,7 +45,7 @@ var sampleConfig = ` # databases = ["app_production", "testing"] ## Toggle adding a server:canonicalizedAddress tag to the measurment. If not ## specified, it's enabled by default. - # addressTag = true|false + # address_tag = true|false ` func (p *Postgresql) SampleConfig() string {