Skip to content
PromQL Query Patterns

PromQL Query Patterns

Apache-2.0
Repository Docs
markdown Development
promqlprometheusgrafanaobservabilitymonitoringslo

Summary

Grafana's official PromQL skill: write, validate, and optimise Prometheus queries, and hunt down the label that blew up your cardinality.

Features

  • rate vs irate vs increase, and the 4x-scrape-interval rule
  • Aggregation with sum, avg, topk, by and without
  • Classic and native histogram_quantile for correct percentiles
  • Ratio queries with divide-by-zero guards
  • SLO and burn-rate maths for error-budget alerts
  • Cardinality-hunting playbook for slow queries and OOMs
  • Recording-rule naming and dashboard-query migration

Install This Skill

Add this skill to your favorite AI agent in a few steps.

Any AI agent

This skill is plain instructions — it works with any assistant that accepts custom instructions or system prompts.

  1. Copy the skill content with the button below.
  2. Paste it into your agent's instruction file or system prompt (for example AGENTS.md, .cursorrules, or a custom instructions field).
  3. Ask the agent to apply the skill whenever the task matches.

Skill Content

Markdown Content

Copy this content and use it with your preferred AI agent

---
name: promql
license: Apache-2.0
description: 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 error-budget alert, debugging "query is slow", finding the noisy label that blew up cardinality, or migrating a dashboard query to a recording rule — even when the user says "calculate the error rate", "p99 latency", "sum by service", "why is this query slow", or "what's filling Mimir" without naming PromQL.
---

# PromQL Query Patterns

> **Docs**: https://prometheus.io/docs/prometheus/latest/querying/basics/

PromQL returns either an **instant vector**, a **range vector**, or a **scalar**.

**Golden rule:** `rate()` / `increase()` require a range vector ≥ 4× the scrape interval. 60s scrape → use `[5m]` minimum.

## Prerequisites

- A Prometheus / Mimir / Grafana Cloud endpoint to query (`/api/v1/query` or via Grafana Explore)
- The PromQL pattern library in [`references/patterns.md`](references/patterns.md)

## Common Workflows

### 1. Write + validate a query

```bash
# 0. Point at your Prometheus/Mimir. For Grafana Cloud, use the metrics endpoint
#    and add basic auth (-u "<metrics_user>:<token>") to each curl below.
PROM=http://localhost:9090   # or https://prometheus-prod-XX.grafana.net/api/prom

# 1. Sketch the query — for "5xx error rate per service":
EXPR='sum(rate(http_requests_total{status_code=~"5.."}[5m])) by (service)'

# 2. Validate syntax + that the metric/labels exist
curl -sG --data-urlencode "query=${EXPR}" \
  "$PROM/api/v1/query" | jq '.status, (.data.result|length)'
# Expect: "success" and result count > 0. If 0 — check label spelling and scrape activity:
curl -sG --data-urlencode "match[]=http_requests_total" "$PROM/api/v1/series" | jq '.data | length'

# 3. Sanity-check the magnitude — open Grafana Explore, paste the expr,
#    confirm the values look right against a known ground truth (k6 run, log count, etc.)
```

### 2. Common patterns to copy

**Per-status request rate** (aggregate AFTER rate):

```promql
sum(rate(http_requests_total{job="api"}[5m])) by (status_code)
```

**p95 latency** (must keep `le` in the inner aggregation):

```promql
histogram_quantile(0.95,
  sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service))
```

**Error rate with divide-by-zero guard:**

```promql
sum(rate(http_requests_total{status_code=~"5.."}[5m]))
  / (sum(rate(http_requests_total[5m])) > 0)
```

Full library (recording rules, SLO burn-rate, offsets, cardinality hunt, native histograms): [`references/patterns.md`](references/patterns.md).

### 3. Convert a slow dashboard query into a recording rule

```yaml
# 1. Pick the slow expression, give it a recording-rule name
groups:
  - name: http_request_rates
    interval: 1m
    rules:
      - record: job:http_request_duration_p95:rate5m
        expr: |
          histogram_quantile(0.95,
            sum(rate(http_request_duration_seconds_bucket[5m])) by (le, job))
```

