Add Warnings to LabelValues and LabelNames (#609)

Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
This commit is contained in:
Thomas Jackson 2019-06-17 11:27:57 -07:00 committed by Krasi Georgiev
parent 4ab88e80c2
commit 3d8379da8f
2 changed files with 50 additions and 16 deletions

View File

@ -229,9 +229,9 @@ type API interface {
// Flags returns the flag values that Prometheus was launched with.
Flags(ctx context.Context) (FlagsResult, error)
// LabelNames returns all the unique label names present in the block in sorted order.
LabelNames(ctx context.Context) ([]string, error)
LabelNames(ctx context.Context) ([]string, api.Warnings, error)
// LabelValues performs a query for the values of the given label.
LabelValues(ctx context.Context, label string) (model.LabelValues, error)
LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error)
// Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error)
// QueryRange performs a query for the given range.
@ -625,32 +625,32 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
return res, json.Unmarshal(body, &res)
}
func (h *httpAPI) LabelNames(ctx context.Context) ([]string, error) {
func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error) {
u := h.client.URL(epLabels, nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
return nil, nil, err
}
_, body, _, err := h.client.Do(ctx, req)
_, body, w, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
return nil, w, err
}
var labelNames []string
return labelNames, json.Unmarshal(body, &labelNames)
return labelNames, w, json.Unmarshal(body, &labelNames)
}
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error) {
u := h.client.URL(epLabelValues, map[string]string{"name": label})
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
return nil, nil, err
}
_, body, _, err := h.client.Do(ctx, req)
_, body, w, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
return nil, w, err
}
var labelValues model.LabelValues
return labelValues, json.Unmarshal(body, &labelValues)
return labelValues, w, json.Unmarshal(body, &labelValues)
}
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error) {

View File

@ -137,15 +137,13 @@ func TestAPIs(t *testing.T) {
doLabelNames := func(label string) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
v, err := promAPI.LabelNames(context.Background())
return v, nil, err
return promAPI.LabelNames(context.Background())
}
}
doLabelValues := func(label string) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
v, err := promAPI.LabelValues(context.Background(), label)
return v, nil, err
return promAPI.LabelValues(context.Background(), label)
}
}
@ -337,6 +335,15 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/labels",
res: []string{"val1", "val2"},
},
{
do: doLabelNames("mylabel"),
inRes: []string{"val1", "val2"},
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/labels",
res: []string{"val1", "val2"},
warnings: []string{"a"},
},
{
do: doLabelNames("mylabel"),
@ -345,6 +352,15 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/labels",
err: fmt.Errorf("some error"),
},
{
do: doLabelNames("mylabel"),
inErr: fmt.Errorf("some error"),
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/labels",
err: fmt.Errorf("some error"),
warnings: []string{"a"},
},
{
do: doLabelValues("mylabel"),
@ -353,6 +369,15 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/label/mylabel/values",
res: model.LabelValues{"val1", "val2"},
},
{
do: doLabelValues("mylabel"),
inRes: []string{"val1", "val2"},
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values",
res: model.LabelValues{"val1", "val2"},
warnings: []string{"a"},
},
{
do: doLabelValues("mylabel"),
@ -361,6 +386,15 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/label/mylabel/values",
err: fmt.Errorf("some error"),
},
{
do: doLabelValues("mylabel"),
inErr: fmt.Errorf("some error"),
inWarnings: []string{"a"},
reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values",
err: fmt.Errorf("some error"),
warnings: []string{"a"},
},
{
do: doSeries("up", testTime.Add(-time.Minute), testTime),