-
Notifications
You must be signed in to change notification settings - Fork 834
add pg_stat_statements plans #1315
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
Open
jfcoz
wants to merge
1
commit into
prometheus-community:master
Choose a base branch
from
jfcoz:feat/pg_statements_plans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−38
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,18 @@ func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) { | |
| } | ||
|
|
||
| var ( | ||
| statStatementsPlansTotal = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statStatementsSubsystem, "plans_total"), | ||
| "Number of times planned", | ||
| []string{"user", "datname", "queryid"}, | ||
| prometheus.Labels{}, | ||
| ) | ||
| statStatementsPlansSecondsTotal = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statStatementsSubsystem, "plans_seconds_total"), | ||
| "Total time spent in planning the statement, in seconds", | ||
| []string{"user", "datname", "queryid"}, | ||
| prometheus.Labels{}, | ||
| ) | ||
| statStatementsCallsTotal = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statStatementsSubsystem, "calls_total"), | ||
| "Number of times executed", | ||
|
|
@@ -159,6 +171,8 @@ const ( | |
| pg_database.datname, | ||
| pg_stat_statements.queryid, | ||
| %s | ||
| 0 as plans_total, | ||
| 0 as plans_seconds_total, | ||
|
Comment on lines
+174
to
+175
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not emitting the metric at all sounds better than a 0, people might misinterpret this as the table actually returning 0. |
||
| pg_stat_statements.calls as calls_total, | ||
| pg_stat_statements.total_time / 1000.0 as seconds_total, | ||
| pg_stat_statements.rows as rows_total, | ||
|
|
@@ -182,6 +196,8 @@ const ( | |
| pg_database.datname, | ||
| pg_stat_statements.queryid, | ||
| %s | ||
| pg_stat_statements.plans as plans_total, | ||
| pg_stat_statements.total_plan_time / 1000.0 as plans_seconds_total, | ||
| pg_stat_statements.calls as calls_total, | ||
| pg_stat_statements.total_exec_time / 1000.0 as seconds_total, | ||
| pg_stat_statements.rows as rows_total, | ||
|
|
@@ -205,6 +221,8 @@ const ( | |
| pg_database.datname, | ||
| pg_stat_statements.queryid, | ||
| %s | ||
| pg_stat_statements.plans as plans_total, | ||
| pg_stat_statements.total_plan_time / 1000.0 as plans_seconds_total, | ||
| pg_stat_statements.calls as calls_total, | ||
| pg_stat_statements.total_exec_time / 1000.0 as seconds_total, | ||
| pg_stat_statements.rows as rows_total, | ||
|
|
@@ -259,13 +277,13 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc | |
| defer rows.Close() | ||
| for rows.Next() { | ||
| var user, datname, queryid, statement sql.NullString | ||
| var callsTotal, rowsTotal sql.NullInt64 | ||
| var secondsTotal, blockReadSecondsTotal, blockWriteSecondsTotal sql.NullFloat64 | ||
| var plansTotal, callsTotal, rowsTotal sql.NullInt64 | ||
| var plansSecondsTotal, secondsTotal, blockReadSecondsTotal, blockWriteSecondsTotal sql.NullFloat64 | ||
| var columns []any | ||
| if c.includeQueryStatement { | ||
| columns = []any{&user, &datname, &queryid, &statement, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal} | ||
| columns = []any{&user, &datname, &queryid, &statement, &plansTotal, &plansSecondsTotal, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal} | ||
| } else { | ||
| columns = []any{&user, &datname, &queryid, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal} | ||
| columns = []any{&user, &datname, &queryid, &plansTotal, &plansSecondsTotal, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal} | ||
| } | ||
| if err := rows.Scan(columns...); err != nil { | ||
| return err | ||
|
|
@@ -292,6 +310,28 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc | |
| } | ||
| seen[key] = struct{}{} | ||
|
|
||
| plansTotalMetric := 0.0 | ||
| if plansTotal.Valid { | ||
| plansTotalMetric = float64(plansTotal.Int64) | ||
| } | ||
| ch <- prometheus.MustNewConstMetric( | ||
| statStatementsPlansTotal, | ||
| prometheus.CounterValue, | ||
| plansTotalMetric, | ||
| userLabel, datnameLabel, queryidLabel, | ||
| ) | ||
|
|
||
| plansSecondsTotalMetric := 0.0 | ||
| if plansSecondsTotal.Valid { | ||
| plansSecondsTotalMetric = plansSecondsTotal.Float64 | ||
| } | ||
| ch <- prometheus.MustNewConstMetric( | ||
| statStatementsPlansSecondsTotal, | ||
| prometheus.CounterValue, | ||
| plansSecondsTotalMetric, | ||
| userLabel, datnameLabel, queryidLabel, | ||
| ) | ||
|
|
||
| callsTotalMetric := 0.0 | ||
| if callsTotal.Valid { | ||
| callsTotalMetric = float64(callsTotal.Int64) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
queryID looks like a label with unbounded cardinality. Could we drop it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need it, for example to get the ratio of calls by plan and find queries which are badly used (for example prepare + bind inside a loop)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work well with Prometheus though 😅.
Maybe that could become an exemplar label in the histogram?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t understand the problem. It’s already the same in all the other metrics from pg_stat_statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I probably should have said this from the beginning. I'm not a maintainer for this repository! If the maintainers are ok with it, that's what counts
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant by "this doesn't work well with Prometheus" is that a single query will have a single ID and generate a single timeseries; another query will get a different ID and therefore become a brand-new timeseries.
All those unique IDs will become an entry in Prometheus's Posting index (good blog in case you'd like to read more). A gigantic posting index will make PromQL queries slower and increase resource usage