From 42cb1dd413278566d9b2140892f61f222facb70a Mon Sep 17 00:00:00 2001 From: Cody Kaczynski Date: Fri, 13 Dec 2024 13:33:51 -0500 Subject: [PATCH] fix: fails to auto-reload on changes to rule and scrape config files Fixes #15673 An issue was reported in the CNCF Slack regarding the new auto-reload feature in Prometheus 3.0 failing to reload on changes to rule files and scrape config files. Signed-off-by: Cody Kaczynski --- config/reload.go | 10 ++++++++-- config/reload_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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))