From 19efd0a6759ac2a0d84e6e593a6aa49f39d034d7 Mon Sep 17 00:00:00 2001 From: Leegin <114397475+Leegin-darknight@users.noreply.github.com> Date: Mon, 29 Jan 2024 15:50:20 +0530 Subject: [PATCH] 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> --- web/api/v1/api.go | 4 ++-- web/api/v1/api_test.go | 48 +++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index edbbe8d80..0c02293bf 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -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(), }) } } diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 9100a9ef9..3dc83548d 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -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},