Address comments

Signed-off-by: Fabian Reinartz <freinartz@google.com>
This commit is contained in:
Fabian Reinartz 2018-06-05 06:30:19 -04:00
parent 0ff42e754e
commit 057a5ae2b1
3 changed files with 19 additions and 22 deletions

View File

@ -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=<series_selector>`: 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>`: Label selectors that match targets by their label sets. All targets are selected if left empty.
- `metric=<string>`: A metric name to retrieve metadata for. All metric metadata is retrieved if left empty.
- `limit=<number>`: 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": [

View File

@ -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,

View File

@ -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 {