> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Grafana Alerting

> Explains Grafana alert rules including PromQL queries, conditions, evaluation intervals, for duration, labels, annotations, and alert state transitions

This article explains how Grafana-managed alerts operate and how to construct alert rules that reliably surface real problems. It walks through a concrete PromQL example for alerting on error rate, then breaks down the key alert rule components: query, condition, evaluation behavior, labels, annotations, and the alert state machine.

## Example: alerting on error rate (5xx responses)

A common signal is the percentage of 5xx responses over a recent window. In PromQL you can compute a 5-minute error rate like this:

```promql theme={null}
100 * (
  sum(rate(http_requests_total{status=~"5.."}[5m]))
  /
  sum(rate(http_requests_total[5m]))
)
```

Important: when Grafana evaluates an alert rule, the alert query is evaluated at a single, most-recent timestamp and yields a single scalar value (like a stat or gauge). Grafana does not evaluate a time range for the alert condition the way a time-series panel does; it repeatedly samples the query on the evaluation schedule.

## Alert rule components

Below is a concise reference for the pieces that make up a Grafana alert rule.

| Component | Purpose | Example / notes |
| - | -: | - |
| Query | The metric expression to evaluate | `100 * ( sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) )` |
| Condition | The threshold test against the query result | `> 1` (e.g., error rate greater than 1%) |
| Evaluation interval | How often Grafana runs the query | Common value: `1m` (every minute) |
| Duration (`for`) | How long the condition must remain true before firing | Example: `for: 5m` — avoids paging on short spikes |
| Labels | Routing/classification metadata for notifications | e.g., `severity: critical` |
| Annotations | Human-friendly context and links for responders | `summary`, `description`, `dashboard_url` |

![A diagram titled "Grafana Alert Rules" showing a flowchart from "PromQL Expression" to "Condition" to "Evaluation Behavior" and then to Labels and Annotations. A side panel explains evaluation intervals and includes example graphs showing when an alert will or won't fire.](https://mintcdn.com/kodekloud-c4ac6d9a/qM9vA-hQoWNCxcKa/images/Grafana-Prometheus-for-Metrics-Driven-Observability/Alert-Management/Grafana-Alerting/grafana-alert-rules-promql-evaluation-diagram.jpg?fit=max\&auto=format\&n=qM9vA-hQoWNCxcKa\&q=85\&s=03e4dbc9c80bc113752c4cb6b7ca9b2b)

### Conditions and thresholds

The condition is the boolean test that determines if an evaluated scalar should be considered a breach. For example, if your SLO states an error rate \< 1%, use a condition like `> 1`. Conditions are intentionally simple because Grafana evaluates a single scalar per check.

### Evaluation behavior: sampling and the `for` duration

* Evaluation interval: how often Grafana samples the query (for example, every `1m`).
* `for` duration: the length of time the condition must continuously remain true before Grafana transitions the alert to the firing state.

Use the `for` window to suppress noisy, short-lived spikes. For example, choose `evaluation interval: 1m` and `for: 5m` so only sustained breaches cause notifications.

The `for` duration acts as a gate: it prevents alerts from firing on short-lived spikes and reduces alert noise. Balance prompt detection and noise suppression by choosing an appropriate evaluation interval and `for` window (e.g., evaluate every 1 minute and use `for: 5m` for sustained breaches).

## Labels vs. annotations

* Labels: for routing and automated handling. Use labels to determine which escalation policy, PagerDuty service, or Slack channel receives the alert. A common label is `severity` with values like `critical` or `warning`.
* Annotations: for human context. Include templated summaries, descriptions, and dashboard links so responders can quickly assess and act.

Example annotations block:

```yaml theme={null}
annotations:
  summary: "Backend error rate exceeds 1% SLO"
  description: "Error rate: {{ $value | printf \"%.2f\" }}%"
  dashboard_url: "/d/golden-signals/order-processing"
```

Note that templated fields such as `{{ $value | printf "%.2f" }}` inject the evaluated scalar into messages and annotation text.

## Alert state machine

Grafana uses a three-state model to manage alert lifecycle:

| State | Meaning |
| - | - |
| Normal | Condition not breached (query result below threshold) |
| Pending | Condition breached, but `for` duration has not yet elapsed |
| Alerting | Condition has been continuously breached for the entire `for` duration; notifications are sent |

Transitions:

* Normal → Pending when the condition is first breached.
* Pending → Alerting after the full `for` duration is satisfied.
* Alerting → Normal when the condition clears (resolved).

This state progression (Normal → Pending → Alerting → Normal) makes the `for` duration essential for distinguishing brief spikes from sustained incidents.

![A diagram titled "Grafana Alert State Flow" showing three states — Normal, Pending, and Alerting — connected by arrows. It indicates "Breached" moves Normal→Pending, "For: 5m" moves Pending→Alerting, and a "Resolved" arrow returns to Normal, with a bell icon for notification.](https://mintcdn.com/kodekloud-c4ac6d9a/qM9vA-hQoWNCxcKa/images/Grafana-Prometheus-for-Metrics-Driven-Observability/Alert-Management/Grafana-Alerting/grafana-alert-state-flow-diagram.jpg?fit=max\&auto=format\&n=qM9vA-hQoWNCxcKa\&q=85\&s=1501d8912515ca3453d66d4cfbb5d79c)

## Best practices

* Alert on symptoms, not causes: alert on high error rates, latency, or saturation — not on a downstream component’s internal error unless that error directly impacts user experience.
* Keep conditions simple: evaluate a single scalar and test it against a straightforward threshold.
* Use `for` to reduce noise: tune `for` to filter short-lived spikes while keeping detection timely.
* Use labels to route and annotations to help responders: include dashboard links and the evaluated value in annotations.
* Align alerts with SLOs: map alert thresholds and severities to your SLO objectives and escalation policies.

## References and further reading

* Grafana Alerting documentation: [https://grafana.com/docs/grafana/latest/alerting/](https://grafana.com/docs/grafana/latest/alerting/)
* PromQL basics: [https://prometheus.io/docs/prometheus/latest/querying/basics/](https://prometheus.io/docs/prometheus/latest/querying/basics/)
* SRE and monitoring best practices: [https://landing.google.com/sre/book.html](https://landing.google.com/sre/book.html)

We’ve covered how Prometheus queries, Grafana alert conditions, evaluation intervals, `for` durations, labels, and annotations work together to produce reliable, actionable alerts. Though the examples used simple web-service golden signals, the same principles apply in MLOps and other monitoring domains.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/grafana-prometheus-for-metrics-driven-observability/module/342c7176-23d6-47a0-9dbd-4e39b27f87cf/lesson/958f4d14-5057-45ff-9ddd-e8a0310186c7)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.