mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Targets API: Remove superfluous if() and make variable names more intuitive
Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
parent
4b01140207
commit
2fb6697eea
|
@ -1105,15 +1105,15 @@ func (api *API) scrapePools(r *http.Request) apiFuncResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *API) targets(r *http.Request) apiFuncResult {
|
func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
sortKeys := func(targets map[string][]*scrape.Target) ([]string, int) {
|
getSortedPools := func(targets map[string][]*scrape.Target) ([]string, int) {
|
||||||
var n int
|
var n int
|
||||||
keys := make([]string, 0, len(targets))
|
pools := make([]string, 0, len(targets))
|
||||||
for k := range targets {
|
for p := range targets {
|
||||||
keys = append(keys, k)
|
pools = append(pools, p)
|
||||||
n += len(targets[k])
|
n += len(targets[p])
|
||||||
}
|
}
|
||||||
slices.Sort(keys)
|
slices.Sort(pools)
|
||||||
return keys, n
|
return pools, n
|
||||||
}
|
}
|
||||||
|
|
||||||
scrapePool := r.URL.Query().Get("scrapePool")
|
scrapePool := r.URL.Query().Get("scrapePool")
|
||||||
|
@ -1125,14 +1125,14 @@ func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
|
|
||||||
if showActive {
|
if showActive {
|
||||||
targetsActive := api.targetRetriever(r.Context()).TargetsActive()
|
targetsActive := api.targetRetriever(r.Context()).TargetsActive()
|
||||||
activeKeys, numTargets := sortKeys(targetsActive)
|
activePools, numTargets := getSortedPools(targetsActive)
|
||||||
res.ActiveTargets = make([]*Target, 0, numTargets)
|
res.ActiveTargets = make([]*Target, 0, numTargets)
|
||||||
|
|
||||||
for _, key := range activeKeys {
|
for _, p := range activePools {
|
||||||
if scrapePool != "" && key != scrapePool {
|
if scrapePool != "" && p != scrapePool {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, target := range targetsActive[key] {
|
for _, target := range targetsActive[p] {
|
||||||
lastErrStr := ""
|
lastErrStr := ""
|
||||||
lastErr := target.LastError()
|
lastErr := target.LastError()
|
||||||
if lastErr != nil {
|
if lastErr != nil {
|
||||||
|
@ -1144,7 +1144,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
res.ActiveTargets = append(res.ActiveTargets, &Target{
|
res.ActiveTargets = append(res.ActiveTargets, &Target{
|
||||||
DiscoveredLabels: target.DiscoveredLabels(builder),
|
DiscoveredLabels: target.DiscoveredLabels(builder),
|
||||||
Labels: target.Labels(builder),
|
Labels: target.Labels(builder),
|
||||||
ScrapePool: key,
|
ScrapePool: p,
|
||||||
ScrapeURL: target.URL().String(),
|
ScrapeURL: target.URL().String(),
|
||||||
GlobalURL: globalURL.String(),
|
GlobalURL: globalURL.String(),
|
||||||
LastError: func() string {
|
LastError: func() string {
|
||||||
|
@ -1170,16 +1170,15 @@ func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
}
|
}
|
||||||
if showDropped {
|
if showDropped {
|
||||||
res.DroppedTargetCounts = api.targetRetriever(r.Context()).TargetsDroppedCounts()
|
res.DroppedTargetCounts = api.targetRetriever(r.Context()).TargetsDroppedCounts()
|
||||||
}
|
|
||||||
if showDropped {
|
|
||||||
targetsDropped := api.targetRetriever(r.Context()).TargetsDropped()
|
targetsDropped := api.targetRetriever(r.Context()).TargetsDropped()
|
||||||
droppedKeys, numTargets := sortKeys(targetsDropped)
|
droppedPools, numTargets := getSortedPools(targetsDropped)
|
||||||
res.DroppedTargets = make([]*DroppedTarget, 0, numTargets)
|
res.DroppedTargets = make([]*DroppedTarget, 0, numTargets)
|
||||||
for _, key := range droppedKeys {
|
for _, p := range droppedPools {
|
||||||
if scrapePool != "" && key != scrapePool {
|
if scrapePool != "" && p != scrapePool {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, target := range targetsDropped[key] {
|
for _, target := range targetsDropped[p] {
|
||||||
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
||||||
DiscoveredLabels: target.DiscoveredLabels(builder),
|
DiscoveredLabels: target.DiscoveredLabels(builder),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue