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 <cody@codykaczynski.com>
This commit is contained in:
Cody Kaczynski 2024-12-13 13:33:51 -05:00 committed by Cody Kaczynski
parent 7802ca263d
commit 42cb1dd413
No known key found for this signature in database
2 changed files with 20 additions and 2 deletions

View file

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -49,10 +50,15 @@ func GenerateChecksum(yamlFilePath string) (string, error) {
dir := filepath.Dir(yamlFilePath) dir := filepath.Dir(yamlFilePath)
for i, file := range config.RuleFiles { 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 { 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{ files := map[string][]string{

View file

@ -40,8 +40,12 @@ func TestGenerateChecksum(t *testing.T) {
yamlContent := ` yamlContent := `
rule_files: rule_files:
- rule_file.yml - rule_file.yml
- /etc/prometheus/rules/rule_file.yml
- /etc/prometheus/rules/glob/*.yml
scrape_config_files: scrape_config_files:
- scrape_config.yml - scrape_config.yml
- /etc/prometheus/scrape_config_files/scrape_config.yml
- /etc/prometheus/scrape_config_files/glob/*.yml
` `
// Write initial content to files. // Write initial content to files.
@ -123,8 +127,12 @@ global:
scrape_interval: 3s scrape_interval: 3s
rule_files: rule_files:
- rule_file.yml - rule_file.yml
- /etc/prometheus/rules/rule_file.yml
- /etc/prometheus/rules/glob/*.yml
scrape_config_files: scrape_config_files:
- scrape_config.yml - 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)) require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
@ -145,6 +153,8 @@ scrape_config_files:
modifiedYamlContent := ` modifiedYamlContent := `
scrape_config_files: scrape_config_files:
- scrape_config.yml - 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)) require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
@ -165,6 +175,8 @@ scrape_config_files:
modifiedYamlContent := ` modifiedYamlContent := `
rule_files: rule_files:
- rule_file.yml - rule_file.yml
- /etc/prometheus/rules/rule_file.yml
- /etc/prometheus/rules/glob/*.yml
` `
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))