mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 22:27:17 +02:00
fix: fails to auto-reload on changes to rule and scrape config files
Signed-off-by: Benjamin Godding <ben.godding@bright.ai>
This commit is contained in:
parent
8cea05dfe4
commit
c024fc2f28
@ -20,6 +20,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
promconfig "github.com/prometheus/common/config"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
@ -49,10 +50,10 @@ func GenerateChecksum(yamlFilePath string) (string, error) {
|
||||
dir := filepath.Dir(yamlFilePath)
|
||||
|
||||
for i, file := range config.RuleFiles {
|
||||
config.RuleFiles[i] = filepath.Join(dir, file)
|
||||
config.RuleFiles[i] = promconfig.JoinDir(dir, file)
|
||||
}
|
||||
for i, file := range config.ScrapeConfigFiles {
|
||||
config.ScrapeConfigFiles[i] = filepath.Join(dir, file)
|
||||
config.ScrapeConfigFiles[i] = promconfig.JoinDir(dir, file)
|
||||
}
|
||||
|
||||
files := map[string][]string{
|
||||
|
@ -14,6 +14,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@ -26,8 +27,10 @@ func TestGenerateChecksum(t *testing.T) {
|
||||
|
||||
// Define paths for the temporary files.
|
||||
yamlFilePath := filepath.Join(tmpDir, "test.yml")
|
||||
ruleFilePath := filepath.Join(tmpDir, "rule_file.yml")
|
||||
scrapeConfigFilePath := filepath.Join(tmpDir, "scrape_config.yml")
|
||||
ruleFile := "rule_file.yml"
|
||||
ruleFilePath := filepath.Join(tmpDir, ruleFile)
|
||||
scrapeConfigFile := "scrape_config.yml"
|
||||
scrapeConfigFilePath := filepath.Join(tmpDir, scrapeConfigFile)
|
||||
|
||||
// Define initial and modified content for the files.
|
||||
originalRuleContent := "groups:\n- name: example\n rules:\n - alert: ExampleAlert"
|
||||
@ -36,13 +39,32 @@ func TestGenerateChecksum(t *testing.T) {
|
||||
originalScrapeConfigContent := "scrape_configs:\n- job_name: example"
|
||||
modifiedScrapeConfigContent := "scrape_configs:\n- job_name: modified_example"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
ruleFilePath string
|
||||
scrapeConfigFilePath string
|
||||
}{
|
||||
{
|
||||
name: "Auto reload using relative path.",
|
||||
ruleFilePath: ruleFile,
|
||||
scrapeConfigFilePath: scrapeConfigFile,
|
||||
},
|
||||
{
|
||||
name: "Auto reload using absolute path.",
|
||||
ruleFilePath: ruleFilePath,
|
||||
scrapeConfigFilePath: scrapeConfigFilePath,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Define YAML content referencing the rule and scrape config files.
|
||||
yamlContent := `
|
||||
yamlContent := fmt.Sprintf(`
|
||||
rule_files:
|
||||
- rule_file.yml
|
||||
- %s
|
||||
scrape_config_files:
|
||||
- scrape_config.yml
|
||||
`
|
||||
- %s
|
||||
`, tc.ruleFilePath, tc.scrapeConfigFilePath)
|
||||
|
||||
// Write initial content to files.
|
||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
||||
@ -118,14 +140,14 @@ scrape_config_files:
|
||||
|
||||
t.Run("Main File Change", func(t *testing.T) {
|
||||
// Modify the main YAML file.
|
||||
modifiedYamlContent := `
|
||||
modifiedYamlContent := fmt.Sprintf(`
|
||||
global:
|
||||
scrape_interval: 3s
|
||||
rule_files:
|
||||
- rule_file.yml
|
||||
- %s
|
||||
scrape_config_files:
|
||||
- scrape_config.yml
|
||||
`
|
||||
- %s
|
||||
`, tc.ruleFilePath, tc.scrapeConfigFilePath)
|
||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||
|
||||
// Checksum should change.
|
||||
@ -142,10 +164,10 @@ scrape_config_files:
|
||||
|
||||
t.Run("Rule File Removed from YAML Config", func(t *testing.T) {
|
||||
// Modify the YAML content to remove the rule file.
|
||||
modifiedYamlContent := `
|
||||
modifiedYamlContent := fmt.Sprintf(`
|
||||
scrape_config_files:
|
||||
- scrape_config.yml
|
||||
`
|
||||
- %s
|
||||
`, tc.scrapeConfigFilePath)
|
||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||
|
||||
// Checksum should change.
|
||||
@ -162,10 +184,10 @@ scrape_config_files:
|
||||
|
||||
t.Run("Scrape Config Removed from YAML Config", func(t *testing.T) {
|
||||
// Modify the YAML content to remove the scrape config file.
|
||||
modifiedYamlContent := `
|
||||
modifiedYamlContent := fmt.Sprintf(`
|
||||
rule_files:
|
||||
- rule_file.yml
|
||||
`
|
||||
- %s
|
||||
`, tc.ruleFilePath)
|
||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||
|
||||
// Checksum should change.
|
||||
@ -211,6 +233,8 @@ rule_files:
|
||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||
require.Equal(t, originalChecksum, revertedChecksum)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// calculateChecksum generates a checksum for the given YAML file path.
|
||||
|
Loading…
Reference in New Issue
Block a user