Skip to content

Export query itself together with queryId in stat_statement metrics #940

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

Merged

Conversation

Delorien84
Copy link
Contributor

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

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.

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.

@Korede-TA
Copy link

@SuperQ hey team, can this get merged soon? We would find this feature really useful in building out our own monitoring stack.. it's a major usability improvement!

@SuperQ
Copy link
Contributor

SuperQ commented May 16, 2025

This currently can not be merged due to conflicts and the tests not passing.

@Delorien84 Delorien84 force-pushed the feature/stat_statement_query branch 2 times, most recently from 8df1d36 to 9d658d8 Compare May 16, 2025 08:07
@Delorien84
Copy link
Contributor Author

I have rebased to current master

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 <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
@Delorien84 Delorien84 force-pushed the feature/stat_statement_query branch from 9d658d8 to c99c749 Compare May 16, 2025 08:10
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.

Minor nits, otherwise LGTM.

Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
Signed-off-by: Jakub Štiller <[email protected]>
@Delorien84 Delorien84 force-pushed the feature/stat_statement_query branch from 821d30b to 7950d3c Compare May 16, 2025 08:34
@SuperQ SuperQ merged commit de42ef7 into prometheus-community:master May 16, 2025
11 checks passed
@SuperQ
Copy link
Contributor

SuperQ commented May 16, 2025

Thanks! Sorry for the long review process.

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
6 participants