Skip to content

Commit 329278e

Browse files
YaoZengzengkrasi-georgiev
authored andcommitted
encapsulate target metadata api (#590)
Signed-off-by: YaoZengzeng <[email protected]>
1 parent 301aa89 commit 329278e

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

api/prometheus/v1/api.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const (
128128
epLabelValues = apiPrefix + "/label/:name/values"
129129
epSeries = apiPrefix + "/series"
130130
epTargets = apiPrefix + "/targets"
131+
epTargetsMetadata = apiPrefix + "/targets/metadata"
131132
epRules = apiPrefix + "/rules"
132133
epSnapshot = apiPrefix + "/admin/tsdb/snapshot"
133134
epDeleteSeries = apiPrefix + "/admin/tsdb/delete_series"
@@ -151,6 +152,9 @@ type RuleType string
151152
// RuleHealth models the health status of a rule.
152153
type RuleHealth string
153154

155+
// MetricType models the type of a metric.
156+
type MetricType string
157+
154158
const (
155159
// Possible values for AlertState.
156160
AlertStateFiring AlertState = "firing"
@@ -179,6 +183,16 @@ const (
179183
RuleHealthGood = "ok"
180184
RuleHealthUnknown = "unknown"
181185
RuleHealthBad = "err"
186+
187+
// Possible values for MetricType
188+
MetricTypeCounter MetricType = "counter"
189+
MetricTypeGauge MetricType = "gauge"
190+
MetricTypeHistogram MetricType = "histogram"
191+
MetricTypeGaugeHistogram MetricType = "gaugehistogram"
192+
MetricTypeSummary MetricType = "summary"
193+
MetricTypeInfo MetricType = "info"
194+
MetricTypeStateset MetricType = "stateset"
195+
MetricTypeUnknown MetricType = "unknown"
182196
)
183197

184198
// Error is an error returned by the API.
@@ -242,6 +256,8 @@ type API interface {
242256
Rules(ctx context.Context) (RulesResult, api.Error)
243257
// Targets returns an overview of the current state of the Prometheus target discovery.
244258
Targets(ctx context.Context) (TargetsResult, api.Error)
259+
// TargetsMetadata returns metadata about metrics currently scraped by the target.
260+
TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, api.Error)
245261
}
246262

247263
// AlertsResult contains the result from querying the alerts endpoint.
@@ -351,6 +367,15 @@ type DroppedTarget struct {
351367
DiscoveredLabels map[string]string `json:"discoveredLabels"`
352368
}
353369

370+
// MetricMetadata models the metadata of a metric.
371+
type MetricMetadata struct {
372+
Target map[string]string `json:"target"`
373+
Metric string `json:"metric,omitempty"`
374+
Type MetricType `json:"type"`
375+
Help string `json:"help"`
376+
Unit string `json:"unit"`
377+
}
378+
354379
// queryResult contains result data for a query.
355380
type queryResult struct {
356381
Type model.ValueType `json:"resultType"`
@@ -760,6 +785,31 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, api.Error) {
760785
return res, api.NewErrorAPI(err, nil)
761786
}
762787

788+
func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, api.Error) {
789+
u := h.client.URL(epTargetsMetadata, nil)
790+
q := u.Query()
791+
792+
q.Set("match_target", matchTarget)
793+
q.Set("metric", metric)
794+
q.Set("limit", limit)
795+
796+
u.RawQuery = q.Encode()
797+
798+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
799+
if err != nil {
800+
return nil, api.NewErrorAPI(err, nil)
801+
}
802+
803+
_, body, apiErr := h.client.Do(ctx, req)
804+
if apiErr != nil {
805+
return nil, apiErr
806+
}
807+
808+
var res []MetricMetadata
809+
err = json.Unmarshal(body, &res)
810+
return res, api.NewErrorAPI(err, nil)
811+
}
812+
763813
// apiClient wraps a regular client and processes successful API responses.
764814
// Successful also includes responses that errored at the API level.
765815
type apiClient struct {

api/prometheus/v1/api_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ func TestAPIs(t *testing.T) {
174174
}
175175
}
176176

177+
doTargetsMetadata := func(matchTarget string, metric string, limit string) func() (interface{}, api.Error) {
178+
return func() (interface{}, api.Error) {
179+
return promAPI.TargetsMetadata(context.Background(), matchTarget, metric, limit)
180+
}
181+
}
182+
177183
queryTests := []apiTest{
178184
{
179185
do: doQuery("2", testTime),
@@ -296,7 +302,7 @@ func TestAPIs(t *testing.T) {
296302
"end": []string{testTime.Format(time.RFC3339Nano)},
297303
},
298304
res: []model.LabelSet{
299-
model.LabelSet{
305+
{
300306
"__name__": "up",
301307
"job": "prometheus",
302308
"instance": "localhost:9090",
@@ -645,6 +651,52 @@ func TestAPIs(t *testing.T) {
645651
inErr: fmt.Errorf("some error"),
646652
err: fmt.Errorf("some error"),
647653
},
654+
655+
{
656+
do: doTargetsMetadata("{job=\"prometheus\"}", "go_goroutines", "1"),
657+
inRes: []map[string]interface{}{
658+
{
659+
"target": map[string]interface{}{
660+
"instance": "127.0.0.1:9090",
661+
"job": "prometheus",
662+
},
663+
"type": "gauge",
664+
"help": "Number of goroutines that currently exist.",
665+
"unit": "",
666+
},
667+
},
668+
reqMethod: "GET",
669+
reqPath: "/api/v1/targets/metadata",
670+
reqParam: url.Values{
671+
"match_target": []string{"{job=\"prometheus\"}"},
672+
"metric": []string{"go_goroutines"},
673+
"limit": []string{"1"},
674+
},
675+
res: []MetricMetadata{
676+
{
677+
Target: map[string]string{
678+
"instance": "127.0.0.1:9090",
679+
"job": "prometheus",
680+
},
681+
Type: "gauge",
682+
Help: "Number of goroutines that currently exist.",
683+
Unit: "",
684+
},
685+
},
686+
},
687+
688+
{
689+
do: doTargetsMetadata("{job=\"prometheus\"}", "go_goroutines", "1"),
690+
inErr: fmt.Errorf("some error"),
691+
reqMethod: "GET",
692+
reqPath: "/api/v1/targets/metadata",
693+
reqParam: url.Values{
694+
"match_target": []string{"{job=\"prometheus\"}"},
695+
"metric": []string{"go_goroutines"},
696+
"limit": []string{"1"},
697+
},
698+
err: fmt.Errorf("some error"),
699+
},
648700
}
649701

650702
var tests []apiTest

0 commit comments

Comments
 (0)