fix: Normalize unix socket path (#9554)

This commit is contained in:
Alexander Krantz 2021-08-03 14:20:02 -07:00 committed by GitHub
parent 0cf37a91bc
commit 9bb274b45a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 9 deletions

View File

@ -97,6 +97,8 @@ type Service struct {
IsPgBouncer bool
}
var socketRegexp = regexp.MustCompile(`/\.s\.PGSQL\.\d+$`)
// Start starts the ServiceInput's service, whatever that may be
func (p *Service) Start(telegraf.Accumulator) (err error) {
const localhost = "host=localhost sslmode=disable"
@ -105,23 +107,23 @@ func (p *Service) Start(telegraf.Accumulator) (err error) {
p.Address = localhost
}
connectionString := p.Address
connConfig, err := pgx.ParseConfig(p.Address)
if err != nil {
return err
}
// Remove the socket name from the path
connConfig.Host = socketRegexp.ReplaceAllLiteralString(connConfig.Host, "")
// Specific support to make it work with PgBouncer too
// See https://github.com/influxdata/telegraf/issues/3253#issuecomment-357505343
if p.IsPgBouncer {
// Remove DriveConfig and revert it by the ParseConfig method
// See https://github.com/influxdata/telegraf/issues/9134
d, err := pgx.ParseConfig(p.Address)
if err != nil {
return err
}
d.PreferSimpleProtocol = true
connectionString = stdlib.RegisterConnConfig(d)
connConfig.PreferSimpleProtocol = true
}
connectionString := stdlib.RegisterConnConfig(connConfig)
if p.DB, err = sql.Open("pgx", connectionString); err != nil {
return err
}