Grafana Alerting: Alerts From Your Dashboards
Lesson 10's Shop Overview (improved) shows the error ratio crossing its red 5% line, but only to someone looking, and at 3 a.m. nobody is. Grafana alerting turns a query you already draw into a rule that the Grafana server checks on a schedule, notifying someone when the condition holds. This lesson compares it with Prometheus rules, takes a rule apart, and builds one that fires on a high error ratio.
Grafana alerting or Prometheus rules?
Any query you can draw on a panel can also become an alert. A Grafana-managed alert rule stores a query, a condition and a schedule in Grafana's database. On that schedule the Grafana server sends the query to the data source, as a panel does, and checks the answer, with or without a dashboard open.
You know the other path from lesson 7 and lesson 8: Prometheus evaluates the rules in rules.yml itself, and Alertmanager routes what fires. Grafana copies that design: it evaluates the rules, then hands firing alerts to its built-in Alertmanager, an extended version of Prometheus's.
Prometheus rules keep working when Grafana is down and sit in Git beside the scrape config. Grafana-managed rules can query any data source that supports alerting, such as Prometheus, Loki or Elasticsearch, combine several in one rule, and handle missing data and query errors explicitly.
A sensible split: keep must-never-miss alerts, such as "the service is down", in Prometheus, and use Grafana alerting for alerts born in dashboards or spanning data sources. Give each alert one home, or one problem pages people twice.
Anatomy of an alert rule
A rule is a small pipeline that runs once per evaluation, the scheduled check from lesson 7, as the flowchart shows for the lab's rule.
The query is PromQL, as in a panel. The lab reuses Shop Overview's error-ratio query without the $route filter: a rule runs on the server with no dashboard, so no drop-down fills in the variable.
sum(rate(http_requests_total{status=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m]))
Reduce turns each returned series into one number, such as its Last or Max value. New Prometheus alert queries are instant queries, which already return one value per series, so reduce has little to do here. The condition, or threshold, then compares that number: Is above 0.05 means more than 5% of requests failed.
Each series the query returns becomes its own alert instance, with its own state. This query sums everything into one series, so the rule has one instance; sum by (route) would give one per route.
Two settings control timing. The evaluation interval belongs to the rule's evaluation group, a named set of rules in one folder that run together: 1 minute by default, 30 seconds in the lab. The pending period is Grafana's version of Prometheus's for: how long the condition must hold before the instance fires, also 1 minute by default.
Labels and annotations work as in lesson 7. Grafana adds alertname, the rule's name, and grafana_folder; you add your own, such as severity=page, for routing. Annotations hold text for people: a summary, a description, a runbook URL.
Contact points and notification policies
A contact point is Grafana's word for Alertmanager's receiver: a named destination with one or more integrations, such as email, Slack, PagerDuty or a webhook. The lab provisions one from a file, like the data source file in lesson 9. Grafana reads grafana/provisioning/alerting/contact-points.yml at startup; here it is without comments:
apiVersion: 1
contactPoints:
- orgId: 1
name: webhook
receivers:
- uid: lab-webhook
type: webhook
settings:
url: http://webhook:5001/
httpMethod: POST
The URL points at webhook, a new lab service running lesson 8's Python receiver on port 5001, which prints each notification's JSON. A contact point from a file is read-only in the UI: change the file, then restart Grafana.
Notification policies are Grafana's route tree. The default policy at the root matches every alert; nested policies match labels, inherit their parent's settings, and the deepest match picks the contact point, much as child routes did in Alertmanager.
Grouping works as in lesson 8. Policies group by grafana_folder and alertname by default, and the timers mean the same and have the same defaults: Group wait 30 seconds, Group interval 5 minutes, Repeat interval 4 hours.
A rule can also skip the tree: pick a contact point in its Configure notifications step, and every notification goes there, still grouped by folder and rule name with the default timers. The lab does this; label-based policies pay off once many teams share one Grafana (see the Grafana Alerting documentation).
States to watch: Normal, Pending, Firing, No Data and Error
The rule list at Alerting > Alert rules shows each rule's state. A rule is Normal when no instance is pending or alerting, Pending when at least one is waiting out the pending period, and Firing when at least one has passed it. Instances call that last state Alerting.
If the value drops under the threshold during Pending, the instance returns to Normal and nobody is told: Grafana routes an instance for notification only when it turns Alerting and when it later resolves. An optional Keep firing for setting adds a Recovering state that holds a cleared alert for a while, so a flapping value does not resolve and fire again.
Two more states exist only for Grafana-managed rules. No Data means the query ran but returned nothing: stop shop-demo and Prometheus soon has no current samples for it, so the error-ratio query comes back empty. Error means the evaluation failed, for example because Prometheus is unreachable or the query timed out.
By default both also wait out the pending period, then set the instance to No Data or Error and send a separate alert, DatasourceNoData or DatasourceError, to the same contact point. Configure no data and error handling offers Alerting, Normal or Keep last state instead. No Data is not healthy: an empty error ratio can mean the app is down.
Check yourself
You stop shop-demo. Does ShopHighErrorRatio fire?
Not as Firing. The query returns nothing, so after the pending period the instance goes to No Data and Grafana sends a separate DatasourceNoData alert to webhook. An up alert like ShopDemoDown catches a down app more clearly.
The value is above 0.05 at one evaluation and below it at the next. Does the webhook get a message?
No. The instance becomes Pending, then returns to Normal before the 1-minute pending period passes; only reaching Alerting, or resolving from it, triggers a notification.
Two rules in one folder, ShopHighErrorRatio and ShopSlowCheckout, fire seconds apart and both use webhook. How many first messages arrive?
Two. Grafana groups by grafana_folder and alertname, and the rule names differ, so each rule forms its own group and message. A notification policy grouping by grafana_folder alone would merge them.