forked from mirror/client_golang
Add /labels API to client (#604)
API ref https://prometheus.io/docs/prometheus/latest/querying/api/#getting-label-names Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
This commit is contained in:
parent
92d8f4a558
commit
f213ad9bfc
|
@ -124,6 +124,7 @@ const (
|
||||||
epAlertManagers = apiPrefix + "/alertmanagers"
|
epAlertManagers = apiPrefix + "/alertmanagers"
|
||||||
epQuery = apiPrefix + "/query"
|
epQuery = apiPrefix + "/query"
|
||||||
epQueryRange = apiPrefix + "/query_range"
|
epQueryRange = apiPrefix + "/query_range"
|
||||||
|
epLabels = apiPrefix + "/labels"
|
||||||
epLabelValues = apiPrefix + "/label/:name/values"
|
epLabelValues = apiPrefix + "/label/:name/values"
|
||||||
epSeries = apiPrefix + "/series"
|
epSeries = apiPrefix + "/series"
|
||||||
epTargets = apiPrefix + "/targets"
|
epTargets = apiPrefix + "/targets"
|
||||||
|
@ -227,6 +228,8 @@ type API interface {
|
||||||
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
|
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
|
||||||
// Flags returns the flag values that Prometheus was launched with.
|
// Flags returns the flag values that Prometheus was launched with.
|
||||||
Flags(ctx context.Context) (FlagsResult, error)
|
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 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, error)
|
||||||
// Query performs a query for the given time.
|
// 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)
|
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) {
|
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
|
||||||
u := h.client.URL(epLabelValues, map[string]string{"name": label})
|
u := h.client.URL(epLabelValues, map[string]string{"name": label})
|
||||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
||||||
|
|
|
@ -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) {
|
doLabelValues := func(label string) func() (interface{}, api.Warnings, error) {
|
||||||
return func() (interface{}, api.Warnings, error) {
|
return func() (interface{}, api.Warnings, error) {
|
||||||
v, err := promAPI.LabelValues(context.Background(), label)
|
v, err := promAPI.LabelValues(context.Background(), label)
|
||||||
|
@ -324,6 +331,22 @@ func TestAPIs(t *testing.T) {
|
||||||
err: fmt.Errorf("some error"),
|
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"),
|
do: doLabelValues("mylabel"),
|
||||||
inRes: []string{"val1", "val2"},
|
inRes: []string{"val1", "val2"},
|
||||||
|
|
Loading…
Reference in New Issue