From f213ad9bfc4833a54781ee135067aaded9321972 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Fri, 14 Jun 2019 07:49:58 -0700 Subject: [PATCH] Add /labels API to client (#604) API ref https://prometheus.io/docs/prometheus/latest/querying/api/#getting-label-names Signed-off-by: Thomas Jackson --- api/prometheus/v1/api.go | 17 +++++++++++++++++ api/prometheus/v1/api_test.go | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 694c7cd..77e7348 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -124,6 +124,7 @@ const ( epAlertManagers = apiPrefix + "/alertmanagers" epQuery = apiPrefix + "/query" epQueryRange = apiPrefix + "/query_range" + epLabels = apiPrefix + "/labels" epLabelValues = apiPrefix + "/label/:name/values" epSeries = apiPrefix + "/series" epTargets = apiPrefix + "/targets" @@ -227,6 +228,8 @@ type API interface { DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error // 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) // LabelValues performs a query for the values of the given label. LabelValues(ctx context.Context, label string) (model.LabelValues, error) // Query performs a query for the given time. @@ -622,6 +625,20 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) { return res, json.Unmarshal(body, &res) } +func (h *httpAPI) LabelNames(ctx context.Context) ([]string, error) { + u := h.client.URL(epLabels, nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + _, body, _, err := h.client.Do(ctx, req) + if err != nil { + return nil, err + } + var labelNames []string + return labelNames, json.Unmarshal(body, &labelNames) +} + func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) { u := h.client.URL(epLabelValues, map[string]string{"name": label}) req, err := http.NewRequest(http.MethodGet, u.String(), nil) diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 49c90d6..f6b4897 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -135,6 +135,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 + } + } + doLabelValues := func(label string) func() (interface{}, api.Warnings, error) { return func() (interface{}, api.Warnings, error) { v, err := promAPI.LabelValues(context.Background(), label) @@ -324,6 +331,22 @@ func TestAPIs(t *testing.T) { err: fmt.Errorf("some error"), }, + { + do: doLabelNames("mylabel"), + inRes: []string{"val1", "val2"}, + reqMethod: "GET", + reqPath: "/api/v1/labels", + res: []string{"val1", "val2"}, + }, + + { + do: doLabelNames("mylabel"), + inErr: fmt.Errorf("some error"), + reqMethod: "GET", + reqPath: "/api/v1/labels", + err: fmt.Errorf("some error"), + }, + { do: doLabelValues("mylabel"), inRes: []string{"val1", "val2"},