Skip to content

Commit 4fa03ef

Browse files
committed
Support matchers in rules API
Similar to the older change on Labels API (#828), this adds `matches` to the `Rules` API. This is a breaking change Signed-off-by: Joel Takvorian <jtakvori@redhat.com>
1 parent 8dfb355 commit 4fa03ef

2 files changed

Lines changed: 70 additions & 7 deletions

File tree

api/prometheus/v1/api.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ type API interface {
494494
// under the TSDB's data directory and returns the directory as response.
495495
Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
496496
// Rules returns a list of alerting and recording rules that are currently loaded.
497-
Rules(ctx context.Context) (RulesResult, error)
497+
Rules(ctx context.Context, matches []string) (RulesResult, error)
498498
// Targets returns an overview of the current state of the Prometheus target discovery.
499499
Targets(ctx context.Context) (TargetsResult, error)
500500
// TargetsMetadata returns metadata about metrics currently scraped by the target.
@@ -1235,8 +1235,15 @@ func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult,
12351235
return res, err
12361236
}
12371237

1238-
func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
1238+
func (h *httpAPI) Rules(ctx context.Context, matches []string) (RulesResult, error) {
12391239
u := h.client.URL(epRules, nil)
1240+
q := u.Query()
1241+
1242+
for _, m := range matches {
1243+
q.Add("match[]", m)
1244+
}
1245+
1246+
u.RawQuery = q.Encode()
12401247

12411248
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
12421249
if err != nil {

api/prometheus/v1/api_test.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ func TestAPIs(t *testing.T) {
191191
}
192192
}
193193

194-
doRules := func() func() (interface{}, Warnings, error) {
194+
doRules := func(matches []string) func() (interface{}, Warnings, error) {
195195
return func() (interface{}, Warnings, error) {
196-
v, err := promAPI.Rules(context.Background())
196+
v, err := promAPI.Rules(context.Background(), matches)
197197
return v, nil, err
198198
}
199199
}
@@ -689,7 +689,7 @@ func TestAPIs(t *testing.T) {
689689
},
690690

691691
{
692-
do: doRules(),
692+
do: doRules(nil),
693693
reqMethod: "GET",
694694
reqPath: "/api/v1/rules",
695695
inRes: map[string]interface{}{
@@ -784,7 +784,7 @@ func TestAPIs(t *testing.T) {
784784

785785
// This has the newer API elements like lastEvaluation, evaluationTime, etc.
786786
{
787-
do: doRules(),
787+
do: doRules(nil),
788788
reqMethod: "GET",
789789
reqPath: "/api/v1/rules",
790790
inRes: map[string]interface{}{
@@ -888,7 +888,63 @@ func TestAPIs(t *testing.T) {
888888
},
889889

890890
{
891-
do: doRules(),
891+
do: doRules([]string{`severity="info"`}),
892+
reqMethod: "GET",
893+
reqPath: "/api/v1/rules",
894+
inRes: map[string]interface{}{
895+
"groups": []map[string]interface{}{
896+
{
897+
"file": "/rules.yaml",
898+
"interval": 60,
899+
"name": "example",
900+
"rules": []map[string]interface{}{
901+
{
902+
"alerts": []map[string]interface{}{},
903+
"annotations": map[string]interface{}{
904+
"summary": "High request latency",
905+
},
906+
"duration": 600,
907+
"health": "ok",
908+
"labels": map[string]interface{}{
909+
"severity": "info",
910+
},
911+
"name": "HighRequestLatency",
912+
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
913+
"type": "alerting",
914+
},
915+
},
916+
},
917+
},
918+
},
919+
res: RulesResult{
920+
Groups: []RuleGroup{
921+
{
922+
Name: "example",
923+
File: "/rules.yaml",
924+
Interval: 60,
925+
Rules: []interface{}{
926+
AlertingRule{
927+
Alerts: []*Alert{},
928+
Annotations: model.LabelSet{
929+
"summary": "High request latency",
930+
},
931+
Labels: model.LabelSet{
932+
"severity": "info",
933+
},
934+
Duration: 600,
935+
Health: RuleHealthGood,
936+
Name: "HighRequestLatency",
937+
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
938+
LastError: "",
939+
},
940+
},
941+
},
942+
},
943+
},
944+
},
945+
946+
{
947+
do: doRules(nil),
892948
reqMethod: "GET",
893949
reqPath: "/api/v1/rules",
894950
inErr: errors.New("some error"),

0 commit comments

Comments
 (0)