From 057a5ae2b147b6a1dfe0d3a667e13ed6535dbe20 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 5 Jun 2018 06:30:19 -0400 Subject: [PATCH] Address comments Signed-off-by: Fabian Reinartz --- docs/querying/api.md | 13 ++++++++----- scrape/scrape.go | 6 ++++-- web/api/v1/api.go | 22 +++++++--------------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/docs/querying/api.md b/docs/querying/api.md index d68dcfb5d..8af0d65b5 100644 --- a/docs/querying/api.md +++ b/docs/querying/api.md @@ -363,9 +363,10 @@ $ curl http://localhost:9090/api/v1/targets } ``` -## Target metadata +## Querying target metadata The following endpoint returns metadata about metrics currently scraped by targets. +This is **experimental** and might change in the future. ``` GET /api/v1/targets/metadata @@ -373,7 +374,8 @@ GET /api/v1/targets/metadata URL query parameters: -- `match=`: A selector that matches targets by label matchers. If a metric name is provided, only metadata for that metric name is returned. +- `match_target=`: Label selectors that match targets by their label sets. All targets are selected if left empty. +- `metric=`: A metric name to retrieve metadata for. All metric metadata is retrieved if left empty. - `limit=`: Maximum number of targets to match. The `data` section of the query result consists of a list of objects that @@ -384,7 +386,8 @@ from the first two targets with label `job="prometheus"`. ```json curl -G http://localhost:9091/api/v1/targets/metadata \ - --data-urlencode 'match=go_goroutines{job="prometheus"}' \ + --data-urlencode 'metric=go_goroutines' \ + --data-urlencode 'match_target={job="prometheus"}' \ --data-urlencode 'limit=2' { "status": "success", @@ -409,12 +412,12 @@ curl -G http://localhost:9091/api/v1/targets/metadata \ } ``` -The following example returns metadata for all metrics for the target with +The following example returns metadata for all metrics for all targets with label `instance="127.0.0.1:9090`. ```json curl -G http://localhost:9091/api/v1/targets/metadata \ - --data-urlencode 'match={instance="127.0.0.1:9090"}' + --data-urlencode 'match_target={instance="127.0.0.1:9090"}' { "status": "success", "data": [ diff --git a/scrape/scrape.go b/scrape/scrape.go index 7823511f8..171240713 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -547,7 +547,7 @@ type scrapeCache struct { // metaEntry holds meta information about a metric. type metaEntry struct { - lastIter uint64 // last scrape iteration the entry was observed + lastIter uint64 // Last scrape iteration the entry was observed at. typ textparse.MetricType help string } @@ -684,10 +684,12 @@ func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) { }, true } -func (c *scrapeCache) listMetadata() (res []MetricMetadata) { +func (c *scrapeCache) listMetadata() []MetricMetadata { c.metaMtx.Lock() defer c.metaMtx.Unlock() + res := make([]MetricMetadata, 0, len(c.metadata)) + for m, e := range c.metadata { res = append(res, MetricMetadata{ Metric: m, diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 468b3903c..a7d728797 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -488,29 +488,21 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) { return res, nil, nil } -func (api *API) targetMetadata(r *http.Request) (interface{}, *apiError) { +func (api *API) targetMetadata(r *http.Request) (interface{}, *apiError, func()) { limit := -1 if s := r.FormValue("limit"); s != "" { var err error if limit, err = strconv.Atoi(s); err != nil { - return nil, &apiError{errorBadData, fmt.Errorf("limit must be a number")} + return nil, &apiError{errorBadData, fmt.Errorf("limit must be a number")}, nil } } - matchers, err := promql.ParseMetricSelector(r.FormValue("match")) + matchers, err := promql.ParseMetricSelector(r.FormValue("match_target")) if err != nil { - return nil, &apiError{errorBadData, err} + return nil, &apiError{errorBadData, err}, nil } - var metric string - for i, m := range matchers { - // Extract metric matcher. - if m.Name == labels.MetricName && m.Type == labels.MatchEqual { - metric = m.Value - matchers = append(matchers[:i], matchers[i+1:]...) - break - } - } + metric := r.FormValue("metric") var res []metricMetadata Outer: @@ -546,9 +538,9 @@ Outer: } } if len(res) == 0 { - return nil, &apiError{errorNotFound, errors.New("specified metadata not found")} + return nil, &apiError{errorNotFound, errors.New("specified metadata not found")}, nil } - return res, nil + return res, nil, nil } type metricMetadata struct {