diff --git a/plugins/inputs/pgbouncer/pgbouncer.go b/plugins/inputs/pgbouncer/pgbouncer.go index 999d27698..29f4fa904 100644 --- a/plugins/inputs/pgbouncer/pgbouncer.go +++ b/plugins/inputs/pgbouncer/pgbouncer.go @@ -103,9 +103,9 @@ func (p *PgBouncer) accRow(row scanner, columns []string) (map[string]string, ma return nil, nil, fmt.Errorf("writing database name failed: %w", err) } } else { - _, err := dbname.WriteString("postgres") + _, err := dbname.WriteString("pgbouncer") if err != nil { - return nil, nil, fmt.Errorf("writing 'postgres' failed: %w", err) + return nil, nil, fmt.Errorf("writing 'pgbouncer' failed: %w", err) } } diff --git a/plugins/inputs/postgresql/postgresql.go b/plugins/inputs/postgresql/postgresql.go index 835d69d50..8e7c4dc5c 100644 --- a/plugins/inputs/postgresql/postgresql.go +++ b/plugins/inputs/postgresql/postgresql.go @@ -120,8 +120,13 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator, columns []str columnVars = append(columnVars, columnMap[columns[i]]) } + tagAddress, err := p.SanitizedAddress() + if err != nil { + return err + } + // deconstruct array of variables and send to Scan - err := row.Scan(columnVars...) + err = row.Scan(columnVars...) if err != nil { return err @@ -139,15 +144,13 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator, columns []str } } } else { - if _, err := dbname.WriteString("postgres"); err != nil { + database, err := p.GetConnectDatabase(tagAddress) + if err != nil { + return err + } + if _, err := dbname.WriteString(database); err != nil { return err } - } - - var tagAddress string - tagAddress, err = p.SanitizedAddress() - if err != nil { - return err } tags := map[string]string{"server": tagAddress, "db": dbname.String()} diff --git a/plugins/inputs/postgresql/service.go b/plugins/inputs/postgresql/service.go index 07a49d285..a774e1b3c 100644 --- a/plugins/inputs/postgresql/service.go +++ b/plugins/inputs/postgresql/service.go @@ -174,3 +174,17 @@ func (p *Service) SanitizedAddress() (sanitizedAddress string, err error) { return kvMatcher.ReplaceAllString(canonicalizedAddress, ""), nil } + +// GetConnectDatabase utility function for getting the database to which the connection was made +func (p *Service) GetConnectDatabase(connectionString string) (string, error) { + connConfig, err := pgx.ParseConfig(connectionString) + if err != nil { + return "", fmt.Errorf("connection string parsing failed: %w", err) + } + + if len(connConfig.Database) != 0 { + return connConfig.Database, nil + } + + return "postgres", nil +} diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index 43090060f..32fee3fcc 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -186,6 +186,10 @@ func (p *Postgresql) accRow(measName string, row scanner, acc telegraf.Accumulat columnVars = append(columnVars, columnMap[columns[i]]) } + if tagAddress, err = p.SanitizedAddress(); err != nil { + return err + } + // deconstruct array of variables and send to Scan if err = row.Scan(columnVars...); err != nil { return err @@ -199,18 +203,22 @@ func (p *Postgresql) accRow(measName string, row scanner, acc telegraf.Accumulat return err } default: - if _, err := dbname.WriteString("postgres"); err != nil { + database, err := p.GetConnectDatabase(tagAddress) + if err != nil { + return err + } + if _, err := dbname.WriteString(database); err != nil { return err } } } else { - if _, err := dbname.WriteString("postgres"); err != nil { + database, err := p.GetConnectDatabase(tagAddress) + if err != nil { + return err + } + if _, err := dbname.WriteString(database); err != nil { return err } - } - - if tagAddress, err = p.SanitizedAddress(); err != nil { - return err } // Process the additional tags