prometheus-community / postgres_exporter

A PostgreSQL metric exporter for Prometheus
Apache License 2.0
2.83k stars 745 forks source link

Export query itself together with queryId in stat_statement metrics #940

Open Delorien84 opened 1 year ago

Delorien84 commented 1 year ago

The feature must be enabled via flag or via environment variable.

The query is not added to every metrics, but instead of new metric stat_statement_query_id is introduced that contains mapping between queryId and query.

Fix #813

jrx-sjg commented 8 months ago

Can someone review and approve this? It will be a very nice add.

mattb18 commented 7 months ago

Also interested in this, it would make dashboards much more digestible for developers.

tpai commented 7 months ago

For someone looking for long queries, I am now using this as a workaround.

version: '3'
services:
  query-exporter:
    image: adonato/query-exporter:2.9.2
    environment:
      - PG_DATABASE_DSN=postgresql://dbuser:dbpass@dbhost:5432/postgres?sslmode=disable
    network_mode: host
    volumes:
      - ./query-config.yml:/config.yaml
databases:
  postgres:
    dsn: env:PG_DATABASE_DSN

metrics:
  long_queries_calls:
    type: gauge
    labels: [queryid, query]
  long_queries_rows:
    type: gauge
    labels: [queryid, query]
  long_queries_min_exec_time:
    type: gauge
    labels: [queryid, query]
  long_queries_max_exec_time:
    type: gauge
    labels: [queryid, query]
  long_queries_avg_exec_time:
    type: gauge
    labels: [queryid, query]

queries:
  long_queries_avg_exec_time:
    databases: [postgres]
    metrics:
      - long_queries_calls
      - long_queries_rows
      - long_queries_min_exec_time
      - long_queries_max_exec_time
      - long_queries_avg_exec_time
    sql: >
      SELECT
          queryid,
          query,
          calls AS long_queries_calls,
          rows AS long_queries_rows,
          min_exec_time AS long_queries_min_exec_time,
          max_exec_time AS long_queries_max_exec_time,
          total_exec_time / calls AS long_queries_avg_exec_time
      FROM
          pg_stat_statements
      WHERE
          calls >= 100
      ORDER BY
          long_queries_avg_exec_time DESC
      LIMIT 10;

I am also waiting for this PR to be merged which is an elegant way.