Skip to content

Commit adcc4e9

Browse files
author
Nikita Denisenko
committed
Alert rules: add AlertRules() method to fetch all alert rules
1 parent b291103 commit adcc4e9

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

alerting_alert_rule.go

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gapi
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
67
"time"
78
)
89

@@ -62,6 +63,34 @@ type RelativeTimeRange struct {
6263
To time.Duration `json:"to"`
6364
}
6465

66+
// AlertRules fetches and returns Grafana alertRules.
67+
func (c *Client) AlertRules() ([]AlertRule, error) {
68+
const limit = 1000
69+
70+
var (
71+
page = 0
72+
newAlertRules []AlertRule
73+
alertRules []AlertRule
74+
query = make(url.Values)
75+
)
76+
query.Set("limit", fmt.Sprint(limit))
77+
78+
for {
79+
page++
80+
query.Set("page", fmt.Sprint(page))
81+
82+
if err := c.request("GET", "/api/v1/provisioning/alert-rules", query, nil, &newAlertRules); err != nil {
83+
return nil, err
84+
}
85+
86+
alertRules = append(alertRules, newAlertRules...)
87+
88+
if len(newAlertRules) < limit {
89+
return alertRules, nil
90+
}
91+
}
92+
}
93+
6594
// AlertRule fetches a single alert rule, identified by its UID.
6695
func (c *Client) AlertRule(uid string) (AlertRule, error) {
6796
path := fmt.Sprintf("/api/v1/provisioning/alert-rules/%s", uid)

alerting_alert_rule_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,47 @@ package gapi
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
67
"time"
78

89
"github.com/gobs/pretty"
910
)
1011

1112
func TestAlertRules(t *testing.T) {
13+
mockData := strings.Repeat(getAlertRulesJSON+",", 1000) // make 1000 alertRules.
14+
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
15+
16+
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of alertRules.
17+
18+
client := gapiTestToolsFromCalls(t, []mockServerCall{
19+
{200, mockData},
20+
{200, mockData},
21+
{200, "[" + getAlertRulesJSON + "]"},
22+
})
23+
24+
const dashCount = 2001
25+
26+
alertRules, err := client.AlertRules()
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
t.Log(pretty.PrettyFormat(alertRules))
32+
33+
if len(alertRules) != dashCount {
34+
t.Errorf("Length of returned folders should be %d", dashCount)
35+
}
36+
37+
if alertRules[0].UID != "123abcd" || alertRules[0].Title != "Always in alarm" {
38+
t.Error("Not correctly parsing returned alertRules.")
39+
}
40+
if alertRules[dashCount-1].UID != "123abcd" || alertRules[dashCount-1].Title != "Always in alarm" {
41+
t.Error("Not correctly parsing returned alertRules.")
42+
}
43+
}
44+
45+
func TestAlertRule(t *testing.T) {
1246
t.Run("get alert rule succeeds", func(t *testing.T) {
1347
client := gapiTestTools(t, 200, getAlertRuleJSON)
1448

@@ -162,6 +196,19 @@ const writeAlertRuleJSON = `
162196
}
163197
`
164198

199+
const getAlertRulesJSON = `{
200+
"conditions": "A",
201+
"data": [{"datasourceUid":"-100","model":{"conditions":[{"evaluator":{"params":[0,0],"type":"gt"},"operator":{"type":"and"},"query":{"params":[]},"reducer":{"params":[],"type":"avg"},"type":"query"}],"datasource":{"type":"__expr__","uid":"__expr__"},"expression":"1 == 1","hide":false,"intervalMs":1000,"maxDataPoints":43200,"refId":"A","type":"math"},"queryType":"","refId":"A","relativeTimeRange":{"from":0,"to":0}}],
202+
"execErrState": "OK",
203+
"folderUID": "project_test",
204+
"noDataState": "OK",
205+
"orgId": 1,
206+
"uid": "123abcd",
207+
"ruleGroup": "eval_group_1",
208+
"title": "Always in alarm",
209+
"for": "1m"
210+
}`
211+
165212
const getAlertRuleJSON = `
166213
{
167214
"conditions": "A",

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/grafana/grafana-api-golang-client
1+
module github.com/4udesenko/grafana-api-golang-client
22

33
go 1.14
44

0 commit comments

Comments
 (0)