lib/promscrape: do not re-use previously loaded scrape targets on failed attempt to load updated scrape targets at file_sd_configs

The logic employed for re-using the previously loaded scrape target was broken initially.
The commit cc0427897c tried to fix it, but the new logic
became too complex and fragile. So it is better to just remove this logic,
since the targets from temporarily broken file should be eventually loaded on next
attempts every -promscrape.fileSDCheckInterval

This also allows removing fragile hacks around __vm_filepath label.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3989
This commit is contained in:
Aliaksandr Valialkin 2023-04-02 21:05:01 -07:00
parent 4c5cc89007
commit 8ff2f1cc02
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 4 additions and 57 deletions

View file

@ -691,26 +691,11 @@ func (cfg *Config) getEurekaSDScrapeWork(prev []*ScrapeWork) []*ScrapeWork {
// getFileSDScrapeWork returns `file_sd_configs` ScrapeWork from cfg.
func (cfg *Config) getFileSDScrapeWork(prev []*ScrapeWork) []*ScrapeWork {
// Create a map for the previous scrape work.
swsMapPrev := make(map[string][]*ScrapeWork)
for _, sw := range prev {
filepath := promrelabel.GetLabelValueByName(sw.Labels, "__vm_filepath")
if len(filepath) == 0 {
logger.Panicf("BUG: missing `__vm_filepath` label")
} else {
// user can define many file_sd_config with the same path and it will produce the same ScrapeWorks
// in this case we just make key for map as job name and filepath with ":" delimiter,
// it will create each job with its ScrapeWorks
key := fmt.Sprintf("%s:%s", sw.Job(), filepath)
swsMapPrev[key] = append(swsMapPrev[key], sw)
}
}
dst := make([]*ScrapeWork, 0, len(prev))
for _, sc := range cfg.ScrapeConfigs {
configPaths := make(map[string]struct{}, len(sc.FileSDConfigs))
for j := range sc.FileSDConfigs {
sdc := &sc.FileSDConfigs[j]
dst = sdc.appendScrapeWork(dst, swsMapPrev, cfg.baseDir, sc.swc, configPaths)
dst = sdc.appendScrapeWork(dst, cfg.baseDir, sc.swc)
}
}
return dst
@ -1021,7 +1006,7 @@ func appendScrapeWorkForTargetLabels(dst []*ScrapeWork, swc *scrapeWorkConfig, t
return dst
}
func (sdc *FileSDConfig) appendScrapeWork(dst []*ScrapeWork, swsMapPrev map[string][]*ScrapeWork, baseDir string, swc *scrapeWorkConfig, configPaths map[string]struct{}) []*ScrapeWork {
func (sdc *FileSDConfig) appendScrapeWork(dst []*ScrapeWork, baseDir string, swc *scrapeWorkConfig) []*ScrapeWork {
for _, file := range sdc.Files {
pathPattern := fs.GetFilepath(baseDir, file)
paths := []string{pathPattern}
@ -1030,31 +1015,15 @@ func (sdc *FileSDConfig) appendScrapeWork(dst []*ScrapeWork, swsMapPrev map[stri
paths, err = filepath.Glob(pathPattern)
if err != nil {
// Do not return this error, since other files may contain valid scrape configs.
logger.Errorf("invalid pattern %q in `files` section: %s; skipping it", file, err)
logger.Errorf("invalid pattern %q in `file_sd_config->files` section of job_name=%q: %s; skipping it", file, swc.jobName, err)
continue
}
}
for _, path := range paths {
// make a key as for previous ScrapeWorks (swsMapPrev map[string][]*ScrapeWork) and show to user
// warning about identical file_sd_config.
// We skip it because it will make dst with duplicated ScrapeWork.
key := fmt.Sprintf("%s:%s", swc.jobName, path)
if _, ok := configPaths[key]; ok {
logger.Warnf("file_sd_config contains multiple references to %q, ignoring duplicated entry. please check -promscrape.config and remove duplicated configurations", path)
continue
}
configPaths[key] = struct{}{}
stcs, err := loadStaticConfigs(path)
if err != nil {
// Do not return this error, since other paths may contain valid scrape configs.
if sws := swsMapPrev[key]; sws != nil {
// Re-use the previous valid scrape work for this path.
logger.Errorf("keeping the previously loaded `static_configs` from %q because of error when re-loading the file: %s", path, err)
dst = append(dst, sws...)
} else {
logger.Errorf("skipping loading `static_configs` from %q because of error: %s", path, err)
}
logger.Errorf("cannot load file %q for job_name=%q at `file_sd_configs`: %s; skipping this file", path, swc.jobName, err)
continue
}
pathShort := path
@ -1066,7 +1035,6 @@ func (sdc *FileSDConfig) appendScrapeWork(dst []*ScrapeWork, swsMapPrev map[stri
}
metaLabels := map[string]string{
"__meta_filepath": pathShort,
"__vm_filepath": path, // This label is needed for internal promscrape logic
}
for i := range stcs {
dst = stcs[i].appendScrapeWork(dst, swc, metaLabels)

View file

@ -731,15 +731,6 @@ func TestGetFileSDScrapeWorkSuccess(t *testing.T) {
}
resetNonEssentialFields(sws)
// Remove `__vm_filepath` label, since its value depends on the current working dir.
for _, sw := range sws {
for j := range sw.Labels {
label := &sw.Labels[j]
if label.Name == "__vm_filepath" {
label.Value = ""
}
}
}
if !reflect.DeepEqual(sws, expectedSws) {
t.Fatalf("unexpected scrapeWork; got\n%+v\nwant\n%+v", sws, expectedSws)
}
@ -783,10 +774,6 @@ scrape_configs:
Name: "__scrape_timeout__",
Value: "10s",
},
{
Name: "__vm_filepath",
Value: "",
},
{
Name: "instance",
Value: "host1:80",
@ -830,10 +817,6 @@ scrape_configs:
Name: "__scrape_timeout__",
Value: "10s",
},
{
Name: "__vm_filepath",
Value: "",
},
{
Name: "instance",
Value: "host2:80",
@ -877,10 +860,6 @@ scrape_configs:
Name: "__scrape_timeout__",
Value: "10s",
},
{
Name: "__vm_filepath",
Value: "",
},
{
Name: "instance",
Value: "localhost:9090",