web/api/v1: fix targets endpoint

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-10-25 10:19:20 +02:00 committed by Frederic Branczyk
parent 270c8760a3
commit c7a48a4a93
No known key found for this signature in database
GPG key ID: 7741A52782A90069
2 changed files with 74 additions and 44 deletions

View file

@ -498,40 +498,51 @@ type DroppedTarget struct {
// TargetDiscovery has all the active targets. // TargetDiscovery has all the active targets.
type TargetDiscovery struct { type TargetDiscovery struct {
ActiveTargets map[string][]*Target `json:"activeTargets"` ActiveTargets []*Target `json:"activeTargets"`
DroppedTargets map[string][]*DroppedTarget `json:"droppedTargets"` DroppedTargets []*DroppedTarget `json:"droppedTargets"`
} }
func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) { func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) {
tActive := api.targetRetriever.TargetsActive() flatten := func(targets map[string][]*scrape.Target) []*scrape.Target {
tDropped := api.targetRetriever.TargetsDropped() var n int
res := &TargetDiscovery{ActiveTargets: make(map[string][]*Target, len(tActive)), DroppedTargets: make(map[string][]*DroppedTarget, len(tDropped))} keys := make([]string, 0, len(targets))
for k := range targets {
for tset, targets := range tActive { keys = append(keys, k)
for _, target := range targets { n += len(targets[k])
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(),
})
} }
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 { tActive := flatten(api.targetRetriever.TargetsActive())
for _, t := range tt { tDropped := flatten(api.targetRetriever.TargetsDropped())
res.DroppedTargets[tset] = append(res.DroppedTargets[tset], &DroppedTarget{ res := &TargetDiscovery{ActiveTargets: make([]*Target, 0, len(tActive)), DroppedTargets: make([]*DroppedTarget, 0, len(tDropped))}
DiscoveredLabels: t.DiscoveredLabels().Map(),
}) 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 return res, nil, nil
} }

View file

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