8000 Support matchers in rules API (#1843) · prometheus/client_golang@4ae032f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ae032f

Browse files
authored
Support matchers in rules API (#1843)
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 1f3a904 commit 4ae032f

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

api/prometheus/v1/api.go

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

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

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

api/prometheus/v1/api_test.go

Lines changed: 61 additions & 5 deletions
8000
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
}
@@ -696,7 +696,7 @@ func TestAPIs(t *testing.T) {
696696
},
697697

698698
{
699-
do: doRules(),
699+
do: doRules(nil),
700700
reqMethod: "GET",
701701
reqPath: "/api/v1/rules",
702702
inRes: map[string]interface{}{
@@ -791,7 +791,7 @@ func TestAPIs(t *testing.T) {
791791

792792
// This has the newer API elements like lastEvaluation, evaluationTime, etc.
793793
{
794-
do: doRules(),
794+
do: doRules(nil),
795795
reqMethod: "GET",
796796
reqPath: "/api/v1/rules",
797797
inRes: map[string]interface{}{
@@ -895,7 +895,63 @@ func TestAPIs(t *testing.T) {
895895
},
896896

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

0 commit comments

Comments
 (0)
0