```bash
# 2. After rules load, verify the new metric exists
curl -sG --data-urlencode "query=job:http_request_duration_p95:rate5m" \
  "$PROM/api/v1/query" | jq '.data.result | length'   # → > 0

# 3. Verify it matches the original expression for at least one sample window
# (Both queries should produce the same value at the same timestamp.)

# 4. Replace the dashboard panel expression with the recording-rule metric.
```

## Common bugs

- `histogram_quantile` returns NaN → forgot `by (le)` in the inner aggregation
- "No data" → check the metric exists (`/api/v1/series`) and the window ≥ 4× scrape interval
- Wrong rate magnitude → counter was aggregated before `rate()` (always `rate()` first)
- Query timeout → series count too high; use `topk(...)` + a recording rule + drop high-cardinality labels (see [`references/patterns.md`](references/patterns.md))

## Resources

- [PromQL basics](https://prometheus.io/docs/prometheus/latest/querying/basics/)
- [Operators](https://prometheus.io/docs/prometheus/latest/querying/operators/)
- [Functions](https://prometheus.io/docs/prometheus/latest/querying/functions/)
- [Grafana Mimir](https://grafana.com/docs/mimir/latest/)

Usage Instructions

Learn how to use this skill with different AI agents.

Claude Desktop
claude plugin marketplace add grafana/skills
claude plugin install grafana-core@grafana-skills

Example Usage

Our checkout p95 dashboard looks wrong after we moved to native histograms — rewrite the panel query and add a 2%/1h burn-rate alert for the 99.9% availability SLO.

Description

Prometheus queries fail in quiet ways. A rate() over too short a window returns nothing instead of erroring, a p95 computed from the wrong histogram type is off by an order of magnitude, and a ratio divides by zero the moment traffic drops — none of which announce themselves. This skill from Grafana Labs gives an agent the query patterns that make those failures visible before they reach a dashboard.

What it covers

The core is the set of distinctions that PromQL punishes you for getting wrong: rate versus irate versus increase, label matchers and regex behaviour, and aggregation with sum, avg, topk, by, and without. On top of that it covers classic and native histogram_quantile, ratio expressions with divide-by-zero guards, absent and changes for staleness detection, time offsets and predict_linear for trend work, recording-rule naming conventions, and the SLO and burn-rate arithmetic behind error-budget alerts.

It also carries a cardinality-hunting playbook — the workflow for finding which label is generating the series explosion behind a slow query, an OOMing Prometheus, or an unexpected Grafana Cloud bill.

The rule it enforces first

rate() and increase() need a range vector of at least four scrape intervals. On a 60-second scrape that means [5m] minimum. Most "my query returns no data" reports resolve to this one line, which is why the skill states it as a golden rule up front.

Where it applies

The queries work against Prometheus, Grafana Mimir, and Grafana Cloud Metrics. The skill is written to trigger on the way people actually phrase these requests — "calculate the error rate", "p99 latency", "sum by service", "why is this query slow", "what's filling Mimir" — rather than requiring anyone to say the word PromQL.

Part of the grafana-core plugin in Grafana's public skills marketplace, alongside dashboarding, alerting and IRM, Alloy, Beyla, and OpenTelemetry skills.

Related Skills

Skill: Superpowers

by Jesse Vincent

New

An agentic software-development methodology: composable skills that push a coding agent through spec, plan, TDD and review instead of straight into code.

Development

Skill: SkillHone

by Tencent

New

Tencent's skill-evolution harness: it rewrites a whole skill folder — SKILL.md, scripts and references together — and lands every decision as a real Git issue, PR and wiki entry you can review.

Development

Skill: OpenSearch Launchpad

by OpenSearch Project

New

Take an OpenSearch search application from requirements to a running cluster — BM25, dense and sparse vectors, hybrid retrieval, agentic search and RAG, with relevance evaluation built in.

Development
2 views

Auth0's official agent skill: a router that detects your framework and intent, then loads the right Auth0 guidance for login, MFA, Organizations, tenant audits, debugging or provider migration.

Development
Browse all skills →