Skip to content

Commit

Permalink
Add /labels API to client (#604)
Browse files Browse the repository at this point in the history
API ref https://prometheus.io/docs/prometheus/latest/querying/api/#getting-label-names

Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
  • Loading branch information
jacksontj authored and krasi-georgiev committed Jun 14, 2019
1 parent 92d8f4a commit f213ad9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit f213ad9

Please sign in to comment.