Back to Skills

promql

Write, validate, and optimize PromQL for Prometheus / Grafana Mimir / Grafana Cloud Metrics. Covers `rate` vs `irate` vs `increase`, label matchers and regex, `sum / avg / topk / by / without` aggregation, classic + native `histogram_quantile`, ratios with divide-by-zero guards, `absent` / `changes` for staleness, time offsets and `predict_linear`, recording-rule naming, SLO + burn-rate math, and a cardinality-hunting playbook. Use when writing a metric query, fixing wrong p95s, building an erro

172stars13forksUpdated 7/2/2026

Security Assessment

Safe(100/100)
Security Score100/100

About promql

A working reference for writing, validating, and optimizing PromQL against Prometheus, Grafana Mimir, and Grafana Cloud Metrics. It addresses the queries engineers actually reach for, such as error rates, p95 and p99 latency, and sums by service, as well as debugging wrong percentiles, building error-budget alerts, diagnosing slow queries, and finding the label that blew up cardinality, even when the request never names PromQL explicitly. A stated golden rule anchors much of it: rate() and increase() need a range vector at least four times the scrape interval, so a 60-second scrape should use a 5m window at minimum.

Common workflows are laid out as copyable recipes. Writing and validating a query points an endpoint variable at Prometheus or Mimir (with basic auth for Grafana Cloud), sketches the expression, then checks via /api/v1/query that the status is success and results are non-empty, falling back to /api/v1/series to confirm the metric and labels exist. Ready-made patterns include per-status request rate (aggregating after rate), p95 latency that keeps le in the inner aggregation of histogram_quantile, and an error-rate ratio with a divide-by-zero guard.

A third workflow converts a slow dashboard query into a recording rule with a proper name, verifies the new metric, and swaps it into the panel. A common-bugs section maps symptoms to causes: histogram_quantile returning NaN from a missing by (le); no-data from a nonexistent metric or too-short window; wrong rate magnitude from aggregating a counter before rate(); and query timeouts from excessive series, addressed with topk, recording rules, and dropping high-cardinality labels. A fuller pattern library covering recording rules, SLO burn-rate math, offsets, native histograms, and a cardinality-hunting playbook lives in references/patterns.md.

FAQ

When do rate() and increase() need a wider window?

They require a range vector of at least four times the scrape interval, so with a 60-second scrape you should use a 5m window as a minimum, or the result will be unreliable.

Why does histogram_quantile return NaN?

Because le was dropped from the inner aggregation. The p95 and p99 patterns must keep le in the by clause, for example sum(rate(..._bucket[5m])) by (le, service).

Which backends does it target?

Prometheus, Grafana Mimir, and Grafana Cloud Metrics, queried via /api/v1/query or Grafana Explore, with basic auth added for Grafana Cloud endpoints.

How do I fix a slow or timing-out query?

Reduce the series count with topk, move the expression into a recording rule, and drop high-cardinality labels; the cardinality-hunting playbook is in references/patterns.md.

How do I guard an error-rate ratio against divide-by-zero?

Divide by the total rate gated with greater-than-zero, so the denominator drops empty results instead of producing errors, for example sum(rate(5xx[5m])) / (sum(rate([5m])) > 0).

All Files

2 files
SKILL.md4.4 KB
View
references/patterns.md4.3 KB
View

Install promql

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill

Repository

grafana/skills