Skip to content

Commit 057bfe8

Browse files
authored
Merge pull request #718 from gotjosh/support-metric-metadata-endpoint
API Client: Support new metadata endpoint in v1
2 parents 673e4a1 + 2463b8e commit 057bfe8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

api/prometheus/v1/api.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ const (
130130
epSeries = apiPrefix + "/series"
131131
epTargets = apiPrefix + "/targets"
132132
epTargetsMetadata = apiPrefix + "/targets/metadata"
133+
epMetadata = apiPrefix + "/metadata"
133134
epRules = apiPrefix + "/rules"
134135
epSnapshot = apiPrefix + "/admin/tsdb/snapshot"
135136
epDeleteSeries = apiPrefix + "/admin/tsdb/delete_series"
@@ -248,6 +249,8 @@ type API interface {
248249
Targets(ctx context.Context) (TargetsResult, error)
249250
// TargetsMetadata returns metadata about metrics currently scraped by the target.
250251
TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, error)
252+
// Metadata returns metadata about metrics currently scraped by the metric name.
253+
Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error)
251254
}
252255

253256
// AlertsResult contains the result from querying the alerts endpoint.
@@ -357,7 +360,7 @@ type DroppedTarget struct {
357360
DiscoveredLabels map[string]string `json:"discoveredLabels"`
358361
}
359362

360-
// MetricMetadata models the metadata of a metric.
363+
// MetricMetadata models the metadata of a metric with its scrape target and name.
361364
type MetricMetadata struct {
362365
Target map[string]string `json:"target"`
363366
Metric string `json:"metric,omitempty"`
@@ -366,6 +369,13 @@ type MetricMetadata struct {
366369
Unit string `json:"unit"`
367370
}
368371

372+
// Metadata models the metadata of a metric.
373+
type Metadata struct {
374+
Type MetricType `json:"type"`
375+
Help string `json:"help"`
376+
Unit string `json:"unit"`
377+
}
378+
369379
// queryResult contains result data for a query.
370380
type queryResult struct {
371381
Type model.ValueType `json:"resultType"`
@@ -802,6 +812,29 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
802812
return res, json.Unmarshal(body, &res)
803813
}
804814

815+
func (h *httpAPI) Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error) {
816+
u := h.client.URL(epMetadata, nil)
817+
q := u.Query()
818+
819+
q.Set("metric", metric)
820+
q.Set("limit", limit)
821+
822+
u.RawQuery = q.Encode()
823+
824+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
825+
if err != nil {
826+
return nil, err
827+
}
828+
829+
_, body, _, err := h.client.Do(ctx, req)
830+
if err != nil {
831+
return nil, err
832+
}
833+
834+
var res map[string][]Metadata
835+
return res, json.Unmarshal(body, &res)
836+
}
837+
805838
// Warnings is an array of non critical errors
806839
type Warnings []string
807840

api/prometheus/v1/api_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ func TestAPIs(t *testing.T) {
202202
}
203203
}
204204

205+
doMetadata := func(metric string, limit string) func() (interface{}, Warnings, error) {
206+
return func() (interface{}, Warnings, error) {
207+
v, err := promAPI.Metadata(context.Background(), metric, limit)
208+
return v, nil, err
209+
}
210+
}
211+
205212
queryTests := []apiTest{
206213
{
207214
do: doQuery("2", testTime),
@@ -857,6 +864,46 @@ func TestAPIs(t *testing.T) {
857864
},
858865
err: fmt.Errorf("some error"),
859866
},
867+
868+
{
869+
do: doMetadata("go_goroutines", "1"),
870+
inRes: map[string]interface{}{
871+
"go_goroutines": []map[string]interface{}{
872+
{
873+
"type": "gauge",
874+
"help": "Number of goroutines that currently exist.",
875+
"unit": "",
876+
},
877+
},
878+
},
879+
reqMethod: "GET",
880+
reqPath: "/api/v1/metadata",
881+
reqParam: url.Values{
882+
"metric": []string{"go_goroutines"},
883+
"limit": []string{"1"},
884+
},
885+
res: map[string][]Metadata{
886+
"go_goroutines": []Metadata{
887+
{
888+
Type: "gauge",
889+
Help: "Number of goroutines that currently exist.",
890+
Unit: "",
891+
},
892+
},
893+
},
894+
},
895+
896+
{
897+
do: doMetadata("", "1"),
898+
inErr: fmt.Errorf("some error"),
899+
reqMethod: "GET",
900+
reqPath: "/api/v1/metadata",
901+
reqParam: url.Values{
902+
"metric": []string{""},
903+
"limit": []string{"1"},
904+
},
905+
err: fmt.Errorf("some error"),
906+
},
860907
}
861908

862909
var tests []apiTest

0 commit comments

Comments
 (0)