mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-10 21:37:26 -08:00
commit
f5d699dabe
|
@ -498,24 +498,38 @@ 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 {
|
||||||
|
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, targets := range tActive {
|
tActive := flatten(api.targetRetriever.TargetsActive())
|
||||||
for _, target := range targets {
|
tDropped := flatten(api.targetRetriever.TargetsDropped())
|
||||||
|
res := &TargetDiscovery{ActiveTargets: make([]*Target, 0, len(tActive)), DroppedTargets: make([]*DroppedTarget, 0, len(tDropped))}
|
||||||
|
|
||||||
|
for _, target := range tActive {
|
||||||
lastErrStr := ""
|
lastErrStr := ""
|
||||||
lastErr := target.LastError()
|
lastErr := target.LastError()
|
||||||
if lastErr != nil {
|
if lastErr != nil {
|
||||||
lastErrStr = lastErr.Error()
|
lastErrStr = lastErr.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
res.ActiveTargets[tset] = append(res.ActiveTargets[tset], &Target{
|
res.ActiveTargets = append(res.ActiveTargets, &Target{
|
||||||
DiscoveredLabels: target.DiscoveredLabels().Map(),
|
DiscoveredLabels: target.DiscoveredLabels().Map(),
|
||||||
Labels: target.Labels().Map(),
|
Labels: target.Labels().Map(),
|
||||||
ScrapeURL: target.URL().String(),
|
ScrapeURL: target.URL().String(),
|
||||||
|
@ -524,15 +538,12 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) {
|
||||||
Health: target.Health(),
|
Health: target.Health(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for tset, tt := range tDropped {
|
for _, t := range tDropped {
|
||||||
for _, t := range tt {
|
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
||||||
res.DroppedTargets[tset] = append(res.DroppedTargets[tset], &DroppedTarget{
|
|
||||||
DiscoveredLabels: t.DiscoveredLabels().Map(),
|
DiscoveredLabels: t.DiscoveredLabels().Map(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res, nil, nil
|
return res, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,18 +641,25 @@ 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://localhost:9115/probe?target=example.com",
|
||||||
|
Health: "unknown",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
DiscoveredLabels: map[string]string{},
|
||||||
|
Labels: map[string]string{
|
||||||
|
"job": "test",
|
||||||
|
},
|
||||||
ScrapeURL: "http://example.com:8080/metrics",
|
ScrapeURL: "http://example.com:8080/metrics",
|
||||||
Health: "unknown",
|
Health: "unknown",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
DroppedTargets: []*DroppedTarget{
|
||||||
DroppedTargets: map[string][]*DroppedTarget{
|
|
||||||
"test": {
|
|
||||||
{
|
{
|
||||||
DiscoveredLabels: map[string]string{
|
DiscoveredLabels: map[string]string{
|
||||||
"__address__": "http://dropped.example.com:9115",
|
"__address__": "http://dropped.example.com:9115",
|
||||||
|
@ -651,7 +671,6 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
{
|
{
|
||||||
endpoint: api.alertmanagers,
|
endpoint: api.alertmanagers,
|
||||||
response: &AlertmanagerDiscovery{
|
response: &AlertmanagerDiscovery{
|
||||||
|
|
Loading…
Reference in a new issue