Browse Source

web/api/v1: fix targets endpoint

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
pull/4783/head
Simon Pasquier 6 years ago
parent
commit
a308a186e4
  1. 65
      web/api/v1/api.go
  2. 53
      web/api/v1/api_test.go

65
web/api/v1/api.go

@ -498,40 +498,51 @@ type DroppedTarget struct {
// TargetDiscovery has all the active targets.
type TargetDiscovery struct {
ActiveTargets map[string][]*Target `json:"activeTargets"`
DroppedTargets map[string][]*DroppedTarget `json:"droppedTargets"`
ActiveTargets []*Target `json:"activeTargets"`
DroppedTargets []*DroppedTarget `json:"droppedTargets"`
}
func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) {
tActive := api.targetRetriever.TargetsActive()
tDropped := api.targetRetriever.TargetsDropped()
res := &TargetDiscovery{ActiveTargets: make(map[string][]*Target, len(tActive)), DroppedTargets: make(map[string][]*DroppedTarget, len(tDropped))}
for tset, targets := range tActive {
for _, target := range targets {
lastErrStr := ""
lastErr := target.LastError()
if lastErr != nil {
lastErrStr = lastErr.Error()
}
res.ActiveTargets[tset] = append(res.ActiveTargets[tset], &Target{
DiscoveredLabels: target.DiscoveredLabels().Map(),
Labels: target.Labels().Map(),
ScrapeURL: target.URL().String(),
LastError: lastErrStr,
LastScrape: target.LastScrape(),
Health: target.Health(),
})
flatten := func(targets map[string][]*scrape.Target) []*scrape.Target {
var n int
keys := make([]string, 0, len(targets))
for k := range targets {
keys = append(keys, k)
n += len(targets[k])
}
sort.Strings(keys)
res := make([]*scrape.Target, 0, n)
for _, k := range keys {
res = append(res, targets[k]...)
}
return res
}
for tset, tt := range tDropped {
for _, t := range tt {
res.DroppedTargets[tset] = append(res.DroppedTargets[tset], &DroppedTarget{
DiscoveredLabels: t.DiscoveredLabels().Map(),
})
tActive := flatten(api.targetRetriever.TargetsActive())
tDropped := flatten(api.targetRetriever.TargetsDropped())
res := &TargetDiscovery{ActiveTargets: make([]*Target, 0, len(tActive)), DroppedTargets: make([]*DroppedTarget, 0, len(tDropped))}
for _, target := range tActive {
lastErrStr := ""
lastErr := target.LastError()
if lastErr != nil {
lastErrStr = lastErr.Error()
}
res.ActiveTargets = append(res.ActiveTargets, &Target{
DiscoveredLabels: target.DiscoveredLabels().Map(),
Labels: target.Labels().Map(),
ScrapeURL: target.URL().String(),
LastError: lastErrStr,
LastScrape: target.LastScrape(),
Health: target.Health(),
})
}
for _, t := range tDropped {
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
DiscoveredLabels: t.DiscoveredLabels().Map(),
})
}
return res, nil, nil
}

53
web/api/v1/api_test.go

@ -61,16 +61,29 @@ func (t testTargetRetriever) TargetsActive() map[string][]*scrape.Target {
model.SchemeLabel: "http",
model.AddressLabel: "example.com:8080",
model.MetricsPathLabel: "/metrics",
model.JobLabel: "test",
}),
nil,
url.Values{},
),
},
"blackbox": {
scrape.NewTarget(
labels.FromMap(map[string]string{
model.SchemeLabel: "http",
model.AddressLabel: "localhost:9115",
model.MetricsPathLabel: "/probe",
model.JobLabel: "blackbox",
}),
nil,
url.Values{"target": []string{"example.com"}},
),
},
}
}
func (t testTargetRetriever) TargetsDropped() map[string][]*scrape.Target {
return map[string][]*scrape.Target{
"test": {
"blackbox": {
scrape.NewTarget(
nil,
labels.FromMap(map[string]string{
@ -628,25 +641,31 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
{
endpoint: api.targets,
response: &TargetDiscovery{
ActiveTargets: map[string][]*Target{
"test": {
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{},
ScrapeURL: "http://example.com:8080/metrics",
Health: "unknown",
ActiveTargets: []*Target{
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "blackbox",
},
ScrapeURL: "http://localhost:9115/probe?target=example.com",
Health: "unknown",
},
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "test",
},
ScrapeURL: "http://example.com:8080/metrics",
Health: "unknown",
},
},
DroppedTargets: map[string][]*DroppedTarget{
"test": {
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
"__metrics_path__": "/probe",
"__scheme__": "http",
"job": "blackbox",
},
DroppedTargets: []*DroppedTarget{
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
"__metrics_path__": "/probe",
"__scheme__": "http",
"job": "blackbox",
},
},
},

Loading…
Cancel
Save