prometheus-community / postgres_exporter

A PostgreSQL metric exporter for Prometheus
Apache License 2.0
2.81k stars 742 forks source link

To many connections open when scrape different instances from one exporter setup #531

Open kadaffy opened 3 years ago

kadaffy commented 3 years ago

What did you do? Configuring a postgres-exporter instance using docker-compose to scrape metrics from different instances open to many connections

postgres=# select count(*), state, usename from pg_stat_activity group by 2,3;
 count | state  |      usename
-------+--------+-------------------
     4 |        |
     2 | active | replication
     1 | active | postgres
     1 |        | postgres
     2 | idle   | postgres
    50 | idle   | postgres_exporter
(6 rows)

This is the DATASOURCE definition:

environment:
      DATA_SOURCE_NAME: "postgresql://postgres_exporter:XXXX@10.0.2.25:5432/postgres?sslmode=disable,postgresql://postgres_exporter:XXXX@10.0.2.26:5432/postgres?sslmode=disable,postgresql://postgres_exporter:XXXX@10.0.2.27:5432/postgres?sslmode=disable"

docker-compose log:

postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Established new database connection to \"10.0.2.25:5432\"." source="postgres_exporter.go:983"
postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Semantic Version Changed on \"10.0.2.25:5432\": 0.0.0 -> 10.15.0" source="postgres_exporter.go:1552"
postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Established new database connection to \"10.0.2.26:5432\"." source="postgres_exporter.go:983"
postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Semantic Version Changed on \"10.0.2.26:5432\": 0.0.0 -> 10.15.0" source="postgres_exporter.go:1552"
postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Established new database connection to \"10.0.2.27:5432\"." source="postgres_exporter.go:983"
postgres-exporter    | time="2021-05-10T10:22:14Z" level=info msg="Semantic Version Changed on \"10.0.2.27:5432\": 0.0.0 -> 10.15.0" source="postgres_exporter.go:1552"

And if you scrape from only one instance, it does not open such many connections:

environment:
      DATA_SOURCE_NAME: "postgresql://postgres_exporter:XXXX@10.0.2.25:5432/postgres?sslmode=disable"

Connections:

postgres=# select count(*), state, usename from pg_stat_activity group by 2,3;
 count | state  |      usename
-------+--------+-------------------
     4 |        |
     2 | active | replication
     1 | active | postgres
     1 |        | postgres
     2 | idle   | postgres
     1 | idle   | postgres_exporter
(6 rows)

docker-compose log:

postgres-exporter    | time="2021-05-10T10:26:47Z" level=info msg="Established new database connection to \"10.0.2.25:5433\"." source="postgres_exporter.go:983"
postgres-exporter    | time="2021-05-10T10:26:47Z" level=info msg="Semantic Version Changed on \"10.0.2.25:5433\": 0.0.0 -> 10.15.0" source="postgres_exporter.go:1552"

What did you expect to see?

postgres-exporter should not open more than one connection per instance even if you use auto-discover-databases.

Check also https://github.com/prometheus-community/postgres_exporter/issues/436

Environment This is the docker-compose definition:

version: '3'

services:
  postgres-exporter:
    image: quay.io/prometheuscommunity/postgres-exporter
    container_name: postgres-exporter
    environment:
      DATA_SOURCE_NAME: "postgresql://postgres_exporter:XXXX@10.0.2.25:5432/postgres?sslmode=disable,postgresql://postgres_exporter:XXXX@10.0.2.26:5432/postgres?sslmode=disable,postgresql://postgres_exporter:XXXX@10.0.2.27:5432/postgres?sslmode=disable"
      PG_EXPORTER_EXTEND_QUERY_PATH: "/queries/queries.yaml"
      PG_EXPORTER_AUTO_DISCOVER_DATABASES: "true"
      PG_EXPORTER_EXCLUDE_DATABASES: "template0,template1"
      PG_EXPORTER_CONSTANT_LABELS: "cluster_name=my_cluster"
    volumes:
       - ./postgres-exporter/:/queries
    ports:
      - 9187:9187
    networks:
      - monitor

networks:
  monitor:
$ docker images | grep postgres-exporter
quay.io/prometheuscommunity/postgres-exporter   latest    ca5bb120a572   2 months ago   16.9MB
dswarbrick commented 2 years ago

Possible duplicate of #436?