prometheus-community / postgres_exporter

A PostgreSQL metric exporter for Prometheus
Apache License 2.0
2.79k stars 737 forks source link

Multi-target support issue with UNIX socket path encoding #1058

Open caseyandgina opened 2 months ago

caseyandgina commented 2 months ago

I'd like to collect metrics like analyze/vacuum stats for tables in multiple databases, not just the one in the primary DSN. I'm trying to use multi-target support for this, but running into an issue when using a UNIX socket for the connection, as the slashes in the path aren't being decoded correctly.

For example, the following URL:

http://localhost:9187/probe?target=user=app_prometheus_postgres_exporter%20dbname=postgres%20host=%2Frun%2Fpostgresql%20port=5432

Results in:

caller=probe.go:81 level=error target="user=app_prometheus_postgres_exporter dbname=postgres host=/run/postgresql port=5432" msg="Error opening connection to database" err="error querying postgresql version: parse \"postgresql://app_prometheus_postgres_exporter:@%2Frun%2Fpostgresql:5432?dbname=postgres\": invalid URL escape \"%2F\""
caller=postgres_exporter.go:682 level=error err="Error opening connection to database (could not parse DATA_SOURCE_NAME): parse \"postgresql://app_prometheus_postgres_exporter:@%2Frun%2Fpostgresql:5432?dbname=postgres\": invalid URL escape \"%2F\""

If there's a better/simpler way, please let me know.

caseyandgina commented 2 months ago

With this URL: http://localhost:9187/probe?target=app_prometheus_postgres_exporter@%2Frun%2Fpostgresql:5432/postgres

It interprets the first %2F as a separator and everything after it as a db name, trying to use a TCP connection instead:

caller=probe.go:81 level=error target=app_prometheus_postgres_exporter@/run/postgresql:5432/postgres msg="Error opening connection to database" err="error querying postgresql version: pq: no pg_hba.conf entry for host \"127.0.0.1\", user \"app_prometheus_postgres_exporter\", database \"run/postgresql:5432/postgres\", SSL encryption"
caller=postgres_exporter.go:682 level=error err="Error opening connection to database (postgresql://app_prometheus_postgres_exporter:PASSWORD_REMOVED@/run/postgresql:5432/postgres): pq: no pg_hba.conf entry for host \"127.0.0.1\", user \"app_prometheus_postgres_exporter\", database \"run/postgresql:5432/postgres\", SSL encryption