prometheus-community / postgres_exporter

A PostgreSQL metric exporter for Prometheus
Apache License 2.0
2.72k stars 725 forks source link

PG_EXPORTER_AUTO_DISCOVER_DATABASES can not be used with unix socket host #476

Open M1ha-Shvn opened 3 years ago

M1ha-Shvn commented 3 years ago

Hi. My goal is to use /var/run/postgresql/ as DSN host with PG_EXPORTER_AUTO_DISCOVER_DATABASES enabled. But I can't figure out correct way to do this, seems a bug.

First attempt: DSN

Readme offers such DSN: DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable" But when I use it I get errors for non default databases:

Feb  5 13:28:59 ru-db-pg-shard-data-01 postgres_exporter[2437731]: time="2021-02-05T13:28:59+03:00" level=error msg="Error opening connection to database (benchmark_main): malformed dsn \"benchmark_main\"" source="postgres_exporter.go:1474"
Feb  5 13:29:05 ru-db-pg-shard-data-01 postgres_exporter[2437731]: time="2021-02-05T13:29:05+03:00" level=error msg="Error opening connection to database (carrot_shard_1): malformed dsn \"carrot_shard_1\"" source="postgres_exporter.go:1474"

Looks like databases found by auto discover are forming wrong dsn.

Second attempt: URI

Correct equal URI as recomended in PostgreSQL docs: DATA_SOURCE_NAME="postgresql://postgres@/postgres?host=/var/run/postgresql&sslmode=disable"

But library ignores / symbol after @. It is important as it means I set database name, not host. URI from logs looks like: "postgresql://postgres@carrot_shard_1?host=/var/run/postgresql&sslmode=disable"

Logs:

Feb  5 13:41:21 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:21+03:00" level=info msg="Established new database connection to \"/var/run/postgresql:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:21 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:21+03:00" level=info msg="Semantic Version Changed on \"/var/run/postgresql:5432\": 0.0.0 -> 9.6.18" source="postgres_exporter.go:1405"
Feb  5 13:41:21 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:21+03:00" level=info msg="Established new database connection to \"benchmark_main:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:22 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:22+03:00" level=info msg="Established new database connection to \"benchmark_main:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:24 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:24+03:00" level=info msg="Established new database connection to \"benchmark_main:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:27 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:27+03:00" level=error msg="Error opening connection to database (postgresql://postgres:PASSWORD_REMOVED@benchmark_main?host=/var/run/postgresql&sslmode=disable): dial tcp: lookup benchmark_main on 127.0.0.53:53: server misbehaving" source="postgres_exporter.go:1474"
Feb  5 13:41:27 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:27+03:00" level=info msg="Established new database connection to \"carrot_shard_1:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:28 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:28+03:00" level=info msg="Established new database connection to \"carrot_shard_1:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:30 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:30+03:00" level=info msg="Established new database connection to \"carrot_shard_1:5432\"." source="postgres_exporter.go:878"
Feb  5 13:41:33 ru-db-pg-shard-data-01 postgres_exporter[2452741]: time="2021-02-05T13:41:33+03:00" level=error msg="Error opening connection to database (postgresql://postgres:PASSWORD_REMOVED@carrot_shard_1?host=/var/run/postgresql&sslmode=disable): dial tcp: lookup carrot_shard_1 on 127.0.0.53:53: server misbehaving" source="postgres_exporter.go:1474"
saifulmuhajir commented 3 years ago

I am using this and working for me:

DATA_SOURCE_URI="postgres@:5432/postgres?host=/var/run/postgresql"

The port has to be there and cannot be empty, for some reasons.

Kronkron52 commented 2 years ago

hi [Unit]

Description=Prometheus PostgreSQL Exporter

After=network.target

[Service]

Type=simple

Restart=always

User=postgres

Group=postgres

Environment=DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable"

EnvironmentFile=/var/lib/pgsql/11/env

ExecStart=/usr/local/bin/postgres_exporter

[Install]

WantedBy=multi-user.target

Внутри файла env : DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable"