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.
type Target struct {
// 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.
Labels map[string]string `json:"labels"`
Labels labels.Labels `json:"labels"`
ScrapePool string `json:"scrapePool"`
ScrapeURL string `json:"scrapeUrl"`
@ -1024,8 +1024,8 @@ func (api *API) targets(r *http.Request) apiFuncResult {
globalURL, err := getGlobalURL(target.URL(), api.globalURLOptions)
res.ActiveTargets = append(res.ActiveTargets, &Target{
DiscoveredLabels: target.DiscoveredLabels().Map(),
Labels: target.Labels().Map(),
DiscoveredLabels: target.DiscoveredLabels(),
Labels: target.Labels(),
ScrapePool: key,
ScrapeURL: target.URL().String(),
GlobalURL: globalURL.String(),

View file

@ -1411,10 +1411,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response: &TargetDiscovery{
ActiveTargets: []*Target{
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "blackbox",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "blackbox"),
ScrapePool: "blackbox",
ScrapeURL: "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",
},
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "test",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "test"),
ScrapePool: "test",
ScrapeURL: "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{
ActiveTargets: []*Target{
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "blackbox",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "blackbox"),
ScrapePool: "blackbox",
ScrapeURL: "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",
},
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "test",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "test"),
ScrapePool: "test",
ScrapeURL: "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{
ActiveTargets: []*Target{
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "blackbox",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "blackbox"),
ScrapePool: "blackbox",
ScrapeURL: "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",
},
{
DiscoveredLabels: map[string]string{},
Labels: map[string]string{
"job": "test",
},
DiscoveredLabels: labels.FromStrings(),
Labels: labels.FromStrings("job", "test"),
ScrapePool: "test",
ScrapeURL: "http://example.com:8080/metrics",
GlobalURL: "http://example.com:8080/metrics",