Include scrape pool name for dropped targets in /api/v1/targets

This is needed so we can display dropped targets in the right scrape pool
in the web interface even if they have a non-default job name.

See also https://github.com/prometheus/prometheus/pull/16078

Fixes https://github.com/prometheus/prometheus/issues/16065

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2025-02-27 10:06:11 +01:00
parent 29d8372a04
commit 4be2243f06
5 changed files with 9 additions and 3 deletions

View file

@ -692,6 +692,7 @@ $ curl http://localhost:9090/api/v1/targets
"__scrape_timeout__": "10s",
"job": "node"
},
"scrapePool": "node"
}
]
}

View file

@ -1021,6 +1021,7 @@ type ScrapePoolsDiscovery struct {
type DroppedTarget struct {
// Labels before any processing.
DiscoveredLabels labels.Labels `json:"discoveredLabels"`
ScrapePool string `json:"scrapePool"`
}
// TargetDiscovery has all the active targets.
@ -1181,6 +1182,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
for _, target := range targetsDropped[pool] {
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
DiscoveredLabels: target.DiscoveredLabels(builder),
ScrapePool: pool,
})
}
}

View file

@ -1767,6 +1767,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
ScrapePool: "blackbox",
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},
@ -1816,6 +1817,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
ScrapePool: "blackbox",
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},
@ -1875,6 +1877,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
ScrapePool: "blackbox",
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},

View file

@ -18,6 +18,7 @@ export type Target = {
export interface DroppedTarget {
discoveredLabels: Labels;
scrapePool: string;
}
// Result type for /api/v1/targets endpoint.

View file

@ -109,8 +109,7 @@ const buildPoolsData = (
}
for (const target of droppedTargets) {
const { job: poolName } = target.discoveredLabels;
const pool = pools[poolName];
const pool = pools[target.scrapePool];
if (!pool) {
// TODO: Should we do better here?
throw new Error(
@ -131,7 +130,7 @@ const buildPoolsData = (
.map((value) => value.original);
for (const target of filteredDroppedTargets) {
pools[target.discoveredLabels.job].targets.push({
pools[target.scrapePool].targets.push({
discoveredLabels: target.discoveredLabels,
isDropped: true,
labels: {},