Skip to content

Commit f213ad9

Browse files
jacksontjkrasi-georgiev
authored andcommitted
Add /labels API to client (#604)
API ref https://prometheus.io/docs/prometheus/latest/querying/api/#getting-label-names Signed-off-by: Thomas Jackson <[email protected]>
1 parent 92d8f4a commit f213ad9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const (
124124
epAlertManagers = apiPrefix + "/alertmanagers"
125125
epQuery = apiPrefix + "/query"
126126
epQueryRange = apiPrefix + "/query_range"
127+
epLabels = apiPrefix + "/labels"
127128
epLabelValues = apiPrefix + "/label/:name/values"
128129
epSeries = apiPrefix + "/series"
129130
epTargets = apiPrefix + "/targets"
@@ -227,6 +228,8 @@ type API interface {
227228
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
228229
// Flags returns the flag values that Prometheus was launched with.
229230
Flags(ctx context.Context) (FlagsResult, error)
231+
// LabelNames returns all the unique label names present in the block in sorted order.
232+
LabelNames(ctx context.Context) ([]string, error)
230233
// LabelValues performs a query for the values of the given label.
231234
LabelValues(ctx context.Context, label string) (model.LabelValues, error)
232235
// Query performs a query for the given time.
@@ -622,6 +625,20 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
622625
return res, json.Unmarshal(body, &res)
623626
}
624627

628+
func (h *httpAPI) LabelNames(ctx context.Context) ([]string, error) {
629+
u := h.client.URL(epLabels, nil)
630+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
631+
if err != nil {
632+
return nil, err
633+
}
634+
_, body, _, err := h.client.Do(ctx, req)
635+
if err != nil {
636+
return nil, err
637+
}
638+
var labelNames []string
639+
return labelNames, json.Unmarshal(body, &labelNames)
640+
}
641+
625642
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
626643
u := h.client.URL(epLabelValues, map[string]string{"name": label})
627644
req, err := http.NewRequest(http.MethodGet, u.String(), nil)

api/prometheus/v1/api_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ func TestAPIs(t *testing.T) {
135135
}
136136
}
137137

138+
doLabelNames := func(label string) func() (interface{}, api.Warnings, error) {
139+
return func() (interface{}, api.Warnings, error) {
140+
v, err := promAPI.LabelNames(context.Background())
141+
return v, nil, err
142+
}
143+
}
144+
138145
doLabelValues := func(label string) func() (interface{}, api.Warnings, error) {
139146
return func() (interface{}, api.Warnings, error) {
140147
v, err := promAPI.LabelValues(context.Background(), label)
@@ -324,6 +331,22 @@ func TestAPIs(t *testing.T) {
324331
err: fmt.Errorf("some error"),
325332
},
326333

334+
{
335+
do: doLabelNames("mylabel"),
336+
inRes: []string{"val1", "val2"},
337+
reqMethod: "GET",
338+
reqPath: "/api/v1/labels",
339+
res: []string{"val1", "val2"},
340+
},
341+
342+
{
343+
do: doLabelNames("mylabel"),
344+
inErr: fmt.Errorf("some error"),
345+
reqMethod: "GET",
346+
reqPath: "/api/v1/labels",
347+
err: fmt.Errorf("some error"),
348+
},
349+
327350
{
328351
do: doLabelValues("mylabel"),
329352
inRes: []string{"val1", "val2"},

0 commit comments

Comments
 (0)