diff --git a/config/reload.go b/config/reload.go index 8be1b28d8..3283b425f 100644 --- a/config/reload.go +++ b/config/reload.go @@ -19,6 +19,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "gopkg.in/yaml.v2" ) @@ -49,10 +50,15 @@ func GenerateChecksum(yamlFilePath string) (string, error) { dir := filepath.Dir(yamlFilePath) for i, file := range config.RuleFiles { - config.RuleFiles[i] = filepath.Join(dir, file) + if !strings.Contains(config.RuleFiles[i], dir) { + config.RuleFiles[i] = filepath.Join(dir, file) // Join the directory only if the parent directory is not present in the config + } } + for i, file := range config.ScrapeConfigFiles { - config.ScrapeConfigFiles[i] = filepath.Join(dir, file) + if !strings.Contains(config.ScrapeConfigFiles[i], dir) { + config.ScrapeConfigFiles[i] = filepath.Join(dir, file) // Join the directory only if the parent directory is not present in the config + } } files := map[string][]string{ diff --git a/config/reload_test.go b/config/reload_test.go index f0f44f358..851536ba8 100644 --- a/config/reload_test.go +++ b/config/reload_test.go @@ -40,8 +40,12 @@ func TestGenerateChecksum(t *testing.T) { yamlContent := ` rule_files: - rule_file.yml + - /etc/prometheus/rules/rule_file.yml + - /etc/prometheus/rules/glob/*.yml scrape_config_files: - scrape_config.yml + - /etc/prometheus/scrape_config_files/scrape_config.yml + - /etc/prometheus/scrape_config_files/glob/*.yml ` // Write initial content to files. @@ -123,8 +127,12 @@ global: scrape_interval: 3s rule_files: - rule_file.yml + - /etc/prometheus/rules/rule_file.yml + - /etc/prometheus/rules/glob/*.yml scrape_config_files: - scrape_config.yml + - /etc/prometheus/scrape_config_files/scrape_config.yml + - /etc/prometheus/scrape_config_files/glob/*.yml ` require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) @@ -145,6 +153,8 @@ scrape_config_files: modifiedYamlContent := ` scrape_config_files: - scrape_config.yml + - /etc/prometheus/scrape_config_files/scrape_config.yml + - /etc/prometheus/scrape_config_files/glob/*.yml ` require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) @@ -165,6 +175,8 @@ scrape_config_files: modifiedYamlContent := ` rule_files: - rule_file.yml + - /etc/prometheus/rules/rule_file.yml + - /etc/prometheus/rules/glob/*.yml ` require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))