Route Scout alerts with notification policies
Label each alert rule with the tier it belongs to, and let one notification policy tree decide where every tier goes. You change who gets paged in one place, without editing rules, and query failures can go to their own channel.
Why route with policies
An alert rule can send straight to a contact point ("Select contact point" in the rule editor). That is quick for a few rules, but the destination is then copied into every rule. Moving on-call from one team to another means editing every rule that pages.
With notification policies, each rule carries a label that says what kind of alert it is, and a single policy tree maps labels to contact points. You get:
- One place to change routing. Point a tier at a different contact point and every rule in that tier follows.
- Mute timings per tier. For example, no warning notifications overnight, without touching the rules.
- Separate handling for broken queries. Scout raises a distinct alert when a rule's query fails, and only policy routing can send it somewhere different from the rule's normal destination.
How it works
A normal alert from a warnings rule matches only the tier = warnings route.
If the same rule's query fails, its DatasourceError alert matches the first
route; if the query returns nothing, its DatasourceNoData alert matches the
second. Each goes to its own contact point and, with "Continue matching" on,
carries on down the tree to the warnings route as well.
Recommended layout
Give every alert rule one label, tier, whose value names the contact point it
should reach. Then add one route per tier to the policy tree, plus a first
route for query errors.
| Order | Matcher | Contact point | Continue matching | Mute timing |
|---|---|---|---|---|
| 1 | alertname = DatasourceError | datasource-errors | Yes | None |
| 2 | alertname = DatasourceNoData (optional) | datasource-nodata | Yes | None |
| 3 | tier = oncall | oncall | No | None |
| 4 | tier = warnings | warnings | No | Night |
| 5 | tier = notifications | notifications | No | None |
| Default policy | (anything unmatched) | notifications | n/a | None |
The names are examples; use your own contact points. Three choices matter:
- Use a dedicated label for routing. Don't route on an existing label such
as
severityif any rule already uses it for something else. A rule labelledseverity=warningmay still be meant to page, and routing onseveritywould demote it. - Point the default policy at a low-noise destination. It only catches
rules with no
tierlabel, typically ones created in the UI without one. - Order matters. Routes are checked top to bottom and the first match wins, unless that route has "Continue matching" on.
Option A: set it up in the Scout UI
Use this if you manage alerting by hand. Do the steps in this order, so no alert is ever left without a route.
- Create the contact points. Go to Alerting → Contact points → Add
contact point, and create one per tier (for example
oncall,warnings,notifications) plusdatasource-errors. - Create any mute timings. Go to Alerting → Notification policies → Mute
timings → Add mute timing. For example,
Night, 00:00–08:00 on weekdays in your time zone. - Build the policy tree. On Alerting → Notification policies:
- Edit the default policy and set its contact point to your low-noise destination.
- Add a child policy per tier with the matcher
tier = <tier>and that tier's contact point. Add the mute timing to the tiers that need it. - Add the
alertname = DatasourceErrorpolicy at the top, and turn on "Continue matching subsequent sibling nodes". If you route no-data alerts too, add analertname = DatasourceNoDatapolicy right below it.
- Label each rule. In each alert rule, under the notification settings
choose Use notification policy instead of "Select contact point". Add
the label
tierwith the right value. Use Preview routing to see which policy the rule lands on before saving. - Set error handling. Under "Configure no data and error handling", set Alert state if execution error or timeout to Error. Without this, a failing query never raises the separate error alert. The no-data state needs the same attention; see Route no-data alerts too.
New rules need the same two settings: policy routing and a tier label. A rule
without a tier label falls through to the default policy.
Option B: set it up with GitOps and the Scout CLI
Use this if you keep alerting in git. The Scout CLI pulls rules and notification settings into plain JSON files and pushes them back.
1. Start from what you have. Pull your current setup into the repo, and commit it before changing anything:
scout login --account <your-account>
scout resources pull --only alerts,notifications --dir resources
You get one folder per alert folder, each rule as
<folder>/alerts/<rule-title>.json, and the notification setup under
_notifications/: contact-points/, mute-timings/, templates/ and
policies.json.
2. Write the policy tree. policies.json holds the default policy and the
routes, in order:
{
"defaults": {
"group_by": ["grafana_folder", "alertname"],
"receiver": "notifications"
},
"routes": [
{
"continue": true,
"matchers": [{"label": "alertname", "type": "=", "value": "DatasourceError"}],
"receiver": "datasource-errors"
},
{
"continue": true,
"matchers": [{"label": "alertname", "type": "=", "value": "DatasourceNoData"}],
"receiver": "datasource-nodata"
},
{
"continue": false,
"matchers": [{"label": "tier", "type": "=", "value": "oncall"}],
"receiver": "oncall"
},
{
"continue": false,
"matchers": [{"label": "tier", "type": "=", "value": "warnings"}],
"mute_time_intervals": ["Night"],
"receiver": "warnings"
},
{
"continue": false,
"matchers": [{"label": "tier", "type": "=", "value": "notifications"}],
"receiver": "notifications"
}
]
}
Use this matchers format exactly. The CLI silently drops Grafana's own
object_matchers format, which leaves routes that match every alert.
3. Label the rules. In each rule's JSON, set
"labels": {"tier": "oncall"} (or the rule's tier) and
"notification_settings": null. A rule that keeps a notification_settings
receiver bypasses the policy tree entirely.
4. Keep secrets out of git. Slack tokens and webhook passwords stay in
Scout: the files mark them "secureFields": {"token": true} and a push leaves
them alone. Webhook URLs are written as placeholders such as
${SCOUT_SECRET_ONCALL_URL}, filled from environment variables of the same
name at push time. A webhook the CLI hasn't seen before comes back as
${SCOUT_SECRET_<INTEGRATION_UID>_URL}; rename it, and the next pull into
resources keeps the name. When you pull into another folder, pass
--names-from resources so the names carry over.
5. Check for drift, then push. Someone may have changed things in the UI since your last push. Pull into a scratch folder and compare before pushing:
scout resources pull --only alerts,notifications --dir .pull --names-from resources
diff -r resources .pull
scout resources push resources --only alerts,notifications
If the diff shows UI changes you want to keep, copy them into the repo first. A push overwrites them otherwise.
A push leaves two things to you. It does not delete contact points that are no longer in the repo, so remove those in the UI. It also cannot create a Slack integration's token: create a new Slack contact point in the UI first, then pull it into the repo unchanged before you edit it.
Send query errors to their own contact point
When a rule's query fails or times out, Scout does not fire the rule's own
alert. It fires a separate alert named DatasourceError, which carries the
rule's labels plus rulename, datasource_uid and ref_id. The error text
is in its Error annotation.
- Turn it on per rule. Set "Alert state if execution error or timeout" to
Error. (In the CLI's JSON,
"execErrState": "Error".) - Route it first. The
alertname = DatasourceErrorroute sits at the top of the tree. - Decide whether errors also reach the normal tier. With "Continue matching" on, an error on a paging rule also pages, which is right if a silently broken rule worries you more than a noisy night. With it off, errors go only to the error contact point.
- No-data is a separate alert. A query that succeeds but returns nothing
fires
DatasourceNoData, which you can route the same way; see the next section.
To make the error readable in Slack, show the rule name and the error in your message template, for example:
{{ if .Labels.rulename }}*Rule:* {{ .Labels.rulename }}
{{ end }}{{ if .Annotations.Error }}*Error:* `{{ .Annotations.Error }}`
{{ end }}
In the title template, {{ if eq .CommonLabels.alertname "DatasourceError" }}
lets you give error notifications their own heading, so nobody mistakes a
broken query for a real incident.
Route no-data alerts too
When a rule's query succeeds but returns no rows, and its no-data state is
No Data, Scout fires a separate alert named DatasourceNoData. Like
DatasourceError, it carries the rule's labels plus rulename, so it can be
routed the same way.
- Choose the no-data state per rule. Set "Alert state if no data" to
No Data (in the CLI's JSON,
"noDataState": "NoData") on rules where an empty result means something is wrong. Set it to OK where empty is normal, such as a rule that only returns rows when there is a problem. Otherwise those rules raise no-data alerts all the time. - Add a route right below the error route:
alertname = DatasourceNoDatato a contact point of your choice. It can be its own (datasource-nodata) or the same as errors. "Continue matching" works the same way: on, the alert also reaches the rule's own tier; off, it goes only to this contact point. - Keep it apart from errors where you can. A failed query means the rule itself is broken. No data often means a source went quiet, an exporter stopped or a sender disconnected, which can be the incident itself. Different people may own the two.
If both go to the same channel, give each its own heading in the title
template, for example
{{ if eq .CommonLabels.alertname "DatasourceNoData" }} for a "NO DATA"
heading.
Test it before you rely on it
The surest test is a throwaway rule whose query always fails, one per tier. Warn your on-call first: a test on a paging tier really pages, and a silence would mute the test too.
-
Create a rule in a scratch folder with this ClickHouse query:
SELECT toUInt64(toUnixTimestamp(now())) * 1000 AS t,throwIf(1, 'datasource error routing test') AS vSet the error state to Error, choose Use notification policy, and add the label
tierfor the tier you are testing. -
Within a minute or two the rule shows state Error and a
DatasourceErroralert appears. Check that the error contact point received it with your heading and the error text. If "Continue matching" is on, check that the tier's own contact point received it too. -
Repeat for each tier, then delete the test rules. You should get a resolved message for each.
To check a single rule without firing anything, open it and use Preview routing: it lists the policy and contact point its labels match.
Common pitfalls
| Pitfall | What happens | Avoid it by |
|---|---|---|
| A rule still uses "Select contact point" | Its alerts, and its query errors, skip the policy tree | Switch every rule to "Use notification policy" |
Routes written with object_matchers and pushed with the CLI | The matchers are dropped; the first routes catch every alert | Write matchers as {label, type, value}, then check the tree in the UI after the first push |
| Routing on a label that already has another meaning | Rules change tier without anyone noticing | Use a dedicated tier label |
| Adding or renaming a label on live rules | Firing alerts resolve and fire again once; silences matching the old labels stop working | Change labels at a quiet time and re-check silences |
| A contact point created in the UI, then edited in git before pulling | The repo and Scout disagree, and the next push overwrites the UI change | Pull it unchanged, commit, then edit |
{{ index $labels.name }} or mul, div in a summary | The template fails, often only on error or no-data alerts that lack the label | Use {{ $labels.name }} and {{ $values.B.Value | humanizePercentage }} |
After any change to routing, open Alerting → Notification policies and confirm each route shows its matcher. A minute spent there catches most of the pitfalls above.