Grafana new Unified Alerting API

Hi,
I am trying to create an alert rule using the new unified alerting API. so under
/api/ruler/grafana/api/v1/rules/

First i do GET /api/ruler/grafana/api/v1/rules/ to list all the rules, and it works correctly. I can see my test alert undert my test folder.
i see in particular:

"namespace_uid": "xIVbTkmVz",
"namespace_id": 149,
 "rule_group": "Test GoLang",

But when i try to create a new alert rule doing a POST on /api/ruler/grafana/api/v1/rules/xIVbTkmVz
I get

{
    "message": "folder not found",
    "status": "not-found"
}

but i chekd and the folder uid is correct.
is it a a possible bug?

Grafana version: v8.2.1
Alerting unified active.

Welcome to the :grafana: community @joel!

The APIs for provisioning alerts with unified alerting are available here. You’ll want to use the http://grafana.staged-by-discourse.com/api/v1/provisioning/alert-rules endpoint.

NOTE: There is an error in docs linked above that duplicates the api/v1 part of the URL, so you’ll want to be aware of that.

I found an example in this github comment that includes the API and a sample rule that work:

Here’s the full request using default admin credentials, but you can also use authorization header as shown in the github comment.

Edit to update “folderUID”: “YOUR FOLDER UID” and “ruleGroup”: “YOUR RULE GROUP”,

 curl -X POST -H "Content-Type: application/json" -u admin:admin http://grafana.staged-by-discourse.com/api/v1/provisioning/alert-rules \
-d '{
  "id": 4,
  "uid": "JZDA4uGVz",
  "orgID": 1,
  "folderUID": "0GdTVXG4z",
  "ruleGroup": "SN000092",
  "title": "Test API Rule",
  "condition": "B",
  "data": [
    {
      "refId": "A",
      "queryType": "",
      "relativeTimeRange": {
        "from": 600,
        "to": 0
      },
      "datasourceUid": "gdev-prometheus",
      "model": {
        "expr": "(time() - (push_time_seconds{job=\"caterva_collector_SN000092\"})) / 3600",
        "hide": false,
        "intervalMs": 1000,
        "maxDataPoints": 43200,
        "refId": "A"
      }
    },
    {
      "refId": "B",
      "queryType": "",
      "relativeTimeRange": {
        "from": 0,
        "to": 0
      },
      "datasourceUid": "-100",
      "model": {
        "conditions": [
          {
            "evaluator": {
              "params": [
                6
              ],
              "type": "gt"
            },
            "operator": {
              "type": "and"
            },
            "query": {
              "params": [
                "A"
              ]
            },
            "reducer": {
              "params": [],
              "type": "last"
            },
            "type": "query"
          }
        ],
        "datasource": {
          "type": "__expr__",
          "uid": "-100"
        },
        "hide": false,
        "intervalMs": 1000,
        "maxDataPoints": 43200,
        "refId": "B",
        "type": "classic_conditions"
      }
    }
  ],
  "updated": "2022-09-12T10:49:52.242276+02:00",
  "noDataState": "OK",
  "execErrState": "OK",
  "for": "20m",
  "annotations": {
    "summary": "Test provisioned alert"
  },
  "labels": {
    "sn": "000092"
  }
}'

1 Like

@melori.arellano
Could you help me please with the API request?
I use Postman for that.

  1. Make GET request to http://url:3000/api/v1/provisioning/alert-rules/I1Nu1J6nz
    This what I get:
{
    "id": 94,
    "uid": "I1Nu1J6nz",
    "orgID": 1,
    "folderUID": "UpxdWjhnz",
    "ruleGroup": "Test",
    "title": "Test alert",
    "condition": "C",
    "data": [
        {
            "refId": "A",
            "queryType": "",
            "relativeTimeRange": {
                "from": 600,
                "to": 0
            },
            "datasourceUid": "MGGf-HtGz",
            "model": {
                "alias": "",
                "editorMode": "code",
                "expr": "kube_deployment_labels{cluster=\"test\"}",
                "format": "table",
                "hide": false,
                "intervalMs": 1000,
                "maxDataPoints": 43200,
                "range": true,
                "refId": "A"
            }
        },
        {
            "refId": "B",
            "queryType": "",
            "relativeTimeRange": {
                "from": 600,
                "to": 0
            },
            "datasourceUid": "-100",
            "model": {
                "conditions": [
                    {
                        "evaluator": {
                            "params": [
                                0,
                                0
                            ],
                            "type": "gt"
                        },
                        "operator": {
                            "type": "and"
                        },
                        "query": {
                            "params": [
                                "A"
                            ]
                        },
                        "reducer": {
                            "params": [],
                            "type": "avg"
                        },
                        "type": "query"
                    }
                ],
                "datasource": {
                    "name": "Expression",
                    "type": "__expr__",
                    "uid": "__expr__"
                },
                "expression": "A",
                "hide": false,
                "intervalMs": 1000,
                "maxDataPoints": 43200,
                "reducer": "mean",
                "refId": "B",
                "type": "reduce"
            }
        },
        {
            "refId": "C",
            "queryType": "",
            "relativeTimeRange": {
                "from": 600,
                "to": 0
            },
            "datasourceUid": "-100",
            "model": {
                "conditions": [
                    {
                        "evaluator": {
                            "params": [
                                0,
                                0
                            ],
                            "type": "gt"
                        },
                        "operator": {
                            "type": "and"
                        },
                        "query": {
                            "params": [
                                "B"
                            ]
                        },
                        "reducer": {
                            "params": [],
                            "type": "avg"
                        },
                        "type": "query"
                    }
                ],
                "datasource": {
                    "name": "Expression",
                    "type": "__expr__",
                    "uid": "__expr__"
                },
                "expression": "$B < 1",
                "hide": false,
                "intervalMs": 1000,
                "maxDataPoints": 43200,
                "refId": "C",
                "type": "math"
            }
        }
    ],
    "updated": "2022-11-25T19:45:17+03:00",
    "noDataState": "NoData",
    "execErrState": "Alerting",
    "for": "15d",
    "annotations": {
        "summary": "{{ $labels.metric }} - {{ $values.A.Value }}"
    },
    "labels": {
        "test": "true"
    }
}
  1. Make PUT request to http://url:3000/api/v1/provisioning/alert-rules/I1Nu1J6nz with that payload in body, changing just the string
    "expr": "kube_deployment_labels{cluster=\"test-test\"}"
    And I get
{
    "message": "invalid alert rule: no queries or expressions are found",
    "traceID": ""
}

What is the problem?

It sounds like it’s not seeing the body, what other arguments are you passing on the command line?

Nothing else. Should I add something?

Did you ever find out? I feel the documentation on this topic is scarce and there is a bunch of questions in the forums that never get answered about how to export/import alert rules which in my opinion is crucial to keep them under source control and make deployment easy.

It seems the capability is there but there is some information missing on how to use it.

1 Like