api: Serialize discovered and target labels into JSON directly (#13469)

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:49:02 +05:30 committed by GitHub
parent bdc3cfdd5d
commit e3040bfabc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 28 deletions

View file

@ -879,9 +879,9 @@ func (api *API) dropSeries(_ *http.Request) apiFuncResult {
// Target has the information for one target. // Target has the information for one target.
type Target struct { type Target struct {
// Labels before any processing. // Labels before any processing.
DiscoveredLabels map[string]string `json:"discoveredLabels"` DiscoveredLabels labels.Labels `json:"discoveredLabels"`
// Any labels that are added to this target and its metrics. // Any labels that are added to this target and its metrics.
Labels map[string]string `json:"labels"` Labels labels.Labels `json:"labels"`
ScrapePool string `json:"scrapePool"` ScrapePool string `json:"scrapePool"`
ScrapeURL string `json:"scrapeUrl"` ScrapeURL string `json:"scrapeUrl"`
@ -1024,8 +1024,8 @@ func (api *API) targets(r *http.Request) apiFuncResult {
globalURL, err := getGlobalURL(target.URL(), api.globalURLOptions) globalURL, err := getGlobalURL(target.URL(), api.globalURLOptions)
res.ActiveTargets = append(res.ActiveTargets, &Target{ res.ActiveTargets = append(res.ActiveTargets, &Target{
DiscoveredLabels: target.DiscoveredLabels().Map(), DiscoveredLabels: target.DiscoveredLabels(),
Labels: target.Labels().Map(), Labels: target.Labels(),
ScrapePool: key, ScrapePool: key,
ScrapeURL: target.URL().String(), ScrapeURL: target.URL().String(),
GlobalURL: globalURL.String(), GlobalURL: globalURL.String(),

View file

@ -1411,10 +1411,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response: &TargetDiscovery{ response: &TargetDiscovery{
ActiveTargets: []*Target{ ActiveTargets: []*Target{
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "blackbox"),
"job": "blackbox",
},
ScrapePool: "blackbox", ScrapePool: "blackbox",
ScrapeURL: "http://localhost:9115/probe?target=example.com", ScrapeURL: "http://localhost:9115/probe?target=example.com",
GlobalURL: "http://localhost:9115/probe?target=example.com", GlobalURL: "http://localhost:9115/probe?target=example.com",
@ -1426,10 +1424,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
ScrapeTimeout: "10s", ScrapeTimeout: "10s",
}, },
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "test"),
"job": "test",
},
ScrapePool: "test", ScrapePool: "test",
ScrapeURL: "http://example.com:8080/metrics", ScrapeURL: "http://example.com:8080/metrics",
GlobalURL: "http://example.com:8080/metrics", GlobalURL: "http://example.com:8080/metrics",
@ -1464,10 +1460,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response: &TargetDiscovery{ response: &TargetDiscovery{
ActiveTargets: []*Target{ ActiveTargets: []*Target{
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "blackbox"),
"job": "blackbox",
},
ScrapePool: "blackbox", ScrapePool: "blackbox",
ScrapeURL: "http://localhost:9115/probe?target=example.com", ScrapeURL: "http://localhost:9115/probe?target=example.com",
GlobalURL: "http://localhost:9115/probe?target=example.com", GlobalURL: "http://localhost:9115/probe?target=example.com",
@ -1479,10 +1473,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
ScrapeTimeout: "10s", ScrapeTimeout: "10s",
}, },
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "test"),
"job": "test",
},
ScrapePool: "test", ScrapePool: "test",
ScrapeURL: "http://example.com:8080/metrics", ScrapeURL: "http://example.com:8080/metrics",
GlobalURL: "http://example.com:8080/metrics", GlobalURL: "http://example.com:8080/metrics",
@ -1517,10 +1509,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response: &TargetDiscovery{ response: &TargetDiscovery{
ActiveTargets: []*Target{ ActiveTargets: []*Target{
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "blackbox"),
"job": "blackbox",
},
ScrapePool: "blackbox", ScrapePool: "blackbox",
ScrapeURL: "http://localhost:9115/probe?target=example.com", ScrapeURL: "http://localhost:9115/probe?target=example.com",
GlobalURL: "http://localhost:9115/probe?target=example.com", GlobalURL: "http://localhost:9115/probe?target=example.com",
@ -1532,10 +1522,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
ScrapeTimeout: "10s", ScrapeTimeout: "10s",
}, },
{ {
DiscoveredLabels: map[string]string{}, DiscoveredLabels: labels.FromStrings(),
Labels: map[string]string{ Labels: labels.FromStrings("job", "test"),
"job": "test",
},
ScrapePool: "test", ScrapePool: "test",
ScrapeURL: "http://example.com:8080/metrics", ScrapeURL: "http://example.com:8080/metrics",
GlobalURL: "http://example.com:8080/metrics", GlobalURL: "http://example.com:8080/metrics",