Grafana: Alert State History and No-Data Handling
No-data handling is what a Grafana alert rule does when its query returns no series at all. It is different from the query returning a zero.
Stop the shop and ask Prometheus
With ShopSentry stopped, Prometheus records up as 0 for it. The target is down, and the value proves it.
curl -s "http://127.0.0.1:19105/api/v1/query?query=up%7Bjob%3D%22shopsentry%22%7D"Ask for the shop's own metric
The shop's own gauge stopped arriving the moment the process stopped. The query returns an empty result, not a zero.
curl -s "http://127.0.0.1:19105/api/v1/query?query=shopsentry_up%7Bjob%3D%22shopsentry%22%7D"See the rule state in Grafana
The rule on up fires, because 0 is below 1. The rule on the quiet gauge raises no data instead.
curl -s -u admin:admin "http://127.0.0.1:13105/api/prometheus/grafana/api/v1/rules" | python3 -c "import json,sys; d=json.load(sys.stdin); [print(r['name'],'state:',r['state'],'lastEval:',r.get('lastEvaluation')) for r in d['data']['groups'][0]['rules']]"Example
curl -s -u admin:admin "http://127.0.0.1:13105/api/alertmanager/grafana/api/v2/alerts" | python3 -c "import json,sys; print([(a['labels'].get('alertname'), a['status']['state']) for a in json.load(sys.stdin)])"Key points
- Alert state history is the record of every state a rule has passed through.
- A rule moves between normal, pending, firing, no data and error states.
- When a target stops answering, Prometheus still writes up as 0 for that target.
- A rule on up therefore fires, because 0 is a real value, not missing data.
- When an exporter dies, its own metrics stop arriving and the series goes quiet.
- A rule on the dead exporter's own metric then has no series to test.
- Grafana calls that state no data, and it raises a DatasourceNoData alert for it.
- A real silence is a quiet series with a healthy target, such as no orders overnight.
- A dead exporter is a missing series with up at 0, and it needs a page, not a shrug.
In this example, two alerts are active at once after ShopSentry stops. ShopSentryDown fires because up is 0, a real value. DatasourceNoData fires because the rule on the shop's own gauge got no series back. One dead service produces both states, and the two alerts tell you which kind of silence you have.
Note
Never treat no data as normal, since silence can hide a dead exporter.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
ShopSentry stops. A Grafana rule on up{job="shopsentry"} fires, and a rule on shopsentry_up reports no data. What does that pair of states tell you?