api: adds label values query implementation

This commit adds a client side implementation for fetching label values
from prometheus api.
This commit is contained in:
André Carvalho 2017-03-31 19:42:19 -03:00
parent 738ed6c0b9
commit 2f33fabffe
No known key found for this signature in database
GPG Key ID: 440C9C458D3A1E61
2 changed files with 43 additions and 2 deletions

View File

@ -280,8 +280,10 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
type QueryAPI interface { type QueryAPI interface {
// Query performs a query for the given time. // Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, error) 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) 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. // 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 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
}

View File

@ -19,6 +19,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"strings"
"testing" "testing"
"time" "time"
@ -313,9 +314,13 @@ type apiTest struct {
} }
func (c *apiTestClient) url(ep string, args map[string]string) *url.URL { 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{ u := &url.URL{
Host: "test:9090", Host: "test:9090",
Path: apiPrefix + ep, Path: path,
} }
return u 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{ queryTests := []apiTest{
{ {
do: doQuery("2", testTime), do: doQuery("2", testTime),
@ -421,6 +432,22 @@ func TestAPIs(t *testing.T) {
}, },
err: fmt.Errorf("some error"), 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 var tests []apiTest