Skip to content

feat: optional per-query duration and row count metrics#996

Merged
burningalchemist merged 1 commit into
burningalchemist:masterfrom
kurzdigital:feat/per-query-metrics
May 12, 2026
Merged

feat: optional per-query duration and row count metrics#996
burningalchemist merged 1 commit into
burningalchemist:masterfrom
kurzdigital:feat/per-query-metrics

Conversation

@rassie
Copy link
Copy Markdown
Contributor

@rassie rassie commented May 11, 2026

This adds an opt-in global.enable_query_metrics flag. When set, every configured query emits two extra gauges per scrape:

  • query_duration_seconds{query="<name>"} — wall-clock time of the query during the last scrape, including row scanning. Emitted even when the query errors so spikes around failures stay visible.
  • query_rows_returned{query="<name>"} — number of rows the database returned during the last scrape.

Both inherit the same constant labels as up and scrape_duration_seconds (notably target in jobs mode).

Motivation

Issue #303 added debug logging for per-query timing; this exposes the same information as first-class metrics so slow queries can be identified directly from PromQL.

Notes

  • Off by default; also settable via ENABLE_QUERY_METRICS=true.
  • Gauges rather than a histogram to stay within the existing auto-metric pipeline. Each scrape contributes one sample, so quantile_over_time works the same in practice (caveat: biased with min_interval > 0).
  • Unit tests cover both flag states and label wiring.

Disclosure: this change was prepared with the help of an LLM-based coding assistant. All design choices were reviewed and the diff was tested locally before submission.

Adds an opt-in `global.enable_query_metrics` flag. When enabled, every
query emits two extra gauges per scrape:

- `query_duration_seconds{query=...}` — wall-clock time of the last run
- `query_rows_returned{query=...}` — number of rows the database returned

Both inherit the target's constant labels, so they aggregate the same
way as `up` and `scrape_duration_seconds`. The feature is off by default
to keep the existing metric surface unchanged.
@burningalchemist
Copy link
Copy Markdown
Owner

Hi @rassie, thank you for your contribution! 👍 I'll have a look at the PR this week and get back to you. Overall, sounds good! 👍

Copy link
Copy Markdown
Owner

@burningalchemist burningalchemist left a comment

Choose a reason for hiding this comment

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

Looks good to me! 🚀 Thanks again! 👍

@burningalchemist burningalchemist merged commit 2548c3c into burningalchemist:master May 12, 2026
4 checks passed
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.

2 participants