From e9f08b1e6adc1064cec7447b6873d41611034f28 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 20 Apr 2022 16:40:17 +0300 Subject: [PATCH] lib/promscrape/discovery/kubernetes: do not pre-allocate memory for ScrapeWork objects There is high chance that ScrapeWork objects won't be generated because of relabeling --- lib/promscrape/config_test.go | 2 +- lib/promscrape/discovery/kubernetes/api_watcher.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/promscrape/config_test.go b/lib/promscrape/config_test.go index 85a5433583..b52d02d23a 100644 --- a/lib/promscrape/config_test.go +++ b/lib/promscrape/config_test.go @@ -28,7 +28,7 @@ func TestInternStringConcurrent(t *testing.T) { resultCh <- testInternString(t) }() } - timer := time.NewTimer(5*time.Second) + timer := time.NewTimer(5 * time.Second) for i := 0; i < concurrency; i++ { select { case err := <-resultCh: diff --git a/lib/promscrape/discovery/kubernetes/api_watcher.go b/lib/promscrape/discovery/kubernetes/api_watcher.go index b142ec3991..fd01321506 100644 --- a/lib/promscrape/discovery/kubernetes/api_watcher.go +++ b/lib/promscrape/discovery/kubernetes/api_watcher.go @@ -135,7 +135,8 @@ func (aw *apiWatcher) removeScrapeWorks(uw *urlWatcher, key string) { } func getScrapeWorkObjectsForLabels(swcFunc ScrapeWorkConstructorFunc, labelss []map[string]string) []interface{} { - swos := make([]interface{}, 0, len(labelss)) + // Do not pre-allocate swos, since it is likely the swos will be empty because of relabeling + var swos []interface{} for _, labels := range labelss { swo := swcFunc(labels) // The reflect check is needed because of https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1