TExcelTAD
Home › Observability: Prometheus, Grafana, Loki & OpenTelemetry › Grafana Alerting to Slack & PagerDuty

Grafana Alerting to Slack & PagerDuty

Grafana has its own alerting system, separate from Prometheus's rule engine and Alertmanager but built on the same ideas (it embeds an Alertmanager internally). It matters when alerts must be based on data sources Prometheus can't see (Loki, SQL databases, CloudWatch), or when a team wants one UI for rules from several sources.

Which engine evaluates the rule?

Prometheus rules + Alertmanager Grafana-managed alert rules
where rules live rule files / PrometheusRule CRDs, next to the data Grafana's database (or provisioning files, Terraform)
data sources Prometheus (Loki and Mimir have rulers with the same format) any Grafana data source, even several in one rule
testing promtool test rules no offline equivalent; test in a staging Grafana
availability independent of Grafana Grafana must be up (HA needs a shared database and clustering)
routing Alertmanager config contact points + notification policies (the same routing-tree model)

A common split: service and infrastructure alerts as Prometheus rules (versioned, tested, independent of the UI), Grafana-managed rules for the few that need other data sources. Grafana can also display and edit data-source-managed rules from Mimir/Loki rulers, so on-call engineers have one alert list.

Rules are queries + expressions

A Grafana alert rule runs one or more queries, then expressions that turn the result into a firing condition:

text
A: query    sum by (service) (rate(http_requests_total{code=~"5.."}[5m])) / sum by (service) (rate(http_requests_total[5m]))
B: reduce   last(A)               ← a time series becomes one number per series
C: threshold B > 0.05            ← the condition
pending period 5m                 ← like Prometheus's `for`

Each series (here, each service) becomes its own alert instance with its labels, just as in Prometheus. Two settings have no Prometheus equivalent and need a decision for every rule:

  • No data handling: what to do when the query returns nothing (NoData state, Alerting, OK, or KeepLast).
  • Error handling: what to do when the data source is unreachable (Error, Alerting, OK, KeepLast).

This model walks one rule through ten evaluations, including a gap in the data and an unreachable data source, with "No data → NoData" and "Error → Error" (the defaults):

python · try it yourself
THRESHOLD, PENDING_EVALS = 0.05, 3      # pending period = 3 evaluations of 1m

# the query result per evaluation: a number, None (no data) or "error" (data source down)
results = [0.01, 0.08, 0.09, 0.11, 0.12, None, 0.10, "error", 0.02, 0.01]

state, pending = "Normal", 0
for minute, value in enumerate(results):
    if value == "error":
        new = "Error"                    # a separate state; its own notification, labelled datasource_error
    elif value is None:
        new = "NoData"                   # also notified: silence means "we can't see", not "all good"
    elif value > THRESHOLD:
        pending += 1
        new = "Alerting" if pending > PENDING_EVALS or state == "Alerting" else "Pending"
    else:
        pending = 0
        new = "Normal"
    if new not in ("Pending", "Alerting"):
        pending = 0
    shown = "-" if value is None else value
    print(f"{minute}m  value={shown!s:<6} {state:>8} -> {new}")
    state = new
0m  value=0.01     Normal -> Normal
1m  value=0.08     Normal -> Pending
2m  value=0.09    Pending -> Pending
3m  value=0.11    Pending -> Pending
4m  value=0.12    Pending -> Alerting
5m  value=-      Alerting -> NoData
6m  value=0.1      NoData -> Pending
7m  value=error   Pending -> Error
8m  value=0.02      Error -> Normal
9m  value=0.01     Normal -> Normal

The NoData and Error evaluations broke the alert's firing streak: after them, it starts pending again. That's why many teams set no-data handling to Keep Last State for noisy sources, and alert on data-source health separately. Whatever you choose, choose it per rule, deliberately.

Contact points

A contact point is a list of integrations: Slack, PagerDuty, email, Microsoft Teams, Opsgenie, webhooks and many more. Provisioned from a file:

yaml
# provisioning/alerting/contact-points.yaml
apiVersion: 1
contactPoints:
  - orgId: 1
    name: links-slack
    receivers:
      - uid: links-slack
        type: slack
        settings:
          url: $SLACK_LINKS_WEBHOOK_URL          # incoming webhook; from the environment, not Git
          recipient: '#links-alerts'
          title: '{{ template "default.title" . }}'
        disableResolveMessage: false
  - orgId: 1
    name: links-pagerduty
    receivers:
      - uid: links-pagerduty
        type: pagerduty
        settings:
          integrationKey: $PAGERDUTY_LINKS_KEY   # the Events API v2 integration key
          severity: critical

Notification policies

The routing tree, with matchers on alert labels, exactly like Alertmanager's:

yaml
# provisioning/alerting/policies.yaml
apiVersion: 1
policies:
  - orgId: 1
    receiver: links-slack                         # default contact point
    group_by: [grafana_folder, alertname]
    group_wait: 30s
    group_interval: 5m
    repeat_interval: 4h
    routes:
      - receiver: links-pagerduty
        object_matchers:
          - ['severity', '=', 'page']
        continue: true
      - receiver: links-slack
        object_matchers:
          - ['severity', '=~', 'page|ticket']

Mute timings (named time intervals) silence routes on schedule, e.g. ticket-level notifications at weekends. Silences work as in Alertmanager.

Rules as files

yaml
# provisioning/alerting/rules.yaml (sample: Grafana writes the full model when you export a rule)
apiVersion: 1
groups:
  - orgId: 1
    name: linkly
    folder: linkly
    interval: 1m
    rules:
      - uid: linkly-5xx
        title: linkly 5xx ratio
        condition: C
        for: 5m
        noDataState: NoData
        execErrState: Error
        labels: {severity: page, service: linkly}
        annotations:
          summary: 'linkly 5xx ratio is {{ humanizePercentage $values.B.Value }}'
          runbook_url: https://runbooks.example.com/linkly/errors
        data:
          - refId: A
            datasourceUid: prometheus
            relativeTimeRange: {from: 600, to: 0}
            model:
              expr: sum(rate(http_requests_total{job="linkly", code=~"5.."}[5m])) / sum(rate(http_requests_total{job="linkly"}[5m]))
          - refId: B
            datasourceUid: __expr__
            model: {type: reduce, expression: A, reducer: last}
          - refId: C
            datasourceUid: __expr__
            model: {type: threshold, expression: B, conditions: [{evaluator: {type: gt, params: [0.05]}}]}

The easiest way to get this right is to build the rule in the UI once, then use Export (Alerting → the rule → More → Export) to get the YAML or Terraform, and commit that.

Test yourself

When is a Grafana-managed alert rule the better choice than a Prometheus rule?

  1. Always
  2. When the condition needs a data source Prometheus doesn't have, such as a SQL database or CloudWatch
  3. When you want promtool tests
  4. When Grafana is down

What is the role of a reduce expression in a Grafana alert rule?

  1. It reduces the number of alerts
  2. It turns each time series returned by the query into one number (last, mean, max…) for the threshold
  3. It deletes old data
  4. It groups notifications

Why decide no-data handling for every rule?

  1. It's required syntax
  2. "No data" can mean healthy silence or a broken pipeline; the default may page or hide problems
  3. It affects the colour of the panel
  4. It sets the evaluation interval