From 2f33fabffeb23450654ebae9478991f6f76db2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Carvalho?= Date: Fri, 31 Mar 2017 19:42:19 -0300 Subject: [PATCH] api: adds label values query implementation This commit adds a client side implementation for fetching label values from prometheus api. --- api/prometheus/api.go | 16 +++++++++++++++- api/prometheus/api_test.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) 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