diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index 2e2489ba6..d78dd51f2 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -4,21 +4,23 @@ import ( "bytes" "database/sql" "fmt" + "regexp" "strings" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" - _ "github.com/lib/pq" + "github.com/lib/pq" ) type Postgresql struct { - Address string - Databases []string - OrderedColumns []string - AllColumns []string - AdditionalTags []string - Query []struct { + Address string + Databases []string + OrderedColumns []string + AllColumns []string + AdditionalTags []string + sanitizedAddress string + Query []struct { Sqlquery string Version int Withdbname bool @@ -179,6 +181,23 @@ type scanner interface { Scan(dest ...interface{}) error } +var passwordKVMatcher, _ = regexp.Compile("password=\\S+ ?") + +func (p *Postgresql) SanitizedAddress() (_ string, err error) { + var canonicalizedAddress string + if strings.HasPrefix(p.Address, "postgres://") || strings.HasPrefix(p.Address, "postgresql://") { + canonicalizedAddress, err = pq.ParseURL(p.Address) + if err != nil { + return p.sanitizedAddress, err + } + } else { + canonicalizedAddress = p.Address + } + p.sanitizedAddress = passwordKVMatcher.ReplaceAllString(canonicalizedAddress, "") + + return p.sanitizedAddress, err +} + func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error { var columnVars []interface{} var dbname bytes.Buffer @@ -211,10 +230,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 + } + // Process the additional tags tags := map[string]string{} - tags["server"] = p.Address + tags["server"] = tagAddress tags["db"] = dbname.String() fields := make(map[string]interface{})