Skip to content

Commit babeb35

Browse files
authored
Merge pull request #828 from yeya24/support-label-matchers
Support matchers in labels API
2 parents d3e175d + 595a1d5 commit babeb35

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

api/prometheus/v1/api.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ type API interface {
230230
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
231231
// Flags returns the flag values that Prometheus was launched with.
232232
Flags(ctx context.Context) (FlagsResult, error)
233-
// LabelNames returns all the unique label names present in the block in sorted order.
234-
LabelNames(ctx context.Context, startTime time.Time, endTime time.Time) ([]string, Warnings, error)
235-
// LabelValues performs a query for the values of the given label.
236-
LabelValues(ctx context.Context, label string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error)
233+
// LabelNames returns the unique label names present in the block in sorted order by given time range and matchers.
234+
LabelNames(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]string, Warnings, error)
235+
// LabelValues performs a query for the values of the given label, time range and matchers.
236+
LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error)
237237
// Query performs a query for the given time.
238238
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
239239
// QueryRange performs a query for the given range.
@@ -691,11 +691,14 @@ func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
691691
return res, json.Unmarshal(body, &res)
692692
}
693693

694-
func (h *httpAPI) LabelNames(ctx context.Context, startTime time.Time, endTime time.Time) ([]string, Warnings, error) {
694+
func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]string, Warnings, error) {
695695
u := h.client.URL(epLabels, nil)
696696
q := u.Query()
697697
q.Set("start", formatTime(startTime))
698698
q.Set("end", formatTime(endTime))
699+
for _, m := range matches {
700+
q.Add("match[]", m)
701+
}
699702

700703
u.RawQuery = q.Encode()
701704

@@ -711,11 +714,14 @@ func (h *httpAPI) LabelNames(ctx context.Context, startTime time.Time, endTime t
711714
return labelNames, w, json.Unmarshal(body, &labelNames)
712715
}
713716

714-
func (h *httpAPI) LabelValues(ctx context.Context, label string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error) {
717+
func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error) {
715718
u := h.client.URL(epLabelValues, map[string]string{"name": label})
716719
q := u.Query()
717720
q.Set("start", formatTime(startTime))
718721
q.Set("end", formatTime(endTime))
722+
for _, m := range matches {
723+
q.Add("match[]", m)
724+
}
719725

720726
u.RawQuery = q.Encode()
721727

api/prometheus/v1/api_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ func TestAPIs(t *testing.T) {
151151
}
152152
}
153153

154-
doLabelNames := func(label string) func() (interface{}, Warnings, error) {
154+
doLabelNames := func(matches []string) func() (interface{}, Warnings, error) {
155155
return func() (interface{}, Warnings, error) {
156-
return promAPI.LabelNames(context.Background(), time.Now().Add(-100*time.Hour), time.Now())
156+
return promAPI.LabelNames(context.Background(), matches, time.Now().Add(-100*time.Hour), time.Now())
157157
}
158158
}
159159

160-
doLabelValues := func(label string) func() (interface{}, Warnings, error) {
160+
doLabelValues := func(matches []string, label string) func() (interface{}, Warnings, error) {
161161
return func() (interface{}, Warnings, error) {
162-
return promAPI.LabelValues(context.Background(), label, time.Now().Add(-100*time.Hour), time.Now())
162+
return promAPI.LabelValues(context.Background(), label, matches, time.Now().Add(-100*time.Hour), time.Now())
163163
}
164164
}
165165

@@ -359,14 +359,14 @@ func TestAPIs(t *testing.T) {
359359
},
360360

361361
{
362-
do: doLabelNames("mylabel"),
362+
do: doLabelNames(nil),
363363
inRes: []string{"val1", "val2"},
364364
reqMethod: "GET",
365365
reqPath: "/api/v1/labels",
366366
res: []string{"val1", "val2"},
367367
},
368368
{
369-
do: doLabelNames("mylabel"),
369+
do: doLabelNames(nil),
370370
inRes: []string{"val1", "val2"},
371371
inWarnings: []string{"a"},
372372
reqMethod: "GET",
@@ -376,31 +376,39 @@ func TestAPIs(t *testing.T) {
376376
},
377377

378378
{
379-
do: doLabelNames("mylabel"),
379+
do: doLabelNames(nil),
380380
inErr: fmt.Errorf("some error"),
381381
reqMethod: "GET",
382382
reqPath: "/api/v1/labels",
383383
err: fmt.Errorf("some error"),
384384
},
385385
{
386-
do: doLabelNames("mylabel"),
386+
do: doLabelNames(nil),
387387
inErr: fmt.Errorf("some error"),
388388
inWarnings: []string{"a"},
389389
reqMethod: "GET",
390390
reqPath: "/api/v1/labels",
391391
err: fmt.Errorf("some error"),
392392
warnings: []string{"a"},
393393
},
394+
{
395+
do: doLabelNames([]string{"up"}),
396+
inRes: []string{"val1", "val2"},
397+
reqMethod: "GET",
398+
reqPath: "/api/v1/labels",
399+
reqParam: url.Values{"match[]": {"up"}},
400+
res: []string{"val1", "val2"},
401+
},
394402

395403
{
396-
do: doLabelValues("mylabel"),
404+
do: doLabelValues(nil, "mylabel"),
397405
inRes: []string{"val1", "val2"},
398406
reqMethod: "GET",
399407
reqPath: "/api/v1/label/mylabel/values",
400408
res: model.LabelValues{"val1", "val2"},
401409
},
402410
{
403-
do: doLabelValues("mylabel"),
411+
do: doLabelValues(nil, "mylabel"),
404412
inRes: []string{"val1", "val2"},
405413
inWarnings: []string{"a"},
406414
reqMethod: "GET",
@@ -410,21 +418,29 @@ func TestAPIs(t *testing.T) {
410418
},
411419

412420
{
413-
do: doLabelValues("mylabel"),
421+
do: doLabelValues(nil, "mylabel"),
414422
inErr: fmt.Errorf("some error"),
415423
reqMethod: "GET",
416424
reqPath: "/api/v1/label/mylabel/values",
417425
err: fmt.Errorf("some error"),
418426
},
419427
{
420-
do: doLabelValues("mylabel"),
428+
do: doLabelValues(nil, "mylabel"),
421429
inErr: fmt.Errorf("some error"),
422430
inWarnings: []string{"a"},
423431
reqMethod: "GET",
424432
reqPath: "/api/v1/label/mylabel/values",
425433
err: fmt.Errorf("some error"),
426434
warnings: []string{"a"},
427435
},
436+
{
437+
do: doLabelValues([]string{"up"}, "mylabel"),
438+
inRes: []string{"val1", "val2"},
439+
reqMethod: "GET",
440+
reqPath: "/api/v1/label/mylabel/values",
441+
reqParam: url.Values{"match[]": {"up"}},
442+
res: model.LabelValues{"val1", "val2"},
443+
},
428444

429445
{
430446
do: doSeries("up", testTime.Add(-time.Minute), testTime),

0 commit comments

Comments
 (0)