This commit is contained in:
ANIRUDH 2025-08-05 20:03:00 +05:30 committed by GitHub
commit 7935e333f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 9 deletions

View File

@ -1195,7 +1195,8 @@ func main() {
g.Add( g.Add(
func() error { func() error {
<-reloadReady.C <-reloadReady.C
ticker := time.NewTicker(time.Duration(cfg.autoReloadInterval))
defer ticker.Stop()
for { for {
select { select {
case <-hup: case <-hup:
@ -1220,15 +1221,19 @@ func main() {
} }
} }
} }
case <-time.Tick(time.Duration(cfg.autoReloadInterval)):
case <-ticker.C:
if !cfg.enableAutoReload { if !cfg.enableAutoReload {
continue continue
} }
currentChecksum, err := config.GenerateChecksum(cfg.configFile) currentChecksum, err := config.GenerateChecksum(cfg.configFile)
if err != nil { if err != nil {
checksum = currentChecksum
logger.Error("Failed to generate checksum during configuration reload", "err", err) logger.Error("Failed to generate checksum during configuration reload", "err", err)
} else if currentChecksum == checksum {
continue
}
if currentChecksum == checksum {
continue continue
} }
logger.Info("Configuration file change detected, reloading the configuration.") logger.Info("Configuration file change detected, reloading the configuration.")
@ -1237,6 +1242,8 @@ func main() {
logger.Error("Error reloading config", "err", err) logger.Error("Error reloading config", "err", err)
} else { } else {
checksum = currentChecksum checksum = currentChecksum
ticker.Stop()
ticker = time.NewTicker(time.Duration(cfg.autoReloadInterval))
} }
case <-cancel: case <-cancel:
return nil return nil

View File

@ -417,7 +417,27 @@ func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames
func TestAgentSuccessfulStartup(t *testing.T) { func TestAgentSuccessfulStartup(t *testing.T) {
t.Parallel() t.Parallel()
prom := exec.Command(promPath, "-test.main", "--agent", "--web.listen-address=0.0.0.0:0", "--config.file="+agentConfig) tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "agent.yml")
configContent := `
global:
scrape_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
`
require.NoError(t, os.WriteFile(configPath, []byte(configContent), 0o644))
prom := exec.Command(promPath,
"-test.main",
"--enable-feature=agent",
"--web.listen-address=0.0.0.0:0",
"--config.file="+configPath,
"--storage.tsdb.path="+filepath.Join(tempDir, "data"),
)
require.NoError(t, prom.Start()) require.NoError(t, prom.Start())
actualExitStatus := 0 actualExitStatus := 0
@ -428,8 +448,8 @@ func TestAgentSuccessfulStartup(t *testing.T) {
case err := <-done: case err := <-done:
t.Logf("prometheus agent should be still running: %v", err) t.Logf("prometheus agent should be still running: %v", err)
actualExitStatus = prom.ProcessState.ExitCode() actualExitStatus = prom.ProcessState.ExitCode()
case <-time.After(startupTime): case <-time.After(3 * time.Second):
prom.Process.Kill() _ = prom.Process.Kill()
} }
require.Equal(t, 0, actualExitStatus) require.Equal(t, 0, actualExitStatus)
} }

View File

@ -137,9 +137,12 @@ func runTestSteps(t *testing.T, steps []struct {
require.NoError(t, os.WriteFile(configFilePath, []byte(step.configText), 0o644), "Failed to write config file for step") require.NoError(t, os.WriteFile(configFilePath, []byte(step.configText), 0o644), "Failed to write config file for step")
require.Eventually(t, func() bool { require.Eventually(t, func() bool {
return verifyScrapeInterval(t, baseURL, step.expectedInterval) && if step.expectedMetric == 0 {
verifyConfigReloadMetric(t, baseURL, step.expectedMetric) return verifyScrapeInterval(t, baseURL, step.expectedInterval) || verifyConfigReloadMetric(t, baseURL, step.expectedMetric)
}
return verifyScrapeInterval(t, baseURL, step.expectedInterval) && verifyConfigReloadMetric(t, baseURL, step.expectedMetric)
}, 10*time.Second, 500*time.Millisecond, "Prometheus config reload didn't happen in time") }, 10*time.Second, 500*time.Millisecond, "Prometheus config reload didn't happen in time")
} }
} }