web/api/v1: fix targets endpoint

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
pull/4783/head
Simon Pasquier 6 years ago
parent 270c8760a3
commit a308a186e4

@ -498,24 +498,38 @@ 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))}
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
}
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 tset, targets := range tActive {
for _, target := range targets {
for _, target := range tActive {
lastErrStr := ""
lastErr := target.LastError()
if lastErr != nil {
lastErrStr = lastErr.Error()
}
res.ActiveTargets[tset] = append(res.ActiveTargets[tset], &Target{
res.ActiveTargets = append(res.ActiveTargets, &Target{
DiscoveredLabels: target.DiscoveredLabels().Map(),
Labels: target.Labels().Map(),
ScrapeURL: target.URL().String(),
@ -524,15 +538,12 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) {
Health: target.Health(),
})
}
}
for tset, tt := range tDropped {
for _, t := range tt {
res.DroppedTargets[tset] = append(res.DroppedTargets[tset], &DroppedTarget{
for _, t := range tDropped {
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
DiscoveredLabels: t.DiscoveredLabels().Map(),
})
}
}
return res, nil, nil
}

@ -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,18 +641,25 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
{
endpoint: api.targets,
response: &TargetDiscovery{
ActiveTargets: map[string][]*Target{
"test": {
ActiveTargets: []*Target{
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{},
ScrapeURL: "http://example.com:8080/metrics",
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": {
DroppedTargets: []*DroppedTarget{
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
@ -651,7 +671,6 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
},
},
},
},
{
endpoint: api.alertmanagers,
response: &AlertmanagerDiscovery{

Loading…
Cancel
Save