web/api: make target and alertmanager api responses consistent

This commit is contained in:
Frederic Branczyk 2017-01-13 17:15:04 +01:00
parent 389c6d0043
commit bd92571bdd
No known key found for this signature in database
GPG Key ID: CA14788B1E48B256
2 changed files with 16 additions and 10 deletions

View File

@ -351,16 +351,20 @@ type Target struct {
// Any labels that are added to this target and its metrics. // Any labels that are added to this target and its metrics.
Labels model.LabelSet `json:"labels"` Labels model.LabelSet `json:"labels"`
ScrapeUrl string `json:"scrapeUrl"` ScrapeURL string `json:"scrapeUrl"`
LastError string `json:"lastError"` LastError string `json:"lastError"`
LastScrape time.Time `json:"lastScrape"` LastScrape time.Time `json:"lastScrape"`
Health retrieval.TargetHealth `json:"health"` Health retrieval.TargetHealth `json:"health"`
} }
type TargetDiscovery struct {
ActiveTargets []*Target `json:"activeTargets"`
}
func (api *API) targets(r *http.Request) (interface{}, *apiError) { func (api *API) targets(r *http.Request) (interface{}, *apiError) {
targets := api.targetRetriever.Targets() targets := api.targetRetriever.Targets()
res := make([]*Target, len(targets)) res := &TargetDiscovery{ActiveTargets: make([]*Target, len(targets))}
for i, t := range targets { for i, t := range targets {
lastErrStr := "" lastErrStr := ""
@ -369,10 +373,10 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError) {
lastErrStr = lastErr.Error() lastErrStr = lastErr.Error()
} }
res[i] = &Target{ res.ActiveTargets[i] = &Target{
DiscoveredLabels: t.DiscoveredLabels(), DiscoveredLabels: t.DiscoveredLabels(),
Labels: t.Labels(), Labels: t.Labels(),
ScrapeUrl: t.URL().String(), ScrapeURL: t.URL().String(),
LastError: lastErrStr, LastError: lastErrStr,
LastScrape: t.LastScrape(), LastScrape: t.LastScrape(),
Health: t.Health(), Health: t.Health(),

View File

@ -440,14 +440,16 @@ func TestEndpoints(t *testing.T) {
}{2}, }{2},
}, { }, {
endpoint: api.targets, endpoint: api.targets,
response: []*Target{ response: &TargetDiscovery{
ActiveTargets: []*Target{
&Target{ &Target{
DiscoveredLabels: model.LabelSet{}, DiscoveredLabels: model.LabelSet{},
Labels: model.LabelSet{}, Labels: model.LabelSet{},
ScrapeUrl: "http://example.com:8080/metrics", ScrapeURL: "http://example.com:8080/metrics",
Health: "unknown", Health: "unknown",
}, },
}, },
},
}, { }, {
endpoint: api.alertmanagers, endpoint: api.alertmanagers,
response: &AlertmanagerDiscovery{ response: &AlertmanagerDiscovery{