prometheus-community / pgbouncer_exporter

Prometheus exporter for PgBouncer
MIT License
140 stars 49 forks source link

Exporter can't connect to pgbouncer's unix socket #135

Closed LexVar closed 10 months ago

LexVar commented 10 months ago

I ran into an issue setting up the exporter in the same VM where pgbouncer is running. I have a pgbouncer_exporter user setup with a password to connect to the pgbouncer DB locally through pgbouncer's unix socket.

I can connect to the pgbouncer DB with all these 3 commands:

pgbouncer_exporter@db-host:~$ psql postgres://pgbouncer_exporter:****@%2Fvar%2Frun%2Fpostgresql:6432/pgbouncer
pgbouncer_exporter@db-host:~$ psql postgres://pgbouncer_exporter:****@[/var/run/postgresql]:6432/pgbouncer
pgbouncer_exporter@db-host:~$ psql postgres://pgbouncer_exporter:****@:6432/pgbouncer

But none of the 3 connection strings work in the pgbouncer_exporter:

pgbouncer_exporter@db-host:~$ /usr/local/bin/pgbouncer_exporter --pgBouncer.connectionString="postgres://pgbouncer_exporter:****@/var/run/postgresql:6432/pgbouncer"ts=2023-12-01T12:19:52.371Z caller=collector.go:138 level=error msg="error setting up DB connection" err="error pinging pgbouncer: pq: password authentication failed for user \"pgbouncer_exporter\""
pgbouncer_exporter@db-host:~$ /usr/local/bin/pgbouncer_exporter --pgBouncer.connectionString="postgres://pgbouncer_exporter:****@%2Fvar%2Frun%2Fpostgresql:6432/pgbouncer"
ts=2023-12-01T12:20:16.842Z caller=collector.go:138 level=error msg="error setting up DB connection" err="error pinging pgbouncer: parse \"postgres://pgbouncer_exporter:****@%2Fvar%2Frun%2Fpostgresql:6432/pgbouncer\": invalid URL escape \"%2F\""
pgbouncer_exporter@db-host:~$ /usr/local/bin/pgbouncer_exporter --pgBouncer.connectionString="postgres://pgbouncer_exporter:****@[/var/run/postgresql]:6432/pgbouncer"
ts=2023-12-01T12:20:19.659Z caller=collector.go:138 level=error msg="error setting up DB connection" err="error pinging pgbouncer: parse \"postgres://pgbouncer_exporter:****@[/var/run/postgresql]:6432/pgbouncer\": missing ']' in host"

I can see by default if the host is not specified, pgbouncer_exporter will use localhost ::1 instead of using the unix socket. PGbouncer logs from the login failure:

Dec 01 10:55:38 db-host pgbouncer[472115]: C-0x563c122c7050: (nodb)/(nouser)@[::1]:45336 closing because: SSL required (age=0s)
Dec 01 10:55:38 db-host pgbouncer[472115]: C-0x563c122c7050: (nodb)/(nouser)@[::1]:45336 pooler error: SSL required

So then I have to specify the unix socket directory. How can I do this with the exporter?

sdvcrx commented 10 months ago

Try:

pgbouncer_exporter --pgBouncer.connectionString='postgres:///pgbouncer?host=/var/run/postgresql/&port=6432&sslmode=disable'

Ref: https://github.com/lib/pq/issues/796

LexVar commented 10 months ago

Thank you, that worked like a charm. I just had to add the user: --pgBouncer.connectionString='postgres:///pgbouncer?host=/var/run/postgresql/&port=6432&user=pgbouncer&sslmode=disable' I will mark the issue as done