diff --git a/api/prometheus/api.go b/api/prometheus/api.go index cc5cbc3..d2b30b6 100644 --- a/api/prometheus/api.go +++ b/api/prometheus/api.go @@ -280,8 +280,10 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error { type QueryAPI interface { // Query performs a query for the given time. Query(ctx context.Context, query string, ts time.Time) (model.Value, error) - // Query performs a query for the given range. + // QueryRange performs a query for the given range. QueryRange(ctx context.Context, query string, r Range) (model.Value, error) + // QueryLabelValues performs a query for the values of the given label. + QueryLabelValues(ctx context.Context, label string) ([]string, error) } // NewQueryAPI returns a new QueryAPI for the client. @@ -346,3 +348,15 @@ func (h *httpQueryAPI) QueryRange(ctx context.Context, query string, r Range) (m return model.Value(qres.v), err } + +func (h *httpQueryAPI) QueryLabelValues(ctx context.Context, label string) ([]string, error) { + u := h.client.url(epLabelValues, map[string]string{"name": label}) + req, _ := http.NewRequest(http.MethodGet, u.String(), nil) + _, body, err := h.client.do(ctx, req) + if err != nil { + return nil, err + } + var values []string + err = json.Unmarshal(body, &values) + return values, err +} diff --git a/api/prometheus/api_test.go b/api/prometheus/api_test.go index ca084a0..4a38572 100644 --- a/api/prometheus/api_test.go +++ b/api/prometheus/api_test.go @@ -19,6 +19,7 @@ import ( "net/http" "net/url" "reflect" + "strings" "testing" "time" @@ -313,9 +314,13 @@ type apiTest struct { } func (c *apiTestClient) url(ep string, args map[string]string) *url.URL { + path := apiPrefix + ep + for k, v := range args { + path = strings.Replace(path, ":"+k, v, -1) + } u := &url.URL{ Host: "test:9090", - Path: apiPrefix + ep, + Path: path, } return u } @@ -368,6 +373,12 @@ func TestAPIs(t *testing.T) { } } + doQueryLabelValues := func(label string) func() (interface{}, error) { + return func() (interface{}, error) { + return queryAPI.QueryLabelValues(context.Background(), label) + } + } + queryTests := []apiTest{ { do: doQuery("2", testTime), @@ -421,6 +432,22 @@ func TestAPIs(t *testing.T) { }, err: fmt.Errorf("some error"), }, + + { + do: doQueryLabelValues("mylabel"), + inRes: []string{"val1", "val2"}, + reqMethod: "GET", + reqPath: "/api/v1/label/mylabel/values", + res: []string{"val1", "val2"}, + }, + + { + do: doQueryLabelValues("mylabel"), + inErr: fmt.Errorf("some error"), + reqMethod: "GET", + reqPath: "/api/v1/label/mylabel/values", + err: fmt.Errorf("some error"), + }, } var tests []apiTest