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

fix: Normalize PostgreSQL unix socket path #9554

Merged
merged 1 commit into from
Aug 3, 2021
Merged
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
20 changes: 11 additions & 9 deletions plugins/inputs/postgresql/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down