mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 14:17:12 +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"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
promconfig "github.com/prometheus/common/config"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,10 +50,10 @@ 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)
|
config.RuleFiles[i] = promconfig.JoinDir(dir, file)
|
||||||
}
|
}
|
||||||
for i, file := range config.ScrapeConfigFiles {
|
for i, file := range config.ScrapeConfigFiles {
|
||||||
config.ScrapeConfigFiles[i] = filepath.Join(dir, file)
|
config.ScrapeConfigFiles[i] = promconfig.JoinDir(dir, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
files := map[string][]string{
|
files := map[string][]string{
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@ -26,8 +27,10 @@ func TestGenerateChecksum(t *testing.T) {
|
|||||||
|
|
||||||
// Define paths for the temporary files.
|
// Define paths for the temporary files.
|
||||||
yamlFilePath := filepath.Join(tmpDir, "test.yml")
|
yamlFilePath := filepath.Join(tmpDir, "test.yml")
|
||||||
ruleFilePath := filepath.Join(tmpDir, "rule_file.yml")
|
ruleFile := "rule_file.yml"
|
||||||
scrapeConfigFilePath := filepath.Join(tmpDir, "scrape_config.yml")
|
ruleFilePath := filepath.Join(tmpDir, ruleFile)
|
||||||
|
scrapeConfigFile := "scrape_config.yml"
|
||||||
|
scrapeConfigFilePath := filepath.Join(tmpDir, scrapeConfigFile)
|
||||||
|
|
||||||
// Define initial and modified content for the files.
|
// Define initial and modified content for the files.
|
||||||
originalRuleContent := "groups:\n- name: example\n rules:\n - alert: ExampleAlert"
|
originalRuleContent := "groups:\n- name: example\n rules:\n - alert: ExampleAlert"
|
||||||
@ -36,181 +39,202 @@ func TestGenerateChecksum(t *testing.T) {
|
|||||||
originalScrapeConfigContent := "scrape_configs:\n- job_name: example"
|
originalScrapeConfigContent := "scrape_configs:\n- job_name: example"
|
||||||
modifiedScrapeConfigContent := "scrape_configs:\n- job_name: modified_example"
|
modifiedScrapeConfigContent := "scrape_configs:\n- job_name: modified_example"
|
||||||
|
|
||||||
// Define YAML content referencing the rule and scrape config files.
|
testCases := []struct {
|
||||||
yamlContent := `
|
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 := fmt.Sprintf(`
|
||||||
rule_files:
|
rule_files:
|
||||||
- rule_file.yml
|
- %s
|
||||||
scrape_config_files:
|
scrape_config_files:
|
||||||
- scrape_config.yml
|
- %s
|
||||||
`
|
`, tc.ruleFilePath, tc.scrapeConfigFilePath)
|
||||||
|
|
||||||
// Write initial content to files.
|
// Write initial content to files.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
||||||
|
|
||||||
// Generate the original checksum.
|
// Generate the original checksum.
|
||||||
originalChecksum := calculateChecksum(t, yamlFilePath)
|
originalChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
|
|
||||||
t.Run("Rule File Change", func(t *testing.T) {
|
t.Run("Rule File Change", func(t *testing.T) {
|
||||||
// Modify the rule file.
|
// Modify the rule file.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(modifiedRuleContent), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(modifiedRuleContent), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
||||||
|
|
||||||
// Revert the rule file.
|
// Revert the rule file.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Scrape Config Change", func(t *testing.T) {
|
t.Run("Scrape Config Change", func(t *testing.T) {
|
||||||
// Modify the scrape config file.
|
// Modify the scrape config file.
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(modifiedScrapeConfigContent), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(modifiedScrapeConfigContent), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
||||||
|
|
||||||
// Revert the scrape config file.
|
// Revert the scrape config file.
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Rule File Deletion", func(t *testing.T) {
|
t.Run("Rule File Deletion", func(t *testing.T) {
|
||||||
// Delete the rule file.
|
// Delete the rule file.
|
||||||
require.NoError(t, os.Remove(ruleFilePath))
|
require.NoError(t, os.Remove(ruleFilePath))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
deletedChecksum := calculateChecksum(t, yamlFilePath)
|
deletedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, deletedChecksum)
|
require.NotEqual(t, originalChecksum, deletedChecksum)
|
||||||
|
|
||||||
// Restore the rule file.
|
// Restore the rule file.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Scrape Config Deletion", func(t *testing.T) {
|
t.Run("Scrape Config Deletion", func(t *testing.T) {
|
||||||
// Delete the scrape config file.
|
// Delete the scrape config file.
|
||||||
require.NoError(t, os.Remove(scrapeConfigFilePath))
|
require.NoError(t, os.Remove(scrapeConfigFilePath))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
deletedChecksum := calculateChecksum(t, yamlFilePath)
|
deletedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, deletedChecksum)
|
require.NotEqual(t, originalChecksum, deletedChecksum)
|
||||||
|
|
||||||
// Restore the scrape config file.
|
// Restore the scrape config file.
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Main File Change", func(t *testing.T) {
|
t.Run("Main File Change", func(t *testing.T) {
|
||||||
// Modify the main YAML file.
|
// Modify the main YAML file.
|
||||||
modifiedYamlContent := `
|
modifiedYamlContent := fmt.Sprintf(`
|
||||||
global:
|
global:
|
||||||
scrape_interval: 3s
|
scrape_interval: 3s
|
||||||
rule_files:
|
rule_files:
|
||||||
- rule_file.yml
|
- %s
|
||||||
scrape_config_files:
|
scrape_config_files:
|
||||||
- scrape_config.yml
|
- %s
|
||||||
`
|
`, tc.ruleFilePath, tc.scrapeConfigFilePath)
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
||||||
|
|
||||||
// Revert the main YAML file.
|
// Revert the main YAML file.
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Rule File Removed from YAML Config", func(t *testing.T) {
|
t.Run("Rule File Removed from YAML Config", func(t *testing.T) {
|
||||||
// Modify the YAML content to remove the rule file.
|
// Modify the YAML content to remove the rule file.
|
||||||
modifiedYamlContent := `
|
modifiedYamlContent := fmt.Sprintf(`
|
||||||
scrape_config_files:
|
scrape_config_files:
|
||||||
- scrape_config.yml
|
- %s
|
||||||
`
|
`, tc.scrapeConfigFilePath)
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
||||||
|
|
||||||
// Revert the YAML content.
|
// Revert the YAML content.
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Scrape Config Removed from YAML Config", func(t *testing.T) {
|
t.Run("Scrape Config Removed from YAML Config", func(t *testing.T) {
|
||||||
// Modify the YAML content to remove the scrape config file.
|
// Modify the YAML content to remove the scrape config file.
|
||||||
modifiedYamlContent := `
|
modifiedYamlContent := fmt.Sprintf(`
|
||||||
rule_files:
|
rule_files:
|
||||||
- rule_file.yml
|
- %s
|
||||||
`
|
`, tc.ruleFilePath)
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
modifiedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
require.NotEqual(t, originalChecksum, modifiedChecksum)
|
||||||
|
|
||||||
// Revert the YAML content.
|
// Revert the YAML content.
|
||||||
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(yamlContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Empty Rule File", func(t *testing.T) {
|
t.Run("Empty Rule File", func(t *testing.T) {
|
||||||
// Write an empty rule file.
|
// Write an empty rule file.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(""), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(""), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
emptyChecksum := calculateChecksum(t, yamlFilePath)
|
emptyChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, emptyChecksum)
|
require.NotEqual(t, originalChecksum, emptyChecksum)
|
||||||
|
|
||||||
// Restore the rule file.
|
// Restore the rule file.
|
||||||
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Empty Scrape Config File", func(t *testing.T) {
|
t.Run("Empty Scrape Config File", func(t *testing.T) {
|
||||||
// Write an empty scrape config file.
|
// Write an empty scrape config file.
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(""), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(""), 0o644))
|
||||||
|
|
||||||
// Checksum should change.
|
// Checksum should change.
|
||||||
emptyChecksum := calculateChecksum(t, yamlFilePath)
|
emptyChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.NotEqual(t, originalChecksum, emptyChecksum)
|
require.NotEqual(t, originalChecksum, emptyChecksum)
|
||||||
|
|
||||||
// Restore the scrape config file.
|
// Restore the scrape config file.
|
||||||
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
require.NoError(t, os.WriteFile(scrapeConfigFilePath, []byte(originalScrapeConfigContent), 0o644))
|
||||||
|
|
||||||
// Checksum should return to the original.
|
// Checksum should return to the original.
|
||||||
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
revertedChecksum := calculateChecksum(t, yamlFilePath)
|
||||||
require.Equal(t, originalChecksum, revertedChecksum)
|
require.Equal(t, originalChecksum, revertedChecksum)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculateChecksum generates a checksum for the given YAML file path.
|
// calculateChecksum generates a checksum for the given YAML file path.
|
||||||
|
Loading…
Reference in New Issue
Block a user