From 67b10896d2579716bbf80d171890f1d9d482c9c5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 22 Apr 2022 13:19:20 +0300 Subject: [PATCH] lib/promscrape: prevent from memory leaks on -promscrape.config reload when only a small part of scrape jobs is updated This is a follow-up after 26b78ad707af75074df1d5444add07a4b261ad6a --- lib/promscrape/config.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index 8c8a25c48..4b7b8cf53 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -166,6 +166,10 @@ func areEqualScrapeConfigs(a, b *ScrapeConfig) bool { return string(sa) == string(sb) } +func (sc *ScrapeConfig) unmarshal(data []byte) error { + return yaml.UnmarshalStrict(data, sc) +} + func (sc *ScrapeConfig) marshal() []byte { data, err := yaml.Marshal(sc) if err != nil { @@ -411,15 +415,29 @@ func (cfg *Config) parseData(data []byte, path string) ([]byte, error) { // Initialize cfg.ScrapeConfigs for i, sc := range cfg.ScrapeConfigs { + // Make a copy of sc in order to remove references to `data` memory. + // This should prevent from memory leaks on config reload. + sc = sc.clone() + cfg.ScrapeConfigs[i] = sc + swc, err := getScrapeWorkConfig(sc, cfg.baseDir, &cfg.Global) if err != nil { - return nil, fmt.Errorf("cannot parse `scrape_config` #%d: %w", i+1, err) + return nil, fmt.Errorf("cannot parse `scrape_config`: %w", err) } sc.swc = swc } return dataNew, nil } +func (sc *ScrapeConfig) clone() *ScrapeConfig { + data := sc.marshal() + var scCopy ScrapeConfig + if err := scCopy.unmarshal(data); err != nil { + logger.Panicf("BUG: cannot unmarshal scrape config: %s", err) + } + return &scCopy +} + func getSWSByJob(sws []*ScrapeWork) map[string][]*ScrapeWork { m := make(map[string][]*ScrapeWork) for _, sw := range sws {