api: Serialize discovered labels into JSON directly in dropped targets (#13484)

Converted maps into labels.Labels to avoid a lot of copying of data which leads to very high memory consumption while opening the /service-discovery endpoint in the Prometheus UI

Signed-off-by: Leegin <114397475+Leegin-darknight@users.noreply.github.com>
This commit is contained in:
Leegin 2024-01-29 15:50:20 +05:30 committed by GitHub
parent e3040bfabc
commit 19efd0a675
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 26 deletions

View File

@ -903,7 +903,7 @@ type ScrapePoolsDiscovery struct {
// DroppedTarget has the information for one target that was dropped during relabelling.
type DroppedTarget struct {
// Labels before any processing.
DiscoveredLabels map[string]string `json:"discoveredLabels"`
DiscoveredLabels labels.Labels `json:"discoveredLabels"`
}
// TargetDiscovery has all the active targets.
@ -1063,7 +1063,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
}
for _, target := range targetsDropped[key] {
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
DiscoveredLabels: target.DiscoveredLabels().Map(),
DiscoveredLabels: target.DiscoveredLabels(),
})
}
}

View File

@ -1439,14 +1439,14 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
},
DroppedTargets: []*DroppedTarget{
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
"__metrics_path__": "/probe",
"__scheme__": "http",
"job": "blackbox",
"__scrape_interval__": "30s",
"__scrape_timeout__": "15s",
},
DiscoveredLabels: labels.FromStrings(
"__address__", "http://dropped.example.com:9115",
"__metrics_path__", "/probe",
"__scheme__", "http",
"job", "blackbox",
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},
@ -1488,14 +1488,14 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
},
DroppedTargets: []*DroppedTarget{
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
"__metrics_path__": "/probe",
"__scheme__": "http",
"job": "blackbox",
"__scrape_interval__": "30s",
"__scrape_timeout__": "15s",
},
DiscoveredLabels: labels.FromStrings(
"__address__", "http://dropped.example.com:9115",
"__metrics_path__", "/probe",
"__scheme__", "http",
"job", "blackbox",
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},
@ -1547,14 +1547,14 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
ActiveTargets: []*Target{},
DroppedTargets: []*DroppedTarget{
{
DiscoveredLabels: map[string]string{
"__address__": "http://dropped.example.com:9115",
"__metrics_path__": "/probe",
"__scheme__": "http",
"job": "blackbox",
"__scrape_interval__": "30s",
"__scrape_timeout__": "15s",
},
DiscoveredLabels: labels.FromStrings(
"__address__", "http://dropped.example.com:9115",
"__metrics_path__", "/probe",
"__scheme__", "http",
"job", "blackbox",
"__scrape_interval__", "30s",
"__scrape_timeout__", "15s",
),
},
},
DroppedTargetCounts: map[string]int{"blackbox": 1},