Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export query itself together with queryId in stat_statement metrics #940

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Delorien84
Copy link

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

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 prometheus-community#813

Signed-off-by: Jakub Štiller <stiller@2n.cz>
collector/pg_stat_statements.go Outdated Show resolved Hide resolved
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to how we do this in the mysqld_exporter, the query string should be length limited by default.

Let's follow the mysqld_exporter and limit to 120 chars.

You can do something like LEFT(pg_stat_statements.query, %d) as query.

Jakub Štiller added 2 commits October 24, 2023 16:29
Signed-off-by: Jakub Štiller <stiller@2n.cz>
Signed-off-by: Jakub Štiller <stiller@2n.cz>
Signed-off-by: Jakub Štiller <stiller@2n.cz>
Signed-off-by: Jakub Štiller <stiller@2n.cz>
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@SuperQ SuperQ requested a review from sysadmind December 8, 2023 09:57
@jrx-sjg
Copy link

jrx-sjg commented Mar 4, 2024

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

@mattb18
Copy link

mattb18 commented Apr 11, 2024

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

@tpai
Copy link

tpai commented Apr 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

About adding query on pg_stat_statements.go
5 participants