Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional postgres server/address tags #1026

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions plugins/inputs/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Postgresql struct {
OrderedColumns []string
AllColumns []string
sanitizedAddress string
AddressTag bool `toml:"address_tag"`
}

var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
Expand All @@ -43,6 +44,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.
# address_tag = true|false
`

func (p *Postgresql) SampleConfig() string {
Expand Down Expand Up @@ -185,13 +189,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 {
Expand All @@ -207,6 +214,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}
})
}
43 changes: 43 additions & 0 deletions plugins/inputs/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down